Skip to content

Commit c85d394

Browse files
Decoupling apps from ansible_base.rbac
1 parent 964c531 commit c85d394

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

ansible_base/authentication/utils/claims.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
from ansible_base.lib.abstract_models import AbstractOrganization, AbstractTeam, CommonModel
2222
from ansible_base.lib.utils.auth import get_organization_model, get_team_model
2323
from ansible_base.lib.utils.string import is_empty
24-
from ansible_base.rbac.models import DABContentType
25-
from ansible_base.rbac.remote import get_local_resource_prefix
2624

2725
from .trigger_definition import TRIGGER_DEFINITION
2826

@@ -876,7 +874,10 @@ class RoleUserAssignmentsCache:
876874
def __init__(self):
877875
self.cache = {}
878876
# NOTE(cutwater): We may probably execute this query once and cache the query results.
879-
self.content_types = {content_type.model: content_type for content_type in DABContentType.objects.get_for_models(Organization, Team).values()}
877+
self.content_types = {}
878+
if 'ansible_base.rbac' in settings.INSTALLED_APPS:
879+
from ansible_base.rbac.models import DABContentType
880+
self.content_types = {content_type.model: content_type for content_type in DABContentType.objects.get_for_models(Organization, Team).values()}
880881
self.role_definitions = {}
881882

882883
def items(self):
@@ -956,6 +957,11 @@ def cache_existing(self, role_assignments: Iterable[models.Model]) -> None:
956957
- All cached assignments are marked with STATUS_EXISTING status
957958
- Role definitions are also cached separately in self.role_definitions
958959
"""
960+
local_resource_prefixes = ["shared"]
961+
if 'ansible_base.rbac' in settings.INSTALLED_APPS:
962+
from ansible_base.rbac.remote import get_local_resource_prefix
963+
local_resource_prefixes.append(get_local_resource_prefix())
964+
959965
for role_assignment in role_assignments:
960966
# Cache role definition
961967
if (role_definition := self._rd_by_id(role_assignment)) is None:
@@ -965,7 +971,7 @@ def cache_existing(self, role_assignments: Iterable[models.Model]) -> None:
965971
# Skip role assignments that should not be cached
966972
if not (
967973
role_assignment.content_type is None # Global/system roles (e.g., System Auditor)
968-
or role_assignment.content_type.service in [get_local_resource_prefix(), "shared"]
974+
or role_assignment.content_type.service in local_resource_prefixes
969975
): # Local object roles
970976
continue
971977

ansible_base/lib/routers/association_resource_router.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
from rest_framework.response import Response
1818
from rest_framework.viewsets import ViewSetMixin
1919

20-
from ansible_base.rbac.permission_registry import permission_registry
21-
2220
logger = logging.getLogger('ansible_base.lib.routers.association_resource_router')
2321

2422

@@ -119,10 +117,13 @@ def check_parent_object_permissions(self, request, parent_obj: Model) -> None:
119117
will not check "change" permissions to the parent object on POST
120118
this method checks parent change permission, view permission should be handled by filter_queryset
121119
"""
122-
if (request.method not in SAFE_METHODS) and 'ansible_base.rbac' in settings.INSTALLED_APPS and permission_registry.is_registered(parent_obj):
123-
from ansible_base.rbac.policies import check_content_obj_permission
120+
if (request.method not in SAFE_METHODS) and 'ansible_base.rbac' in settings.INSTALLED_APPS:
121+
from ansible_base.rbac.permission_registry import permission_registry
122+
123+
if permission_registry.is_registered(parent_obj):
124+
from ansible_base.rbac.policies import check_content_obj_permission
124125

125-
check_content_obj_permission(request.user, parent_obj)
126+
check_content_obj_permission(request.user, parent_obj)
126127

127128
def get_parent_object(self) -> Model:
128129
"""Modeled mostly after DRF get_object, but for the parent model

0 commit comments

Comments
 (0)