Skip to content

Commit edef9a3

Browse files
docs: calling update_callback fun if set and updated docs (#27)
* feat: improve docs * fix: calling update_callback if set * fix: remove none value for timeout * fix: typo in readme
1 parent 11466e0 commit edef9a3

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

README.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,50 @@ See [PostgresQL documentation](https://www.postgresql.org/docs/current/libpq-con
7373
watcher = PostgresqlWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD, dbname=DBNAME, sslmode="verify_full", sslcert=SSLCERT, sslrootcert=SSLROOTCERT, sslkey=SSLKEY)
7474
...
7575
```
76+
77+
78+
## Django setup with casbin django orm adapter
79+
80+
Enforcer and Watcher setup
81+
```
82+
# settings.py
83+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
84+
85+
INSTALLED_APPS += [
86+
'casbin_adapter.apps.CasbinAdapterConfig',
87+
]
88+
89+
CASBIN_MODEL = os.path.join(BASE_DIR, 'casbin.conf')
90+
91+
from postgresql_watcher.watcher import PostgresqlWatcher
92+
from casbin_adapter.enforcer import enforcer
93+
94+
watcher = PostgresqlWatcher(host=BANK_CONNECT_APIS_PG_HOST_URL, port=BANK_CONNECT_APIS_PG_PORT,
95+
user=BANK_CONNECT_APIS_PG_USER, password=BANK_CONNECT_APIS_PG_PASSWORD, dbname=BANK_CONNECT_APIS_PG_DBNAME)
96+
97+
def update_enforcer():
98+
print("before loading policy", enforcer)
99+
enforcer.load_policy()
100+
101+
watcher.set_update_callback(update_enforcer)
102+
CASBIN_WATCHER = watcher
103+
```
104+
105+
Usage of enforcer
106+
107+
```
108+
#views.py or any other file
109+
from casbin_adapter.enforcer import enforcer
110+
111+
roles = enforcer.get_filtered_named_grouping_policy("g", 1, str(member_id))
112+
```
113+
114+
### Reload Casbin enforcer
115+
In current setup enforcer does not automatically refresh in memory data, we can call watcher.should_reload() before every data access from enforcer.
116+
```
117+
from setting import watcher
118+
watcher.should_reload()
119+
```
120+
If there are any changes in db this call will refresh in memory data from database
121+
122+
For automatic reloading of data, parent process need to poll child process for messages and call should_reload function if there is any message in pipe between child and parent process

0 commit comments

Comments
 (0)