Skip to content

Commit

Permalink
Merge pull request #183 from smk4664/permission_based_help
Browse files Browse the repository at this point in the history
Add option to limit Help prompt based on user Access Grants.
  • Loading branch information
smk4664 authored Mar 11, 2023
2 parents 7054d18 + d3901f6 commit 814a869
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions changes/182.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add option to limit Help prompt based on user Access Grants.
1 change: 1 addition & 0 deletions development/nautobot_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,6 @@
"enable_mattermost": True,
"mattermost_api_token": os.environ.get("MATTERMOST_API_TOKEN"),
"mattermost_url": os.environ.get("MATTERMOST_URL"),
"restrict_help": True,
},
}
1 change: 1 addition & 0 deletions docs/admin/install/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ The plugin behavior can be controlled with the following list of settings:
| Configuration Setting | Description | Mandatory? | Default |
| ---------------------------- | ----------- | ---------- | ------- |
| `delete_input_on_submission` | After prompting the user for additional inputs, delete the input prompt from the chat history | No | `False` |
| `restrict_help` | Only show Help prompt for users based on their Access Grants | No | `False` |

## Grant Access to the Chatbot

Expand Down
1 change: 1 addition & 0 deletions nautobot_chatops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class NautobotChatOpsConfig(PluginConfig):
# sending all messages as an ephemeral message, meaning only the person interacting with the bot will see the
# responses.
"send_all_messages_private": False,
"restrict_help": False,
}

max_version = "1.999"
Expand Down
16 changes: 11 additions & 5 deletions nautobot_chatops/workers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@
import shlex
import pkg_resources

from nautobot_chatops.choices import CommandStatusChoices
from nautobot_chatops.models import CommandLog
from django.conf import settings
from django.db.models import Q

from nautobot_chatops.choices import AccessGrantTypeChoices, CommandStatusChoices
from nautobot_chatops.models import AccessGrant
from nautobot_chatops.utils import create_command_log
from nautobot_chatops.metrics import request_command_cntr, command_histogram

Expand Down Expand Up @@ -267,6 +270,10 @@ def second_subcommand(dispatcher, arg_1):
if subcommand == "help" or not subcommand:
message = f"I know the following `{dispatcher.command_prefix}{command}` subcommands:\n"
for subcmd, entry in registry[command].get("subcommands", {}).items():
if settings.PLUGINS_CONFIG["nautobot_chatops"].get("restrict_help") and not AccessGrant.objects.filter(
Q(command="*") | Q(command=command, subcommand="*") | Q(command=command, subcommand=subcmd),
).filter(grant_type=AccessGrantTypeChoices.TYPE_USER).filter(Q(value="*") | Q(value=context["user_id"])):
continue
message += (
f"- `{dispatcher.command_prefix}{command} {subcmd} "
f"{' '.join(f'[{param}]' for param in entry['params'])}`\t{entry['doc']}\n"
Expand Down Expand Up @@ -298,15 +305,14 @@ def second_subcommand(dispatcher, arg_1):
result = registry[command]["subcommands"][subcommand]["worker"](dispatcher, *params)
if result is not False:
command_log.runtime = datetime.now(timezone.utc) - command_log.start_time
status = result
details = ""
# For backward compatibility, a return value of True is considered a "success" status.
if result is True:
status = CommandStatusChoices.STATUS_SUCCEEDED
details = ""
elif isinstance(result, (list, tuple)):
status, details = result[:2]
else:
status = result
details = ""

if status not in CommandStatusChoices.values():
if not details:
Expand Down

0 comments on commit 814a869

Please sign in to comment.