casbin/swoole-redis-watcher

Redis watcher for PHP-Casbin in swoole.

v1.0.0 2021-11-22 11:14 UTC

This package is auto-updated.

Last update: 2024-03-22 16:12:55 UTC


README

Latest Stable Version Total Downloads License

Redis watcher for PHP-Casbin in Swoole , Casbin is a powerful and efficient open-source access control library.

Installation

Via Composer.

composer require casbin/swoole-redis-watcher

Usage

require dirname(__FILE__) . '/../vendor/autoload.php';

use Casbin\Enforcer;
use CasbinWatcher\SwooleRedis\Watcher;

Co::set(['hook_flags'=> SWOOLE_HOOK_ALL]); 

$http = new Swoole\Http\Server('0.0.0.0', 9501);

$http->on('WorkerStart', function ($server, $worker_id) {
    global $enforcer;

    // Initialize the Watcher.
    $watcher = new Watcher([
        'host' => '127.0.0.1',
        'password' => '',
        'port' => 6379,
        'database' => 0,
    ]);

    // Initialize the Enforcer.
    $enforcer = new Enforcer("path/to/model.conf", "path/to/policy.csv");

    // Set the watcher for the enforcer.
    $enforcer->setWatcher($watcher);

    // By default, the watcher's callback is automatically set to the
    // $enforcer->loadPolicy() in the setWatcher() call.
    // We can change it by explicitly setting a callback.
    $watcher->setUpdateCallback(function () use ($enforcer) {
        // Now should reload all policies.
        // $enforcer->loadPolicy();
    });

};

$http->on('Request', function ($request, $response) {
    // ...
});

$http->start();

Getting Help

License

This project is licensed under the Apache 2.0 license.