casbin/phalcon-permission

An authorization library that supports access control models like ACL, RBAC, ABAC in Phalcon.

dev-master 2020-09-18 02:16 UTC

This package is auto-updated.

Last update: 2024-10-26 15:47:39 UTC


README

An authorization library that supports access control models like ACL, RBAC, ABAC in Phalcon.

Installing

Require this package in the composer.json of your phalcon project. This will download the package.

$ composer require

Configure database connection:

'database' => [
    'adapter' => 'mysql',
    'host' => '127.0.0.1',
    'username' => 'root',
    'password' => '',
    'dbname' => 'db-name',
    'charset' => 'utf8',
],

Then phalcon-permission migrations must run to create the needed tables. For this, you need to have installed the Phalcon Dev Tools.

use Phalcon Dev Tools to migrate the migrations, run the command:

$ phalcon migration run --migrations=vendor/casbin/phalcon-permission/migrations

This will create a new table named casbin_rules.

Usage

Quick start

use Easyswolle\Permission\Casbin;
use Easyswolle\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:

Gets all roles:

$casbin->getAllRoles(); // ['writer', 'reader']

Gets all the authorization rules in the policy.:

$casbin->getPolicy();

Gets the roles that a user has.

$casbin->getRolesForUser('eve'); // ['writer']

Gets the users that has a role.

$casbin->getUsersForRole('writer'); // ['eve']

Determines whether a user has a role.

$casbin->hasRoleForUser('eve', 'writer'); // true or false

Adds a role for a user.

$casbin->addRoleForUser('eve', 'writer');

Adds a permission for a user or role.

// to user
$casbin->addPermissionForUser('eve', 'articles', 'read');
// to role
$casbin->addPermissionForUser('writer', 'articles','edit');

Deletes a role for a user.

$casbin->deleteRoleForUser('eve', 'writer');

Deletes all roles for a user.

$casbin->deleteRolesForUser('eve');

Deletes a role.

$casbin->deleteRole('writer');

Deletes a permission.

$casbin->deletePermission('articles', 'read'); // returns false if the permission does not exist (aka not affected).

Deletes a permission for a user or role.

$casbin->deletePermissionForUser('eve', 'articles', 'read');

Deletes permissions for a user or role.

// to user
$casbin->deletePermissionsForUser('eve');
// to role
$casbin->deletePermissionsForUser('writer');

Gets permissions for a user or role.

$casbin->getPermissionsForUser('eve'); // return array

Determines whether a user has a permission.

$casbin->hasPermissionForUser('eve', 'articles', 'read');  // true or false

See Casbin API for more APIs.