From e96a4d1c34bda53a332d3bef8fb4e566ffbeb692 Mon Sep 17 00:00:00 2001 From: jafilson Date: Wed, 6 Mar 2024 15:35:32 -0500 Subject: [PATCH 1/2] added hidden admin commands --- webex_bot/commands/help.py | 26 ++++++++++++++------------ webex_bot/models/command.py | 3 ++- webex_bot/webex_bot.py | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 13 deletions(-) diff --git a/webex_bot/commands/help.py b/webex_bot/commands/help.py index 1028ed6..9ed3520 100644 --- a/webex_bot/commands/help.py +++ b/webex_bot/commands/help.py @@ -76,17 +76,19 @@ def build_actions_and_hints(self, thread_parent_id): # Sort list by keyword sorted_commands_list = sorted(self.commands, key=lambda command: ( command.command_keyword is not None, command.command_keyword)) + for command in sorted_commands_list: - if command.help_message and command.command_keyword != HELP_COMMAND_KEYWORD: - action = Submit( - title=f"{command.help_message}", - data={COMMAND_KEYWORD_KEY: command.command_keyword, - 'thread_parent_id': thread_parent_id}, - ) - help_actions.append(action) - - hint = Fact(title=command.command_keyword, - value=command.help_message) - - hint_texts.append(hint) + if not command.admin_command: + if command.help_message and command.command_keyword != HELP_COMMAND_KEYWORD: + action = Submit( + title=f"{command.help_message}", + data={COMMAND_KEYWORD_KEY: command.command_keyword, + 'thread_parent_id': thread_parent_id}, + ) + help_actions.append(action) + + hint = Fact(title=command.command_keyword, + value=command.help_message) + + hint_texts.append(hint) return help_actions, hint_texts diff --git a/webex_bot/models/command.py b/webex_bot/models/command.py index 1d100d3..3ce0fa3 100644 --- a/webex_bot/models/command.py +++ b/webex_bot/models/command.py @@ -11,7 +11,7 @@ class Command(ABC): def __init__(self, command_keyword=None, chained_commands=[], card=None, help_message=None, delete_previous_message=False, - card_callback_keyword=None, approved_rooms=None): + card_callback_keyword=None, approved_rooms=None, admin_command=None): """ Create a new bot command. @@ -45,6 +45,7 @@ def __init__(self, command_keyword=None, chained_commands=[], card=None, help_me self.delete_previous_message = delete_previous_message self.approved_rooms = approved_rooms self.chained_commands = chained_commands + self.admin_command = admin_command # Now, if this card has a Action.Submit action, let's read the callback keyword, # or if it doesnt exist, add it. diff --git a/webex_bot/webex_bot.py b/webex_bot/webex_bot.py index 2fc94e6..71522f3 100644 --- a/webex_bot/webex_bot.py +++ b/webex_bot/webex_bot.py @@ -29,6 +29,7 @@ def __init__(self, approved_users=[], approved_domains=[], approved_rooms=[], + approved_admins=[], device_url=DEFAULT_DEVICE_URL, include_demo_commands=False, bot_name="Webex Bot", @@ -74,6 +75,7 @@ def __init__(self, self.approved_users = approved_users self.approved_domains = approved_domains self.approved_rooms = approved_rooms + self.approved_admins = approved_admins # Set default help message self.help_message = "Hello! I understand the following commands: \n" self.approval_parameters_check() @@ -147,6 +149,16 @@ def check_user_approved(self, user_email, approved_rooms): log.warning(f"{user_email} is not approved to interact at this time. Ignoring.") return user_approved + def check_user_admin(self, user_email, approved_admins): + is_an_admin = False + + if approved_admins is None: + is_an_admin = False + elif user_email in approved_admins: + is_an_admin = True + + return is_an_admin + def is_user_member_of_room(self, user_email, approved_rooms): is_user_member = False @@ -243,6 +255,10 @@ def process_raw_command(self, raw_message, teams_message, user_email, activity, log.info(f"{user_email} is not allowed to run command: '{command.command_keyword}'") return + if command.admin_command and not self.check_user_admin(user_email=user_email,approved_admins=command.approved_admins): + log.info(f"{user_email} is not allowed to run command: '{command.command_keyword}' as it is an admin command") + return + # Build the reply to the user reply = "" reply_one_to_one = False From d6a516ae433729e6c3be733b6a6354773e939525 Mon Sep 17 00:00:00 2001 From: jafilson Date: Mon, 11 Mar 2024 13:50:59 -0400 Subject: [PATCH 2/2] update --- webex_bot/models/command.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/webex_bot/models/command.py b/webex_bot/models/command.py index 3ce0fa3..b76355f 100644 --- a/webex_bot/models/command.py +++ b/webex_bot/models/command.py @@ -11,7 +11,7 @@ class Command(ABC): def __init__(self, command_keyword=None, chained_commands=[], card=None, help_message=None, delete_previous_message=False, - card_callback_keyword=None, approved_rooms=None, admin_command=None): + card_callback_keyword=None, approved_rooms=None, approved_admins=None, admin_command=None): """ Create a new bot command. @@ -46,6 +46,7 @@ def __init__(self, command_keyword=None, chained_commands=[], card=None, help_me self.approved_rooms = approved_rooms self.chained_commands = chained_commands self.admin_command = admin_command + self.approved_admins = approved_admins # Now, if this card has a Action.Submit action, let's read the callback keyword, # or if it doesnt exist, add it.