Skip to content
Snippets Groups Projects
Verified Commit f56f70a5 authored by Jonathan Weth's avatar Jonathan Weth :keyboard:
Browse files

Optimize has_person_group_object_perm

parent ea8239e3
No related branches found
No related tags found
1 merge request!90Performance optimizations
Pipeline #3656 passed
from typing import Union from typing import Union
from django.contrib.auth.models import User from django.contrib.auth.models import User, Permission
from django.contrib.contenttypes.models import ContentType
from guardian.models import UserObjectPermission
from guardian.shortcuts import get_objects_for_user from guardian.shortcuts import get_objects_for_user
from rules import predicate from rules import predicate
...@@ -100,12 +102,14 @@ def has_person_group_object_perm(perm: str): ...@@ -100,12 +102,14 @@ def has_person_group_object_perm(perm: str):
""" """
name = f"has_person_group_object_perm:{perm}" name = f"has_person_group_object_perm:{perm}"
ct = ContentType.objects.get_for_model(Group)
permissions = Permission.objects.filter(content_type=ct, codename=perm)
@predicate(name) @predicate(name)
def fn(user: User, obj: Person) -> bool: def fn(user: User, obj: Person) -> bool:
for group in obj.member_of.all(): groups = obj.member_of.all()
if check_object_permission(user, perm, group): qs = UserObjectPermission.objects.filter(object_pk__in=groups.values_list("pk", flat=True), content_type=ct, user=user, permission__in=permissions)
return True return qs.exists()
return False
return fn return fn
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment