More refactoring

This commit is contained in:
2025-05-14 23:27:50 +02:00
parent ebd8529c41
commit 4a3d2746fb
3 changed files with 25 additions and 10 deletions

View File

@@ -9,7 +9,7 @@ class Role(Enum):
GUEST = "guest"
class _Grant:
def __init__(self, grantor: str, expires_at: datetime, granted_at: datetime = datetime.now(), last_used_at: datetime = None, status: int | Status = Status.ENABLED):
def __init__(self, grantor: str, expires_at: datetime, granted_at: datetime = datetime.now(), last_used_at: datetime = None, status: Status = Status.ENABLED):
self._grantor: str = grantor
self._granted_at: datetime = granted_at
self._expires_at = expires_at
@@ -50,14 +50,14 @@ class _Grant:
return self._status
class _User:
def __init__(self, id: str, username: str, fullname: str, role: str | Role = Role.GUEST, credentials: dict = None, grants: dict = None, status: int | Status = Status.ENABLED):
def __init__(self, id: str, username: str, fullname: str, role: Role = Role.GUEST, credentials: Credential = Credential("", ""), grants: dict = None, status: Status = Status.ENABLED):
self._id: str = id
self._username: str = username
self._fullname: str = fullname
self._role: Role = role if isinstance(role, Role) else Role(role)
self._credentials: Credential = Credential(**credentials)
self._grants: dict[str, _Grant] = {gate: _Grant(**grant) for gate, grant in grants.items()} if grants else {}
self._status: Status = status if isinstance(status, Status) else Status(status)
self._credentials: Credential = credentials
self._grants: dict[str, _Grant] = {gate:_Grant(grant.get("grantor", ""), datetime.fromisoformat(grant.get("expires_at").replace("Z", "+00:00"))) for gate, grant in grants.items()} if grants else {}
self._status: Status = status
def __dict__(self):
return {
@@ -133,7 +133,22 @@ class Users:
def _load_users(self) -> dict[str, _User]:
try:
with open(self._json_path, "r") as f:
return {uid: _User(**info) for uid, info in json.load(f)}
users = {}
for uid, info in json.load(f).items():
try:
user = _User(
id=uid,
username=info.get("username", ""),
fullname=info.get("fullname", ""),
role=info.get("role", Role.GUEST),
credentials=Credential(info.get("credentials", {}).get("username", ""), info.get("credentials", {}).get("password", "")),
grants={gate: info for gate, info in info.get("grants", {}).items()},
status=info.get("status", Status.ENABLED)
)
users[uid] = user
except:
continue
return users
except Exception:
return {}