Skip to content

Commit 47132c6

Browse files
refactor: address PR reviews
1 parent 564ffa7 commit 47132c6

4 files changed

Lines changed: 9 additions & 12 deletions

File tree

openedx_authz/engine/config/model.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,4 @@ e = some(where (p.eft == allow)) && !some(where (p.eft == deny))
9292
# 1. Subject must have role in scope OR global role
9393
# 2. Scope must match pattern
9494
# 3. Action must match OR inherit via action grouping
95-
m = custom_check(r.sub, r.act, r.scope) || (g(r.sub, p.sub, r.scope) || g(r.sub, p.sub, "*")) && keyMatch(r.scope, p.scope) && (r.act == p.act || g2(p.act, r.act))
95+
m = is_staff_or_superuser(r.sub, r.act, r.scope) || (g(r.sub, p.sub, r.scope) || g(r.sub, p.sub, "*")) && keyMatch(r.scope, p.scope) && (r.act == p.act || g2(p.act, r.act))

openedx_authz/engine/enforcer.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from django.conf import settings
2323

2424
from openedx_authz.engine.adapter import ExtendedAdapter
25-
25+
from openedx_authz.engine.matcher import is_admin_or_superuser_check
2626

2727
def libraries_v2_enabled() -> bool:
2828
"""Dummy toggle that is always enabled."""
@@ -182,9 +182,6 @@ def _initialize_enforcer(cls) -> SyncedEnforcer:
182182
Returns:
183183
SyncedEnforcer: Configured Casbin enforcer with adapter and auto-sync
184184
"""
185-
# Avoid circular import
186-
from openedx_authz.engine.matcher import check_custom_conditions # pylint: disable=import-outside-toplevel
187-
188185
db_alias = getattr(settings, "CASBIN_DB_ALIAS", "default")
189186

190187
try:
@@ -198,6 +195,6 @@ def _initialize_enforcer(cls) -> SyncedEnforcer:
198195

199196
adapter = ExtendedAdapter()
200197
enforcer = SyncedEnforcer(settings.CASBIN_MODEL, adapter)
201-
enforcer.add_function("custom_check", check_custom_conditions)
198+
enforcer.add_function("is_staff_or_superuser", is_admin_or_superuser_check)
202199

203200
return enforcer

openedx_authz/engine/matcher.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
User = get_user_model()
99

1010

11-
def check_custom_conditions(request_user: str, request_action: str, request_scope: str) -> bool: # pylint: disable=unused-argument
11+
def is_admin_or_superuser_check(request_user: str, request_action: str, request_scope: str) -> bool: # pylint: disable=unused-argument
1212
"""
1313
Evaluates custom, non-role-based conditions for authorization checks.
1414

openedx_authz/tests/test_enforcement.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from openedx_authz import ROOT_DIRECTORY
1818
from openedx_authz.constants import roles
19-
from openedx_authz.engine.matcher import check_custom_conditions
19+
from openedx_authz.engine.matcher import is_admin_or_superuser_check
2020
from openedx_authz.tests.test_utils import (
2121
make_action_key,
2222
make_library_key,
@@ -71,7 +71,7 @@ def setUpClass(cls) -> None:
7171
raise FileNotFoundError(f"Model file not found: {model_file}")
7272

7373
cls.enforcer = casbin.Enforcer(model_file)
74-
cls.enforcer.add_function("custom_check", check_custom_conditions)
74+
cls.enforcer.add_function("is_staff_or_superuser", is_admin_or_superuser_check)
7575

7676
def _load_policy(self, policy: list[str]) -> None:
7777
"""
@@ -586,10 +586,10 @@ def test_wildcard_library_access(self, scope: str, expected_result: bool):
586586
@ddt
587587
class StaffSuperuserAccessTests(CasbinEnforcementTestCase):
588588
"""
589-
Tests for staff and superuser automatic permission grants via custom_check.
589+
Tests for staff and superuser automatic permission grants via is_staff_or_superuser.
590590
591591
This test class verifies that staff members and superusers are automatically
592-
granted access to ContentLibrary scopes through the check_custom_conditions function,
592+
granted access to ContentLibrary scopes through the is_admin_or_superuser_check function,
593593
without requiring explicit role assignments.
594594
"""
595595

@@ -646,7 +646,7 @@ def test_staff_superuser_guaranteed_permissions(self, subject: str, action: str,
646646
- Staff users automatically have access to all library scopes without role assignments
647647
- Superusers automatically have access to all library scopes without role assignments
648648
- Regular users require explicit role assignments to access libraries
649-
- Access is granted through the custom_check matcher function
649+
- Access is granted through the is_staff_or_superuser matcher function
650650
651651
Expected result:
652652
- Staff and superusers can perform any action on any ContentLibrary scope

0 commit comments

Comments
 (0)