Spaces:
Running
Running
File size: 2,196 Bytes
01ae535 b77c0a2 01ae535 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
import json
import os
from passlib.context import CryptContext
# Tạo thư mục data nếu chưa tồn tại
os.makedirs("data", exist_ok=True)
USERS_FILE = "data/users.json"
# Context để hash password
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
# Hàm để đọc users từ file JSON
def get_users():
if not os.path.exists(USERS_FILE):
# Tạo file với dữ liệu mặc định nếu chưa tồn tại
default_users = {
"chien_vm": {
"username": "chien_vm",
"full_name": "Chien VM",
"email": "[email protected]",
"hashed_password": "$2b$12$RtcKFk7B3hKd7vYkwxdFN.eBXSiryQIRUG.OoJ07Pl9lzHNUkugMi",
"disabled": False,
},
"hoi_nv": {
"username": "hoi_nv",
"full_name": "Hoi NV",
"email": "[email protected]",
"hashed_password": "$2b$12$RtcKFk7B3hKd7vYkwxdFN.eBXSiryQIRUG.OoJ07Pl9lzHNUkugMi",
"disabled": False,
},
}
save_users(default_users)
return default_users
with open(USERS_FILE, "r", encoding="utf-8") as f:
return json.load(f)
# Hàm để lưu users vào file JSON
def save_users(users_data):
with open(USERS_FILE, "w", encoding="utf-8") as f:
json.dump(users_data, f, indent=4, ensure_ascii=False)
# Hàm để tạo tài khoản mới
def create_account(username, password):
# Kiểm tra xem username đã tồn tại chưa
users = get_users()
if username in users:
return False, "Username already exists"
# Hash password
hashed_password = pwd_context.hash(password)
# Tạo user mới
new_user = {
"username": username,
"full_name": username, # Mặc định full_name là username
"email": "", # Không yêu cầu email
"hashed_password": hashed_password,
"disabled": False,
}
# Thêm user mới vào database
users[username] = new_user
save_users(users)
return True, "Account created successfully"
# Để tương thích với code cũ
users_db = get_users()
|