Skip to content

Commit e6a7bdc

Browse files
committed
docs: updated README
1 parent 5ffe939 commit e6a7bdc

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

README.md

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pip install casbin-postgresql-watcher
1616
```
1717

1818
## Basic Usage Example
19-
### With Flask-authz
19+
2020
```python
2121
from flask_authz import CasbinEnforcer
2222
from postgresql_watcher import PostgresqlWatcher
@@ -25,23 +25,51 @@ from casbin.persist.adapters import FileAdapter
2525

2626
casbin_enforcer = CasbinEnforcer(app, adapter)
2727
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)
2929
casbin_enforcer.set_watcher(watcher)
30-
```
3130

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+
```
3341

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
3543

36-
### With Flask-authz
3744
```python
3845
from flask_authz import CasbinEnforcer
3946
from postgresql_watcher import PostgresqlWatcher
4047
from flask import Flask
4148
from casbin.persist.adapters import FileAdapter
4249

4350
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)
4652
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+
...
4775
```

0 commit comments

Comments
 (0)