1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Implement ObjectPermissionManager

This commit is contained in:
Jeremy Stretch
2020-05-11 14:32:10 -04:00
parent 06aca2e1d5
commit 63f842c7db
5 changed files with 85 additions and 54 deletions

View File

@ -56,21 +56,12 @@ class ObjectPermissionBackend(ModelBackend):
if model._meta.model_name != model_name:
raise ValueError(f"Invalid permission {perm} for model {model}")
# Retrieve user's permissions for this model
# This can probably be cached
obj_permissions = ObjectPermission.objects.filter(
Q(users=user_obj) | Q(groups__user=user_obj),
model=ContentType.objects.get_for_model(obj),
**{f'can_{action}': True}
)
for perm in obj_permissions:
# Attempt to retrieve the model from the database using the
# attributes defined in the ObjectPermission. If we have a
# match, assert that the user has permission.
if model.objects.filter(pk=obj.pk, **perm.attrs).exists():
return True
# Attempt to retrieve the model from the database using the
# attributes defined in the ObjectPermission. If we have a
# match, assert that the user has permission.
attrs = ObjectPermission.objects.get_attr_constraints(user_obj, obj, action)
if model.objects.filter(pk=obj.pk, **attrs).exists():
return True
class RemoteUserBackend(ViewExemptModelBackend, RemoteUserBackend_):