diff --git a/handlers/access.py b/handlers/access.py index c44e643..2079d65 100644 --- a/handlers/access.py +++ b/handlers/access.py @@ -14,7 +14,7 @@ async def requestaccess(update: Update, context: ContextTypes.DEFAULT_TYPE, user if not context.args: return await update.message.reply_text("Usage: `/requestaccess`", parse_mode="Markdown") requester = users.get_fullname(user_id) or users.get_username(user_id) - text = (f"Access request: {requester} ({user_id}) requests access.\nUse `/approve {user_id} YYYY-MM-DDTHH:MM:SSZ` to grant access.") + text = (f"Access request: {requester} ({user_id}) requests access.\nUse `/grantaccess {user_id} YYYY-MM-DDTHH:MM:SSZ` to grant access.") await update.message.reply_text("Your request has been submitted.") admins = users.get_admins() for admin_id in admins: @@ -42,4 +42,4 @@ async def grantaccess(update: Update, context: ContextTypes.DEFAULT_TYPE, users: except Exception as e: print(f"Failed to notify {user_id} that admin {grantor_id} granted access for {gate} up to {expires_at}: {e}") except Exception: - await update.message.reply_text("Usage: `/approve `") \ No newline at end of file + await update.message.reply_text("Usage: `/grantaccess `") \ No newline at end of file diff --git a/handlers/gates.py b/handlers/gates.py index d3550c3..15363f9 100644 --- a/handlers/gates.py +++ b/handlers/gates.py @@ -38,12 +38,26 @@ async def opengate(update: Update, context: ContextTypes.DEFAULT_TYPE, users: Us return await update.message.reply_text(f"Gate {gate_name} opened!") await update.message.reply_text(f"ERROR: Cannot open gate {gate_name}") -async def open_gate_menu(update: Update, context: ContextTypes.DEFAULT_TYPE, gates: Gates): +async def open_gate_menu(update: Update, context: ContextTypes.DEFAULT_TYPE, users: Users, gates: Gates): + assert update.effective_user is not None + + user_id = str(update.effective_user.id) + granted_gates = users.get_granted_gates(user_id) + if not granted_gates: + if update.callback_query: + await update.callback_query.answer("You have no gates to open.") + elif update.message: + return await update.message.reply_text("You have no gates to open.") + + if 'all' in granted_gates: + granted_gates = gates.get_all_gates_id() # Show a list of available gates as buttons - keyboard = [ - [InlineKeyboardButton(gate.name, callback_data=f"opengate_{gate_id}")] - for gate_id, gate in gates._gates.items() - ] + keyboard = [] + for gate_id in granted_gates: + gate_name = gates.get_name(gate_id) + assert gate_name is not None + keyboard.append([InlineKeyboardButton(gate_name, callback_data=f"opengate_{gate_id}")]) + reply_markup = InlineKeyboardMarkup(keyboard) if update.callback_query: await update.callback_query.answer() diff --git a/handlers/main.py b/handlers/main.py index 24337b0..8df9401 100644 --- a/handlers/main.py +++ b/handlers/main.py @@ -45,6 +45,6 @@ async def handle_main_menu_callback(update: Update, context: ContextTypes.DEFAUL assert query is not None data = query.data if data == "open_gate_menu": - await open_gate_menu(update, context, gates=gates) + await open_gate_menu(update, context, users=users, gates=gates) elif data == "request_access": await requestaccess(update, context, users=users) \ No newline at end of file diff --git a/models/gates.py b/models/gates.py index a2b396a..d225b24 100644 --- a/models/gates.py +++ b/models/gates.py @@ -31,6 +31,9 @@ class Gates: def get_name(self, gate: str) -> str | None: return self._gates[gate].name if gate in self._gates else None + + def get_all_gates_id(self) -> list[str]: + return [gate for gate in self._gates.keys() if self._gates[gate].status == Status.ENABLED] def open_gate(self, gate: str, credentials: Credential) -> bool: if gate not in self._gates: diff --git a/models/users.py b/models/users.py index 7b2d4d8..02dbfd2 100644 --- a/models/users.py +++ b/models/users.py @@ -196,3 +196,13 @@ class Users: grant.last_used_at = datetime.now(timezone.utc) self._save_users() return True + + def get_granted_gates(self, user_id: str) -> list[str]: + user = self._users.get(user_id) + if not user: + return [] + if user.role == Role.ADMIN or user.role == Role.MEMBER: + return ['all'] + if 'all' in user.grants and user.grants['all'].status == Status.ENABLED and user.grants['all'].expires_at > datetime.now(timezone.utc): + return ['all'] + return [gate for gate, grant in user.grants.items() if grant.status == Status.ENABLED and grant.expires_at > datetime.now(timezone.utc)] \ No newline at end of file