mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-14 01:41:22 -06:00
* Closes #13638: Add optional staff_only attribute to MenuItem * Add missing file * Add release note
This commit is contained in:
parent
2544e2bf18
commit
f962fb3b53
@ -64,12 +64,15 @@ 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 |
|
||||
|
||||
!!! info "The `staff_only` attribute was introduced in NetBox v3.6.1."
|
||||
|
||||
## Menu Buttons
|
||||
|
||||
|
@ -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.")
|
||||
|
@ -34,6 +34,7 @@ class MenuItem:
|
||||
link: str
|
||||
link_text: str
|
||||
permissions: Optional[Sequence[str]] = ()
|
||||
staff_only: Optional[bool] = False
|
||||
buttons: Optional[Sequence[MenuItemButton]] = ()
|
||||
|
||||
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user