12589 fix permission model check

This commit is contained in:
Arthur 2023-06-20 10:16:15 -07:00
parent d0d74c98ad
commit bdfcb939f1
3 changed files with 8 additions and 6 deletions

View File

@ -360,7 +360,6 @@ class NetBoxUserView(generic.ObjectView):
def get_required_permission(self): def get_required_permission(self):
# Need to override as ObjectView will query for NetBoxUser as the model # Need to override as ObjectView will query for NetBoxUser as the model
# but the model we need to check perms for is User # but the model we need to check perms for is User
breakpoint()
return get_permission_for_model(User, 'view') return get_permission_for_model(User, 'view')
def get_extra_context(self, request, instance): def get_extra_context(self, request, instance):

View File

@ -18,10 +18,12 @@ def get_permission_for_model(model, action):
:param model: A model or instance :param model: A model or instance
:param action: View, add, change, or delete (string) :param action: View, add, change, or delete (string)
""" """
# breakpoint()
ct = ContentType.objects.get_for_model(model)
return '{}.{}_{}'.format( return '{}.{}_{}'.format(
model._meta.app_label, ct.app_label,
action, action,
model._meta.model_name ct.model
) )

View File

@ -79,6 +79,7 @@ class ObjectPermissionRequiredMixin(AccessMixin):
if user.has_perms((permission_required, *self.additional_permissions)): if user.has_perms((permission_required, *self.additional_permissions)):
# Update the view's QuerySet to filter only the permitted objects # Update the view's QuerySet to filter only the permitted objects
if isinstance(self.queryset, RestrictedQuerySet):
action = resolve_permission(permission_required)[1] action = resolve_permission(permission_required)[1]
self.queryset = self.queryset.restrict(user, action) self.queryset = self.queryset.restrict(user, action)
@ -94,7 +95,7 @@ class ObjectPermissionRequiredMixin(AccessMixin):
'a base queryset'.format(self.__class__.__name__) 'a base queryset'.format(self.__class__.__name__)
) )
if isinstance(self.queryset, RestrictedQuerySet) and not self.has_permission(): if not self.has_permission():
return self.handle_no_permission() return self.handle_no_permission()
return super().dispatch(request, *args, **kwargs) return super().dispatch(request, *args, **kwargs)