@@ -16,7 +16,7 @@ pip install casbin-postgresql-watcher
16
16
```
17
17
18
18
## Basic Usage Example
19
- ### With Flask-authz
19
+
20
20
``` python
21
21
from flask_authz import CasbinEnforcer
22
22
from postgresql_watcher import PostgresqlWatcher
@@ -25,23 +25,51 @@ from casbin.persist.adapters import FileAdapter
25
25
26
26
casbin_enforcer = CasbinEnforcer(app, adapter)
27
27
watcher = PostgresqlWatcher(host = HOST , port = PORT , user = USER , password = PASSWORD , dbname = DBNAME )
28
- watcher.set_update_callback(casbin_enforcer.e .load_policy)
28
+ watcher.set_update_callback(casbin_enforcer.adapter .load_policy)
29
29
casbin_enforcer.set_watcher(watcher)
30
- ```
31
30
32
- ## Basic Usage Example With SSL Enabled
31
+ # Call should_reload before every call of enforce to make sure
32
+ # the policy is update to date
33
+ watcher.should_reload()
34
+ if casbin_enforcer.enforce(" alice" , " data1" , " read" ):
35
+ # permit alice to read data1
36
+ pass
37
+ else :
38
+ # deny the request, show an error
39
+ pass
40
+ ```
33
41
34
- See [ PostgresQL documentation ] ( https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS ) for full details of SSL parameters.
42
+ alternatively, if you need more control
35
43
36
- ### With Flask-authz
37
44
``` python
38
45
from flask_authz import CasbinEnforcer
39
46
from postgresql_watcher import PostgresqlWatcher
40
47
from flask import Flask
41
48
from casbin.persist.adapters import FileAdapter
42
49
43
50
casbin_enforcer = CasbinEnforcer(app, adapter)
44
- watcher = PostgresqlWatcher(host = HOST , port = PORT , user = USER , password = PASSWORD , dbname = DBNAME , sslmode = " verify_full" , sslcert = SSLCERT , sslrootcert = SSLROOTCERT , sslkey = SSLKEY )
45
- watcher.set_update_callback(casbin_enforcer.e.load_policy)
51
+ watcher = PostgresqlWatcher(host = HOST , port = PORT , user = USER , password = PASSWORD , dbname = DBNAME )
46
52
casbin_enforcer.set_watcher(watcher)
53
+
54
+ # Call should_reload before every call of enforce to make sure
55
+ # the policy is update to date
56
+ if watcher.should_reload():
57
+ adapter.load_policy()
58
+
59
+ if casbin_enforcer.enforce(" alice" , " data1" , " read" ):
60
+ # permit alice to read data1
61
+ pass
62
+ else :
63
+ # deny the request, show an error
64
+ pass
65
+ ```
66
+
67
+ ## Basic Usage Example With SSL Enabled
68
+
69
+ See [ PostgresQL documentation] ( https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS ) for full details of SSL parameters.
70
+
71
+ ``` python
72
+ ...
73
+ watcher = PostgresqlWatcher(host = HOST , port = PORT , user = USER , password = PASSWORD , dbname = DBNAME , sslmode = " verify_full" , sslcert = SSLCERT , sslrootcert = SSLROOTCERT , sslkey = SSLKEY )
74
+ ...
47
75
```
0 commit comments