diff --git a/README.md b/README.md index 3ac5ffb..6ac3278 100644 --- a/README.md +++ b/README.md @@ -64,6 +64,32 @@ def hello(request): pass ``` +## Configuration + +### `CASBIN_MODEL` +A string containing the file location of your casbin model. + +### `CASBIN_LOG_ENABLED` +If `True`, enables logging. `False` by default. + +### `CASBIN_ADAPTER` +A string containing the adapter import path. Defaults to the django adapter shipped with this package: `casbin_adapter.adapter.Adapter` + +### `CASBIN_ADAPTER_ARGS` +A tuple of arguments to be passed into the constructor of the adapter specified +in `CASBIN_ADAPTER`. Refer to adapters to see available arguments. + +E.g. if you wish to use the file adapter +set the adapter to `casbin.persist.adapters.FileAdapter` and use +`CASBIN_ADAPTER_ARGS = ('path/to/policy_file.csv',)` + +### `CASBIN_WATCHER` +Watcher instance to be set as the watcher on the enforcer instance. + +### `CASBIN_ROLE_MANAGER` +Role manager instance to be set as the role manager on the enforcer instance. + + ### Getting Help - [PyCasbin](https://github.com/casbin/pycasbin) diff --git a/casbin_adapter/enforcer.py b/casbin_adapter/enforcer.py index 65cfcbc..4e63f50 100644 --- a/casbin_adapter/enforcer.py +++ b/casbin_adapter/enforcer.py @@ -1,3 +1,4 @@ +import logging from django.conf import settings from django.db import connection from django.db.utils import OperationalError, ProgrammingError @@ -5,6 +6,10 @@ from casbin import Enforcer from .adapter import Adapter +from .utils import import_class + +logger = logging.getLogger(__name__) + class ProxyEnforcer(Enforcer): @@ -13,15 +18,22 @@ class ProxyEnforcer(Enforcer): def __init__(self, *args, **kwargs): if self._initialized: super().__init__(*args, **kwargs) + else: + logger.info('Deferring casbin enforcer initialisation until django is ready') def _load(self): if self._initialized == False: + logger.info('Performing deferred casbin enforcer initialisation') self._initialized = True model = getattr(settings, 'CASBIN_MODEL') enable_log = getattr(settings, 'CASBIN_LOG_ENABLED', False) - adapter = Adapter() + adapter_loc = getattr(settings, 'CASBIN_ADAPTER', 'casbin_adapter.adapter.Adapter') + adapter_args = getattr(settings, 'CASBIN_ADAPTER_ARGS', tuple()) + Adapter = import_class(adapter_loc) + adapter = Adapter(*adapter_args) super().__init__(model, adapter, enable_log) + logger.debug('Casbin enforcer initialised') watcher = getattr(settings, 'CASBIN_WATCHER', None) if watcher: diff --git a/casbin_adapter/utils.py b/casbin_adapter/utils.py new file mode 100644 index 0000000..97d6d35 --- /dev/null +++ b/casbin_adapter/utils.py @@ -0,0 +1,11 @@ +import importlib + +def import_class(name): + """Import class from string + e.g. `package.module.ClassToImport` returns the `ClasToImport` class""" + components = name.split('.') + module_name = '.'.join(components[:-1]) + class_name = components[-1] + module = importlib.import_module(module_name) + class_ = getattr(module, class_name) + return class_