From e6a7bdc31599d1f4c7a863f914d1138de73611fe Mon Sep 17 00:00:00 2001 From: Thore Bartholomaeus Date: Mon, 8 Jul 2024 23:05:03 +0200 Subject: [PATCH] docs: updated README --- README.md | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index a78cc00..acd58b2 100644 --- a/README.md +++ b/README.md @@ -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 @@ -25,15 +25,22 @@ 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 @@ -41,7 +48,28 @@ 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) +... ```