8000 GitHub - Fantom409/rbac: Role based access control
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fantom409/rbac

 
 

Yii Role-Based Access Control Library


This package provides RBAC (Role-Based Access Control) library. It is used in Yii Framework but is supposed to be usable separately.

Latest Stable Version Total Downloads Code Coverage Build Status

Install:

composer require yiisoft/rbac

Basic usage:

Сreate instance

$manager = new Manager($storage, new ClassNameRuleFactory());

In the directory config will contain permissions and rules.

Сreate permissions

$manager->addPermission(new Permission('createPost'));
$manager->addPermission(new Permission('readPost'));
$manager->addPermission(new Permission('deletePost'));

After executing this code, this configuration will be saved in ../config/items.php

Create roles

$manager->addRole(new Role('author'));
$manager->addRole(new Role('reader'));

Attach permissions to roles

$manager->addChild(
    $storage->getRoleByName('reader'),
    $storage->getPermissionByName('readPost')
);

$manager->addChild(
    $storage->getRoleByName('author'),
    $storage->getPermissionByName('createPost')
);

$manager->addChild(
    $storage->getRoleByName('author'),
    $storage->getRoleByName('reader')
);

Assign role to user

$userId = 100;
$manager->assign($storage->getRoleByName('author'), $userId);

After executing this code, this configuration will be saved in ../config/assignments.php

Check permissions

if ($manager->userHasPermission($userId, 'createPost')) {
    echo 'author has permission createPost';
}

Usage rules

$manager->addRule(new ActionRule());
$manager->addPermission(
    (new Permission('viewList'))->withRuleName('action_rule')
);

The role will also support the rules.

Rule example

class ActionRule extends Rule
{
    public function __construct()
    {
        parent::__construct('action_rule');
    }

    public function execute(string $userId, Item $item, array $parameters = []): bool
    {
        return isset($parameters['action']) && $parameters['action'] === 'home';
    }
}

Check permissions with rule

$anotherUserId = 103;
if (!$manager->userHasPermission($anotherUserId, 'viewList', ['action' => 'home'])) {
    echo 'reader not has permission index';
}

Storage:

Storage Description
PhpStorage PHP file storage

About

Role based access control

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%
0