Skip to content

Commit d8a03a4

Browse files
author
Tycho Hob
committed
refactor: Add RBAC admin cache constants
1 parent 09e7325 commit d8a03a4

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

openedx_authz/engine/enforcer.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ def configure_enforcer_auto_save_and_load(cls):
151151

152152
if auto_load_policy_interval > 0:
153153
cls.configure_enforcer_auto_loading(auto_load_policy_interval)
154-
else:
155-
logger.warning("CASBIN_AUTO_LOAD_POLICY_INTERVAL is not set or zero; auto-load is disabled.")
156154

157155
cls.configure_enforcer_auto_save(auto_save_policy)
158156

openedx_authz/engine/matcher.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
from openedx_authz.api.data import ContentLibraryData, ScopeData, UserData
77
from openedx_authz.rest_api.utils import get_user_by_username_or_email
88

9+
RBAC_ADMIN_CACHE_KEY_FMT = "rbac_is_admin_or_superuser_{username}"
10+
RBAC_ADMIN_CACHE_TIMEOUT_SECS = 2
11+
912
User = get_user_model()
1013

1114

@@ -36,8 +39,8 @@ def is_admin_or_superuser_check(request_user: str, request_action: str, request_
3639
if not isinstance(scope, ContentLibraryData):
3740
return False
3841

39-
# TODO: Make this key format a constant
40-
is_allowed = cache.get(f"rbac_is_admin_or_superuser_{username}")
42+
cache_key = RBAC_ADMIN_CACHE_KEY_FMT.format(username=username)
43+
is_allowed = cache.get(cache_key)
4144

4245
if is_allowed is not None:
4346
return is_allowed
@@ -50,6 +53,6 @@ def is_admin_or_superuser_check(request_user: str, request_action: str, request_
5053
is_allowed = user.is_staff or user.is_superuser
5154

5255
# TODO: Make this cache timeout configurable
53-
cache.set(f"rbac_is_admin_or_superuser_{username}", is_allowed, 2)
56+
cache.set(cache_key, is_allowed, RBAC_ADMIN_CACHE_TIMEOUT_SECS)
5457

5558
return is_allowed

0 commit comments

Comments
 (0)