Skip to content

Commit e53d75b

Browse files
authored
Merge pull request #10 from actioncy/proxy-enforcer-improvements
Proxy enforcer improvements
2 parents c8c47c9 + 3f8527e commit e53d75b

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,32 @@ def hello(request):
6464
pass
6565
```
6666

67+
## Configuration
68+
69+
### `CASBIN_MODEL`
70+
A string containing the file location of your casbin model.
71+
72+
### `CASBIN_LOG_ENABLED`
73+
If `True`, enables logging. `False` by default.
74+
75+
### `CASBIN_ADAPTER`
76+
A string containing the adapter import path. Defaults to the django adapter shipped with this package: `casbin_adapter.adapter.Adapter`
77+
78+
### `CASBIN_ADAPTER_ARGS`
79+
A tuple of arguments to be passed into the constructor of the adapter specified
80+
in `CASBIN_ADAPTER`. Refer to adapters to see available arguments.
81+
82+
E.g. if you wish to use the file adapter
83+
set the adapter to `casbin.persist.adapters.FileAdapter` and use
84+
`CASBIN_ADAPTER_ARGS = ('path/to/policy_file.csv',)`
85+
86+
### `CASBIN_WATCHER`
87+
Watcher instance to be set as the watcher on the enforcer instance.
88+
89+
### `CASBIN_ROLE_MANAGER`
90+
Role manager instance to be set as the role manager on the enforcer instance.
91+
92+
6793
### Getting Help
6894

6995
- [PyCasbin](https://github.com/casbin/pycasbin)

casbin_adapter/enforcer.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import logging
12
from django.conf import settings
23
from django.db import connection
34
from django.db.utils import OperationalError, ProgrammingError
45

56
from casbin import Enforcer
67

78
from .adapter import Adapter
9+
from .utils import import_class
10+
11+
logger = logging.getLogger(__name__)
12+
813

914

1015
class ProxyEnforcer(Enforcer):
@@ -13,15 +18,22 @@ class ProxyEnforcer(Enforcer):
1318
def __init__(self, *args, **kwargs):
1419
if self._initialized:
1520
super().__init__(*args, **kwargs)
21+
else:
22+
logger.info('Deferring casbin enforcer initialisation until django is ready')
1623

1724
def _load(self):
1825
if self._initialized == False:
26+
logger.info('Performing deferred casbin enforcer initialisation')
1927
self._initialized = True
2028
model = getattr(settings, 'CASBIN_MODEL')
2129
enable_log = getattr(settings, 'CASBIN_LOG_ENABLED', False)
22-
adapter = Adapter()
30+
adapter_loc = getattr(settings, 'CASBIN_ADAPTER', 'casbin_adapter.adapter.Adapter')
31+
adapter_args = getattr(settings, 'CASBIN_ADAPTER_ARGS', tuple())
32+
Adapter = import_class(adapter_loc)
33+
adapter = Adapter(*adapter_args)
2334

2435
super().__init__(model, adapter, enable_log)
36+
logger.debug('Casbin enforcer initialised')
2537

2638
watcher = getattr(settings, 'CASBIN_WATCHER', None)
2739
if watcher:

casbin_adapter/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import importlib
2+
3+
def import_class(name):
4+
"""Import class from string
5+
e.g. `package.module.ClassToImport` returns the `ClasToImport` class"""
6+
components = name.split('.')
7+
module_name = '.'.join(components[:-1])
8+
class_name = components[-1]
9+
module = importlib.import_module(module_name)
10+
class_ = getattr(module, class_name)
11+
return class_

0 commit comments

Comments
 (0)