Skip to content

Commit

Permalink
docs: updated README
Browse files Browse the repository at this point in the history
  • Loading branch information
trbtm committed Jul 8, 2024
1 parent a05f25a commit bd0be42
Showing 1 changed file with 36 additions and 8 deletions.
44 changes: 36 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pip install casbin-postgresql-watcher
```

## Basic Usage Example
### With Flask-authz

```python
from flask_authz import CasbinEnforcer
from postgresql_watcher import PostgresqlWatcher
Expand All @@ -25,23 +25,51 @@ from casbin.persist.adapters import FileAdapter

casbin_enforcer = CasbinEnforcer(app, adapter)
watcher = PostgresqlWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD, dbname=DBNAME)
watcher.set_update_callback(casbin_enforcer.e.load_policy)
watcher.set_update_callback(casbin_enforcer.adapter.load_policy)
casbin_enforcer.set_watcher(watcher)
```

## Basic Usage Example With SSL Enabled
# Call should_reload before every call of enforce to make sure
# the policy is update to date
watcher.should_reload()
if casbin_enforcer.enforce("alice", "data1", "read"):
# permit alice to read data1
pass
else:
# deny the request, show an error
pass
```

See [PostgresQL documentation](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS) for full details of SSL parameters.
alternatively, if you need more control

### With Flask-authz
```python
from flask_authz import CasbinEnforcer
from postgresql_watcher import PostgresqlWatcher
from flask import Flask
from casbin.persist.adapters import FileAdapter

casbin_enforcer = CasbinEnforcer(app, adapter)
watcher = PostgresqlWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD, dbname=DBNAME, sslmode="verify_full", sslcert=SSLCERT, sslrootcert=SSLROOTCERT, sslkey=SSLKEY)
watcher.set_update_callback(casbin_enforcer.e.load_policy)
watcher = PostgresqlWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD, dbname=DBNAME)
casbin_enforcer.set_watcher(watcher)

# Call should_reload before every call of enforce to make sure
# the policy is update to date
if watcher.should_reload():
adapter.load_policy()

if casbin_enforcer.enforce("alice", "data1", "read"):
# permit alice to read data1
pass
else:
# deny the request, show an error
pass
```

## Basic Usage Example With SSL Enabled

See [PostgresQL documentation](https://www.postgresql.org/docs/current/libpq-connect.html#LIBPQ-PARAMKEYWORDS) for full details of SSL parameters.

```python
...
watcher = PostgresqlWatcher(host=HOST, port=PORT, user=USER, password=PASSWORD, dbname=DBNAME, sslmode="verify_full", sslcert=SSLCERT, sslrootcert=SSLROOTCERT, sslkey=SSLKEY)
...
```

0 comments on commit bd0be42

Please sign in to comment.