21
21
from ansible_base .lib .abstract_models import AbstractOrganization , AbstractTeam , CommonModel
22
22
from ansible_base .lib .utils .auth import get_organization_model , get_team_model
23
23
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
26
24
27
25
from .trigger_definition import TRIGGER_DEFINITION
28
26
32
30
User = get_user_model ()
33
31
34
32
33
+ is_rbac_installed = 'ansible_base.rbac' in settings .INSTALLED_APPS
34
+
35
+
35
36
class TriggerResult (Enum ):
36
37
ALLOW = auto ()
37
38
DENY = auto ()
@@ -722,7 +723,7 @@ def reconcile_user_claims(cls, user: AbstractUser, authenticator_user: Authentic
722
723
723
724
claims = getattr (user , 'claims' , authenticator_user .claims )
724
725
725
- if 'ansible_base.rbac' in settings . INSTALLED_APPS :
726
+ if is_rbac_installed :
726
727
cls (claims , user , authenticator_user ).manage_permissions ()
727
728
else :
728
729
logger .info (_ ("Skipping user claims with RBAC roles, because RBAC app is not installed" ))
@@ -876,7 +877,11 @@ class RoleUserAssignmentsCache:
876
877
def __init__ (self ):
877
878
self .cache = {}
878
879
# 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 ()}
880
885
self .role_definitions = {}
881
886
882
887
def items (self ):
@@ -956,6 +961,12 @@ def cache_existing(self, role_assignments: Iterable[models.Model]) -> None:
956
961
- All cached assignments are marked with STATUS_EXISTING status
957
962
- Role definitions are also cached separately in self.role_definitions
958
963
"""
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
+
959
970
for role_assignment in role_assignments :
960
971
# Cache role definition
961
972
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:
965
976
# Skip role assignments that should not be cached
966
977
if not (
967
978
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
969
980
): # Local object roles
970
981
continue
971
982
0 commit comments