Skip to content

Role based permissions management

Notifications You must be signed in to change notification settings

Tolkam-PHP-Components/permissions

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tolkam/permissions

Role based permissions management.

Documentation

The code is rather self-explanatory and API is intended to be as simple as possible. Please, read the sources/Docblock if you have any questions. See Usage for quick start.

Usage

use Tolkam\Permissions\Grant\Grant;
use Tolkam\Permissions\PermissionsManager;
use Tolkam\Permissions\Resource\CRUDResource;
use Tolkam\Permissions\Role\Role;

$permissionsManager = new PermissionsManager;

$reader = new Role('reader');
$editor = new Role('editor', 'reader');
$owner = new Role('owner', 'editor');

$resource = new CRUDResource('article');
$allResourceActions = $resource->getActions();

$permissionsManager->addResources($resource);
$permissionsManager->addRoles($reader, $editor, $owner);
$permissionsManager->addGrants(
    new Grant('reader', 'article', ['read']),
    new Grant('editor', 'article', ['update']),
    new Grant('owner', 'article', $allResourceActions)
);

$permissionsManager->compile();

foreach ([$reader, $editor, $owner] as $role) {
    foreach ($resource->getActions() as $action) {
        
        $roleName = $role->getName();
        $resourceName = $resource->getName();
        
        if ($permissionsManager->can($roleName, $action, $resourceName)) {
            echo "$roleName can $action $resourceName\n";
        }
        else {
            echo "$roleName can`t $action $resourceName\n";
        }
    }
    echo PHP_EOL;
}

License

Proprietary / Unlicensed 🤷

About

Role based permissions management

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages