Skip to content

Commit

Permalink
Merge pull request #10 from actioncy/proxy-enforcer-improvements
Browse files Browse the repository at this point in the history
Proxy enforcer improvements
  • Loading branch information
hsluoyz authored Jul 1, 2020
2 parents c8c47c9 + 3f8527e commit e53d75b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 13 additions & 1 deletion casbin_adapter/enforcer.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import logging
from django.conf import settings
from django.db import connection
from django.db.utils import OperationalError, ProgrammingError

from casbin import Enforcer

from .adapter import Adapter
from .utils import import_class

logger = logging.getLogger(__name__)



class ProxyEnforcer(Enforcer):
Expand All @@ -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:
Expand Down
11 changes: 11 additions & 0 deletions casbin_adapter/utils.py
Original file line number Diff line number Diff line change
@@ -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_

0 comments on commit e53d75b

Please sign in to comment.