import json from datetime import datetime, timezone ACCESS_FILE = "./data/access.json" def load_access(): try: with open(ACCESS_FILE, "r") as f: return json.load(f) except FileNotFoundError: return {} def save_access(data): with open(ACCESS_FILE, "w") as f: json.dump(data, 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 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": 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)