casbin / easyswoole-permission
An authorization library that supports access control models like ACL, RBAC, ABAC in EasySwoole.
Requires
- php: ^7.2
- ext-swoole: >=4.4
- casbin/casbin: ~3.0
- easyswoole/easyswoole: ~3.3|~3.4
- easyswoole/orm: ^1.4
Requires (Dev)
- easyswoole/easyswoole: ~3.3|~3.4
- easyswoole/phpunit: ^1.0
README
easyswoole-permission is an authorization library for the easyswoole framework.
It's based on Casbin, an authorization library that supports access control models like ACL
, RBAC
, ABAC
.
All you need to learn to use Casbin
first.
Installation
Require this package in the composer.json
of your easyswoole project. This will download the package.
$ composer install
Or in the root directory of your easyswoole application, you can use the following composer command to install this package directly .
$ composer require casbin/easyswoole-permission
Usage
Database settings
add mysql configuration to dev.php
:
/*################ MYSQL CONFIG ##################*/ 'MYSQL' => [ 'host' => '127.0.0.1', 'port' => 3306, 'user' => 'root', 'password' => 'root', 'database' => 'easyswoole', 'timeout' => 5, 'charset' => 'utf8mb4', ]
add mysql configuration to EasySwooleEvent.php
:
use EasySwoole\ORM\Db\Connection; use EasySwoole\ORM\DbManager; public static function initialize() { ... $config = new \EasySwoole\ORM\Db\Config(Config::getInstance()->getConf('MYSQL')); DbManager::getInstance()->addConnection(new Connection($config)); }
Create corresponding data table
Before using it, you need to create a table named casbin_rules
for Casbin to store the policy.
Take mysql as an example:
CREATE TABLE if not exists `casbin_rules` ( `id` BigInt(20) unsigned NOT NULL AUTO_INCREMENT, `ptype` varchar(255) DEFAULT NULL, `v0` varchar(255) DEFAULT NULL, `v1` varchar(255) DEFAULT NULL, `v2` varchar(255) DEFAULT NULL, `v3` varchar(255) DEFAULT NULL, `v4` varchar(255) DEFAULT NULL, `v5` varchar(255) DEFAULT NULL, `create_time` timestamp NULL DEFAULT NULL, `update_time` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Quick start
Then you can start like this:
use EasySwoole\Permission\Casbin; use EasySwoole\Permission\Config; $config = new Config(); $casbin = new Casbin($config); // adds permissions to a user $casbin->addPermissionForUser('eve', 'articles', 'read'); // adds a role for a user. $casbin->addRoleForUser('eve', 'writer'); // adds permissions to a rule $casbin->addPolicy('writer', 'articles', 'edit');
You can check if a user has a permission like this:
// to check if a user has permission if ($casbin->enforce('eve', 'articles', 'edit')) { // permit eve to edit articles } else { // deny the request, show an error }
Using Enforcer Api
It provides a very rich api to facilitate various operations on the Policy:
First create an instance of the enforcer class, and the following operations are based on this instance:
$config = new Config(); $casbin = new Casbin($config); $enforcer = $casbin->enforcer();
Gets all roles:
$enforcer->getAllRoles(); // ['writer', 'reader']
Gets all the authorization rules in the policy.:
$enforcer->getPolicy();
Gets the roles that a user has.
$enforcer->getRolesForUser('eve'); // ['writer']
Gets the users that has a role.
$enforcer->getUsersForRole('writer'); // ['eve']
Determines whether a user has a role.
$enforcer->hasRoleForUser('eve', 'writer'); // true or false
Adds a role for a user.
$enforcer->addRoleForUser('eve', 'writer');
Adds a permission for a user or role.
// to user $enforcer->addPermissionForUser('eve', 'articles', 'read'); // to role $enforcer->addPermissionForUser('writer', 'articles','edit');
Deletes a role for a user.
$enforcer->deleteRoleForUser('eve', 'writer');
Deletes all roles for a user.
$enforcer->deleteRolesForUser('eve');
Deletes a role.
$enforcer->deleteRole('writer');
Deletes a permission.
$enforcer->deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).
Deletes a permission for a user or role.
$enforcer->deletePermissionForUser('eve', 'articles', 'read');
Deletes permissions for a user or role.
// to user $enforcer->deletePermissionsForUser('eve'); // to role $enforcer->deletePermissionsForUser('writer');
Gets permissions for a user or role.
$enforcer->getPermissionsForUser('eve'); // return array
Determines whether a user has a permission.
$enforcer->hasPermissionForUser('eve', 'articles', 'read'); // true or false
See Casbin API for more APIs.
Thinks
Casbin in Easyswoole. You can find the full documentation of Casbin on the website.
License
This project is licensed under the Apache 2.0 license.