PostgreSQL adapter for Casbin

Clone this repo:

Branches

Tags

  1. 6e9080e feat: add the code and CI by D0000M · 9 months ago master v1.0.0
  2. 8d93f1b Initial commit by Yang Luo · 9 months ago

jcasbin-postgres-watcher

GitHub Actions License Javadoc Maven Central Release Discord

jCasbin PostgreSQL Watcher is a PostgreSQL watcher for jCasbin.

Installation

For Maven

<dependency>
   <groupId>org.casbin</groupId>
   <artifactId>jcasbin-postgres-watcher</artifactId>
   <version>1.0.0</version>
</dependency>

Simple Example

if you have two casbin instances A and B

A: Producer

// Initialize PostgreSQL Watcher
String channel = "casbin_channel";
JCasbinPostgresWatcher watcher = new JCasbinPostgresWatcher(
    "jdbc:postgresql://localhost:5432/your_db",
    "postgres",
    "your_password",
    channel
);
// Support for advanced configuration with WatcherConfig
// WatcherConfig config = new WatcherConfig();
// config.setChannel(channel);
// config.setVerbose(true);
// config.setLocalId("instance-1");
// JCasbinPostgresWatcher watcher = new JCasbinPostgresWatcher(url, user, password, config);

Enforcer enforcer = new SyncedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
enforcer.setWatcher(watcher);

// The following code is not necessary and generally does not need to be written unless you understand what you want to do
/*
Runnable updateCallback = () -> {
    // Custom behavior
};
watcher.setUpdateCallback(updateCallback);
*/

// Modify policy, it will notify B
enforcer.addPolicy(...);

// Using WatcherEx specific methods for fine-grained policy updates
// Add a policy
enforcer.addPolicy(...);
watcher.updateForAddPolicy(...);

B: Consumer

// Initialize PostgreSQL Watcher with same channel
String channel = "casbin_channel";
JCasbinPostgresWatcher watcher = new JCasbinPostgresWatcher(
    "jdbc:postgresql://localhost:5432/your_db",
    "postgres",
    "your_password",
    channel
);

Enforcer enforcer = new SyncedEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv");
enforcer.setWatcher(watcher);
// B set watcher and subscribe to the same channel, then it will receive the notification of A, and then call LoadPolicy to reload policy

Getting Help

License

This project is under Apache 2.0 License. See the LICENSE file for the full license text.