mirror of
https://github.com/Noettore/lagomareGateKeeperBot.git
synced 2025-10-14 19:16:40 +02:00
60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
import json
|
|
|
|
USER_FILE = "./data/users.json"
|
|
|
|
def load_users():
|
|
try:
|
|
with open(USER_FILE, "r") as f:
|
|
return json.load(f)
|
|
except FileNotFoundError:
|
|
return {}
|
|
|
|
def save_users(data):
|
|
with open(USER_FILE, "w") as f:
|
|
json.dump(data, f)
|
|
|
|
def update_user(user_id, username, fullname):
|
|
has_changes = False
|
|
users = load_users()
|
|
user = users.get(str(user_id), {})
|
|
|
|
if user.get("username", "") != username:
|
|
has_changes = True
|
|
user["username"] = username
|
|
if user.get("fullname", "") != fullname:
|
|
has_changes = True
|
|
user["fullname"] = fullname
|
|
if not user.get("role"):
|
|
has_changes = True
|
|
user["role"] = "guest"
|
|
|
|
if has_changes:
|
|
users[str(user_id)] = user
|
|
save_users(users)
|
|
|
|
|
|
def get_role(user_id):
|
|
return load_users().get(str(user_id), {}).get("role", "guest")
|
|
|
|
def set_credentials(user_id, username, password):
|
|
users = load_users()
|
|
user = users.get(str(user_id), {})
|
|
user["credentials"] = {"username": username, "password": password}
|
|
users[str(user_id)] = user
|
|
save_users(users)
|
|
|
|
def get_credentials(user_id):
|
|
return load_users().get(str(user_id), {}).get("credentials")
|
|
|
|
def get_grantor_credentials(user_id, gate):
|
|
from access_control import load_access
|
|
access = load_access().get(str(user_id), {})
|
|
entry = access.get(gate) or access.get("all")
|
|
if not entry:
|
|
return None
|
|
grantor_id = entry.get("grantor")
|
|
return get_credentials(grantor_id)
|
|
|
|
def get_admins():
|
|
return [uid for uid, u in load_users().items() if u.get("role") == "admin"]
|