11"""
22Core authorization enforcer for Open edX AuthZ system.
33
4- Provides a Casbin FastEnforcer instance with extended adapter for database policy
5- storage and Redis watcher for distributed policy synchronization.
4+ Provides a Casbin SyncedEnforcer instance with extended adapter for database policy
5+ storage and automatic policy synchronization.
66
77Components:
8- - Enforcer: Main FastEnforcer instance for policy evaluation
8+ - Enforcer: Main SyncedEnforcer instance for policy evaluation
99 - Adapter: ExtendedAdapter for filtered database policy loading
10- - Watcher: Redis-based watcher for real-time policy updates
1110
1211Usage:
1312 from openedx_authz.engine.enforcer import AuthzEnforcer
1413 allowed = enforcer.enforce(user, resource, action)
1514
16- Requires `CASBIN_MODEL` setting and Redis configuration for watcher functionality .
15+ Requires `CASBIN_MODEL` setting.
1716"""
1817
1918import logging
2322from django .conf import settings
2423
2524from openedx_authz .engine .adapter import ExtendedAdapter
26- from openedx_authz .engine .watcher import Watcher
2725
2826logger = logging .getLogger (__name__ )
2927
@@ -32,7 +30,7 @@ class AuthzEnforcer:
3230 """Singleton class to manage the Casbin SyncedEnforcer instance.
3331
3432 Ensures a single enforcer instance is created safely and configured with the
35- ExtendedAdapter and Redis watcher for policy management and synchronization.
33+ ExtendedAdapter for policy management and automatic synchronization.
3634
3735 There are two main use cases for this class:
3836
@@ -75,13 +73,12 @@ def _initialize_enforcer() -> SyncedEnforcer:
7573 """
7674 Create and configure the Casbin SyncedEnforcer instance.
7775
78- This method initializes the FastEnforcer with the ExtendedAdapter
79- for database policy storage and sets up the Redis watcher for real-time
80- policy synchronization if the Watcher is available. It also initializes
81- the enforcer with the specified database alias from settings.
76+ This method initializes the SyncedEnforcer with the ExtendedAdapter
77+ for database policy storage and automatic policy synchronization.
78+ It also initializes the enforcer with the specified database alias from settings.
8279
8380 Returns:
84- SyncedEnforcer: Configured Casbin enforcer with adapter and watcher
81+ SyncedEnforcer: Configured Casbin enforcer with adapter and auto-sync
8582 """
8683 db_alias = getattr (settings , "CASBIN_DB_ALIAS" , "default" )
8784
@@ -99,14 +96,4 @@ def _initialize_enforcer() -> SyncedEnforcer:
9996 enforcer .start_auto_load_policy (settings .CASBIN_AUTO_LOAD_POLICY_INTERVAL )
10097 enforcer .enable_auto_save (True )
10198
102- if not Watcher :
103- logger .warning ("Redis configuration not completed successfully. Watcher is disabled." )
104- return enforcer
105-
106- try :
107- enforcer .set_watcher (Watcher )
108- logger .info ("Watcher successfully set on Casbin enforcer" )
109- except Exception as e : # pylint: disable=broad-exception-caught
110- logger .error (f"Failed to set watcher on Casbin enforcer: { e } " )
111-
11299 return enforcer
0 commit comments