From d0b1facfd8181b61f6eca98b7092544e167bcdb4 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 31 Aug 2023 10:51:16 -0400 Subject: [PATCH 1/3] Closes #13638: Add optional staff_only attribute to MenuItem --- docs/plugins/development/navigation.md | 13 +++++++------ netbox/netbox/navigation/__init__.py | 1 + netbox/utilities/templatetags/navigation.py | 13 ++++++++----- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/docs/plugins/development/navigation.md b/docs/plugins/development/navigation.md index 3e7762184e2..4239e873361 100644 --- a/docs/plugins/development/navigation.md +++ b/docs/plugins/development/navigation.md @@ -64,12 +64,13 @@ item1 = PluginMenuItem( A `PluginMenuItem` has the following attributes: -| Attribute | Required | Description | -|---------------|----------|------------------------------------------------------| -| `link` | Yes | Name of the URL path to which this menu item links | -| `link_text` | Yes | The text presented to the user | -| `permissions` | - | A list of permissions required to display this link | -| `buttons` | - | An iterable of PluginMenuButton instances to include | +| Attribute | Required | Description | +|---------------|----------|----------------------------------------------------------------------------------------------------------| +| `link` | Yes | Name of the URL path to which this menu item links | +| `link_text` | Yes | The text presented to the user | +| `permissions` | - | A list of permissions required to display this link | +| `staff_only` | - | Display only for users who have `is_staff` set to true (any specified permissions will also be required) | +| `buttons` | - | An iterable of PluginMenuButton instances to include | ## Menu Buttons diff --git a/netbox/netbox/navigation/__init__.py b/netbox/netbox/navigation/__init__.py index a05b1c495cc..4c7190bbb65 100644 --- a/netbox/netbox/navigation/__init__.py +++ b/netbox/netbox/navigation/__init__.py @@ -34,6 +34,7 @@ class MenuItem: link: str link_text: str permissions: Optional[Sequence[str]] = () + staff_only: Optional[bool] = False buttons: Optional[Sequence[MenuItemButton]] = () diff --git a/netbox/utilities/templatetags/navigation.py b/netbox/utilities/templatetags/navigation.py index 4a229e95215..7534d703432 100644 --- a/netbox/utilities/templatetags/navigation.py +++ b/netbox/utilities/templatetags/navigation.py @@ -26,11 +26,14 @@ def nav(context: Context) -> Dict: for group in menu.groups: items = [] for item in group.items: - if user.has_perms(item.permissions): - buttons = [ - button for button in item.buttons if user.has_perms(button.permissions) - ] - items.append((item, buttons)) + if not user.has_perms(item.permissions): + continue + if item.staff_only and not user.is_staff: + continue + buttons = [ + button for button in item.buttons if user.has_perms(button.permissions) + ] + items.append((item, buttons)) if items: groups.append((group, items)) if groups: From b23f7b76547297ea8d960648f02a6fd6adfd7295 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 31 Aug 2023 11:07:16 -0400 Subject: [PATCH 2/3] Add missing file --- netbox/extras/plugins/navigation.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/netbox/extras/plugins/navigation.py b/netbox/extras/plugins/navigation.py index 288a78512f6..2075c97b62c 100644 --- a/netbox/extras/plugins/navigation.py +++ b/netbox/extras/plugins/navigation.py @@ -36,9 +36,10 @@ class PluginMenuItem: permissions = [] buttons = [] - def __init__(self, link, link_text, permissions=None, buttons=None): + def __init__(self, link, link_text, staff_only=False, permissions=None, buttons=None): self.link = link self.link_text = link_text + self.staff_only = staff_only if permissions is not None: if type(permissions) not in (list, tuple): raise TypeError("Permissions must be passed as a tuple or list.") From b7decfaa7e34b83d044f68107dedcb1caa3ed864 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 31 Aug 2023 11:23:00 -0400 Subject: [PATCH 3/3] Add release note --- docs/plugins/development/navigation.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/plugins/development/navigation.md b/docs/plugins/development/navigation.md index 4239e873361..8d758014700 100644 --- a/docs/plugins/development/navigation.md +++ b/docs/plugins/development/navigation.md @@ -72,6 +72,8 @@ A `PluginMenuItem` has the following attributes: | `staff_only` | - | Display only for users who have `is_staff` set to true (any specified permissions will also be required) | | `buttons` | - | An iterable of PluginMenuButton instances to include | +!!! info "The `staff_only` attribute was introduced in NetBox v3.6.1." + ## Menu Buttons Each menu item can include a set of buttons. These can be handy for providing shortcuts related to the menu item. For instance, most items in NetBox's navigation menu include buttons to create and import new objects.