mirror of
https://github.com/inventree/InvenTree
synced 2024-08-30 18:33:04 +00:00
Add global context 'roles'
- Access via template e.g. {% if roles.part.view %} - Always True if the user is a superuser
This commit is contained in:
parent
3f59ce3f93
commit
8b2189daca
@ -17,3 +17,36 @@ def status_codes(request):
|
||||
'BuildStatus': BuildStatus,
|
||||
'StockStatus': StockStatus,
|
||||
}
|
||||
|
||||
|
||||
def user_roles(request):
|
||||
"""
|
||||
Return a map of the current roles assigned to the user.
|
||||
|
||||
Roles are denoted by their simple names, and then the permission type.
|
||||
|
||||
Permissions can be access as follows:
|
||||
|
||||
- roles.part.view
|
||||
- roles.build.delete
|
||||
|
||||
Each value will return a boolean True / False
|
||||
"""
|
||||
|
||||
user = request.user
|
||||
|
||||
roles = {}
|
||||
|
||||
for group in user.groups.all():
|
||||
for rule in group.rule_sets.all():
|
||||
roles[rule.name] = {
|
||||
'view': rule.can_view or user.is_superuser,
|
||||
'add': rule.can_add or user.is_superuser,
|
||||
'change': rule.can_change or user.is_superuser,
|
||||
'delete': rule.can_delete or user.is_superuser,
|
||||
}
|
||||
|
||||
print("Roles:")
|
||||
print(roles)
|
||||
|
||||
return {'roles': roles}
|
||||
|
@ -210,6 +210,7 @@ TEMPLATES = [
|
||||
'django.contrib.auth.context_processors.auth',
|
||||
'django.contrib.messages.context_processors.messages',
|
||||
'InvenTree.context.status_codes',
|
||||
'InvenTree.context.user_roles',
|
||||
],
|
||||
},
|
||||
},
|
||||
|
Loading…
Reference in New Issue
Block a user