feat(core): only display navigation menu item if use can view model
!35 fixes #114
This commit is contained in:
@ -12,6 +12,8 @@ from core.views.common import ChangeView, IndexView
|
||||
|
||||
|
||||
class IndexView(IndexView):
|
||||
|
||||
model = Organization
|
||||
permission_required = [
|
||||
'access.view_organization'
|
||||
]
|
||||
|
@ -5,6 +5,8 @@ from app.urls import urlpatterns
|
||||
from django.conf import settings
|
||||
from django.urls import URLPattern, URLResolver
|
||||
|
||||
from access.models import Organization
|
||||
|
||||
from settings.models.user_settings import UserSettings
|
||||
|
||||
|
||||
@ -88,7 +90,7 @@ def nav_items(context) -> list(dict()):
|
||||
is_active: {bool} if this link is the active URL
|
||||
|
||||
Returns:
|
||||
_type_: _description_
|
||||
list: Items user has view access to
|
||||
"""
|
||||
|
||||
dnav = []
|
||||
@ -142,11 +144,37 @@ def nav_items(context) -> list(dict()):
|
||||
|
||||
name = str(pattern.name)
|
||||
|
||||
nav_items = nav_items + [ {
|
||||
'name': name,
|
||||
'url': url,
|
||||
'is_active': is_active
|
||||
} ]
|
||||
if hasattr(pattern.callback.view_class, 'permission_required'):
|
||||
|
||||
permissions_required = pattern.callback.view_class.permission_required
|
||||
|
||||
user_has_perm = False
|
||||
|
||||
if type(permissions_required) is list:
|
||||
|
||||
user_has_perm = context.user.has_perms(permissions_required)
|
||||
|
||||
else:
|
||||
|
||||
user_has_perm = context.user.has_perm(permissions_required)
|
||||
|
||||
if hasattr(pattern.callback.view_class, 'model'):
|
||||
|
||||
if pattern.callback.view_class.model is Organization and context.user.is_authenticated:
|
||||
|
||||
organizations = Organization.objects.filter(manager = context.user)
|
||||
|
||||
if len(organizations) > 0:
|
||||
|
||||
user_has_perm = True
|
||||
|
||||
if user_has_perm:
|
||||
|
||||
nav_items = nav_items + [ {
|
||||
'name': name,
|
||||
'url': url,
|
||||
'is_active': is_active
|
||||
} ]
|
||||
|
||||
if len(nav_items) > 0:
|
||||
|
||||
|
@ -49,4 +49,12 @@ class DeleteView(OrganizationPermission, generic.DeleteView):
|
||||
|
||||
class IndexView(View, generic.ListView):
|
||||
|
||||
pass
|
||||
model = None
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
|
||||
if not self.model:
|
||||
|
||||
raise Exception('Model is required for view')
|
||||
|
||||
super().__init__(**kwargs)
|
||||
|
@ -6,7 +6,7 @@ from itam.views import device, device_type, software, software_category, softwar
|
||||
app_name = "ITIM"
|
||||
urlpatterns = [
|
||||
|
||||
path("clusters", device.IndexView.as_view(), name="Clusters"),
|
||||
path("services", device.IndexView.as_view(), name="Services"),
|
||||
# path("clusters", device.IndexView.as_view(), name="Clusters"),
|
||||
# path("services", device.IndexView.as_view(), name="Services"),
|
||||
|
||||
]
|
||||
|
@ -4,6 +4,6 @@ from .views import ProjectIndex
|
||||
|
||||
app_name = "Project Management"
|
||||
urlpatterns = [
|
||||
path('', ProjectIndex.as_view(), name='Projects'),
|
||||
# path('', ProjectIndex.as_view(), name='Projects'),
|
||||
|
||||
]
|
||||
|
Reference in New Issue
Block a user