Skip to content

Commit 97d5317

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

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-10
lines changed

ansible_base/authentication/utils/claims.py

Lines changed: 16 additions & 5 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

@@ -32,6 +30,9 @@
3230
User = get_user_model()
3331

3432

33+
is_rbac_installed = 'ansible_base.rbac' in settings.INSTALLED_APPS
34+
35+
3536
class TriggerResult(Enum):
3637
ALLOW = auto()
3738
DENY = auto()
@@ -722,7 +723,7 @@ def reconcile_user_claims(cls, user: AbstractUser, authenticator_user: Authentic
722723

723724
claims = getattr(user, 'claims', authenticator_user.claims)
724725

725-
if 'ansible_base.rbac' in settings.INSTALLED_APPS:
726+
if is_rbac_installed:
726727
cls(claims, user, authenticator_user).manage_permissions()
727728
else:
728729
logger.info(_("Skipping user claims with RBAC roles, because RBAC app is not installed"))
@@ -876,7 +877,11 @@ class RoleUserAssignmentsCache:
876877
def __init__(self):
877878
self.cache = {}
878879
# 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()}
880+
self.content_types = {}
881+
if is_rbac_installed:
882+
from ansible_base.rbac.models import DABContentType
883+
884+
self.content_types = {content_type.model: content_type for content_type in DABContentType.objects.get_for_models(Organization, Team).values()}
880885
self.role_definitions = {}
881886

882887
def items(self):
@@ -956,6 +961,12 @@ def cache_existing(self, role_assignments: Iterable[models.Model]) -> None:
956961
- All cached assignments are marked with STATUS_EXISTING status
957962
- Role definitions are also cached separately in self.role_definitions
958963
"""
964+
local_resource_prefixes = ["shared"]
965+
if is_rbac_installed:
966+
from ansible_base.rbac.remote import get_local_resource_prefix
967+
968+
local_resource_prefixes.append(get_local_resource_prefix())
969+
959970
for role_assignment in role_assignments:
960971
# Cache role definition
961972
if (role_definition := self._rd_by_id(role_assignment)) is None:
@@ -965,7 +976,7 @@ def cache_existing(self, role_assignments: Iterable[models.Model]) -> None:
965976
# Skip role assignments that should not be cached
966977
if not (
967978
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"]
979+
or role_assignment.content_type.service in local_resource_prefixes
969980
): # Local object roles
970981
continue
971982

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)