Skip to content

Commit

Permalink
Cache the user model lookup with lru_cache to prevent circular imports.
Browse files Browse the repository at this point in the history
fixes pulp#3957

Signed-off-by: James Tanner <tanner.jc@gmail.com>
  • Loading branch information
jctanner committed Jun 26, 2023
1 parent e5f3598 commit f3245d5
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/3957.bugfix
@@ -0,0 +1 @@
Fixed circular imports caused by get_user_model calls.
4 changes: 2 additions & 2 deletions pulpcore/app/models/role.py
@@ -1,4 +1,4 @@
from django.contrib.auth import get_user_model
from django.conf import settings
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
Expand Down Expand Up @@ -41,7 +41,7 @@ class UserRole(BaseModel):
"""

user = models.ForeignKey(
get_user_model(), related_name="object_roles", on_delete=models.CASCADE
settings.AUTH_USER_MODEL, related_name="object_roles", on_delete=models.CASCADE
)
role = models.ForeignKey(Role, related_name="object_users", on_delete=models.CASCADE)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True)
Expand Down
11 changes: 9 additions & 2 deletions pulpcore/app/role_util.py
@@ -1,19 +1,23 @@
from gettext import gettext as _

from collections import defaultdict
from functools import lru_cache

from django.conf import settings
from django.core.exceptions import BadRequest
from django.db.models import Q, Exists, OuterRef, CharField
from django.db.models.functions import Cast
from django.contrib.auth import get_user_model
from django.contrib.auth import get_user_model as django_get_user_model
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType

from pulpcore.app.models import Group
from pulpcore.app.models.role import GroupRole, Role, UserRole

User = get_user_model()

@lru_cache()
def get_user_model(maxsize=1):
return django_get_user_model()


def assign_role(rolename, entity, obj=None, domain=None):
Expand Down Expand Up @@ -300,6 +304,7 @@ def get_users_with_perms_roles(
include_model_permissions=True,
for_concrete_model=False,
):
User = get_user_model()
qs = User.objects.none()
if with_superusers:
qs |= User.objects.filter(is_superuser=True)
Expand Down Expand Up @@ -338,6 +343,7 @@ def get_users_with_perms_attached_perms(
include_model_permissions=True,
for_concrete_model=False,
):
User = get_user_model()
ctype = ContentType.objects.get_for_model(obj, for_concrete_model=for_concrete_model)
perms = Permission.objects.filter(content_type__pk=ctype.id)
if only_with_perms_in:
Expand Down Expand Up @@ -422,6 +428,7 @@ def get_users_with_perms(
include_model_permissions=True,
for_concrete_model=False,
):
User = get_user_model()
if attach_perms:
res = defaultdict(set)
if "pulpcore.backends.ObjectRolePermissionBackend" in settings.AUTHENTICATION_BACKENDS:
Expand Down

0 comments on commit f3245d5

Please sign in to comment.