PHP code example of mathsgod / light-rbac
1. Go to this page and download the library: Download mathsgod/light-rbac library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mathsgod / light-rbac example snippets
$rbac = new \Light\Rbac\Rbac();
$rbac->addRole('admin');
$rbac->addUser('John Doe', ['admin']);
$admin = $rbac->getRole('admin');
$role = $rbac->addRole('admin');
$role->addPermission('post:read');
$role->addPermission('post:write');
$user = $rbac->addUser('John Doe', ['admin']);
if ($user->can('post:read')) {
echo 'John Doe can read posts.';
}
$role = $rbac->addRole('admin');
$role->addPermission('*');
if ($role->can('post:read')) {
echo 'Admin can read posts.';
}
$role = $rbac->addRole('admin');
$role->addPermission('post:*');
if ($role->can('post:read')) {
echo 'Admin can read posts.';
}
$user = $rbac->addUser('John Doe', ['admin']);
if ($user->hasRole('admin')) {
echo 'John Doe is an admin.';
}
$admin = $rbac->addRole('admin');
$admin->addChild('editor');
$rbac->getRole('editor')->addPermission('post:read');
if($admin->can('post:read')) {
echo 'Admin can read posts.';
}