mirror of
https://github.com/Noettore/lagomareGateKeeperBot.git
synced 2025-10-15 03:26:40 +02:00
Complete refactor... Some issue...
This commit is contained in:
@@ -1,40 +1,45 @@
|
||||
import json
|
||||
from datetime import datetime, timezone
|
||||
|
||||
ACCESS_FILE = "./data/access.json"
|
||||
class AccessControl:
|
||||
_ACCESS_FILE = "./data/access.json"
|
||||
|
||||
def load_access():
|
||||
try:
|
||||
with open(ACCESS_FILE, "r") as f:
|
||||
return json.load(f)
|
||||
except FileNotFoundError:
|
||||
return {}
|
||||
def __init__(self):
|
||||
self._access = self._load_access()
|
||||
|
||||
def save_access(data):
|
||||
with open(ACCESS_FILE, "w") as f:
|
||||
json.dump(data, f)
|
||||
def _load_access(self) -> dict:
|
||||
try:
|
||||
with open(self._ACCESS_FILE, "r") as f:
|
||||
return json.load(f)
|
||||
except FileNotFoundError:
|
||||
return {}
|
||||
|
||||
def _save_access(self):
|
||||
with open(self._ACCESS_FILE, "w") as f:
|
||||
json.dump(self._access, f)
|
||||
|
||||
def get_grantor(user_id, gate):
|
||||
access = load_access().get(str(user_id), {})
|
||||
entry = access.get(gate) or access.get("all")
|
||||
return entry.get("grantor", "")
|
||||
def get_grantor(self, user_id: str, gate: str) -> str:
|
||||
access = self._access.get(user_id, {})
|
||||
entry = access.get(gate) or access.get("all")
|
||||
return entry.get("grantor", "")
|
||||
|
||||
def can_open_gate(user_id, gate):
|
||||
access = load_access().get(str(user_id), {})
|
||||
entry = access.get(gate) or access.get("all")
|
||||
if not entry or entry["type"] != "timed":
|
||||
def can_open_gate(self, user_id: str, gate: str) -> bool:
|
||||
access = self._access.get(user_id, {})
|
||||
entry = access.get(gate) or access.get("all")
|
||||
if not entry or entry["type"] != "timed":
|
||||
return False
|
||||
if datetime.now(timezone.utc) < datetime.fromisoformat(entry["expires_at"].replace("Z", "+00:00")):
|
||||
return True
|
||||
return False
|
||||
if datetime.now(timezone.utc) < datetime.fromisoformat(entry["expires_at"].replace("Z", "+00:00")):
|
||||
return True
|
||||
return False
|
||||
|
||||
def grant_access(user_id, gate, expires_at, grantor_id):
|
||||
access = load_access()
|
||||
user_access = access.get(str(user_id), {})
|
||||
user_access[gate] = {
|
||||
"type": "timed",
|
||||
"expires_at": expires_at,
|
||||
"grantor": grantor_id
|
||||
}
|
||||
access[str(user_id)] = user_access
|
||||
save_access(access)
|
||||
def grant_access(self, user_id: str, gate: str, expires_at: str, grantor_id: str):
|
||||
user_access = self._access.get(user_id, {})
|
||||
user_access[gate] = {
|
||||
"type": "timed",
|
||||
"granted_at": datetime.now(timezone.utc).isoformat(),
|
||||
"expires_at": expires_at,
|
||||
"grantor": grantor_id,
|
||||
"last_used_at": None
|
||||
}
|
||||
self._access[user_id] = user_access
|
||||
self._save_access()
|
||||
|
Reference in New Issue
Block a user