mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-23 07:56:44 -06:00
Merge actions and action_perms into a single mapping
This commit is contained in:
parent
7efbfabc0b
commit
41cc5e46af
@ -9,13 +9,13 @@ __all__ = (
|
|||||||
|
|
||||||
|
|
||||||
class ActionsMixin:
|
class ActionsMixin:
|
||||||
actions = ('add', 'import', 'export', 'bulk_edit', 'bulk_delete')
|
actions = {
|
||||||
action_perms = defaultdict(set, **{
|
|
||||||
'add': {'add'},
|
'add': {'add'},
|
||||||
'import': {'add'},
|
'import': {'add'},
|
||||||
|
'export': set(),
|
||||||
'bulk_edit': {'change'},
|
'bulk_edit': {'change'},
|
||||||
'bulk_delete': {'delete'},
|
'bulk_delete': {'delete'},
|
||||||
})
|
}
|
||||||
|
|
||||||
def get_permitted_actions(self, user, model=None):
|
def get_permitted_actions(self, user, model=None):
|
||||||
"""
|
"""
|
||||||
@ -23,11 +23,34 @@ class ActionsMixin:
|
|||||||
"""
|
"""
|
||||||
model = model or self.queryset.model
|
model = model or self.queryset.model
|
||||||
|
|
||||||
return [
|
# TODO: Remove backward compatibility in Netbox v4.0
|
||||||
action for action in self.actions if user.has_perms([
|
# Determine how permissions are being mapped to actions for the view
|
||||||
get_permission_for_model(model, name) for name in self.action_perms[action]
|
if type(self.actions) is dict:
|
||||||
])
|
# New actions format (3.7+)
|
||||||
]
|
permissions_map = self.actions
|
||||||
|
elif hasattr(self, 'action_perms'):
|
||||||
|
# Backward compatibility for <3.7
|
||||||
|
permissions_map = self.action_perms
|
||||||
|
else:
|
||||||
|
# actions is still defined as a list or tuple (<3.7) but no custom mapping is defined; use the old
|
||||||
|
# default mapping
|
||||||
|
permissions_map = {
|
||||||
|
'add': {'add'},
|
||||||
|
'import': {'add'},
|
||||||
|
'bulk_edit': {'change'},
|
||||||
|
'bulk_delete': {'delete'},
|
||||||
|
}
|
||||||
|
|
||||||
|
# Resolve required permissions for each action
|
||||||
|
permitted_actions = []
|
||||||
|
for action in self.actions:
|
||||||
|
required_permissions = [
|
||||||
|
get_permission_for_model(model, name) for name in permissions_map.get(action, set())
|
||||||
|
]
|
||||||
|
if not required_permissions or user.has_perms(required_permissions):
|
||||||
|
permitted_actions.append(action)
|
||||||
|
|
||||||
|
return permitted_actions
|
||||||
|
|
||||||
|
|
||||||
class TableMixin:
|
class TableMixin:
|
||||||
|
Loading…
Reference in New Issue
Block a user