$roles = ['user', 'admin'];
$permissions = [
'user' => 'view',
'admin' => 'edit',
];
$hierarchy = [
'user' => [],
'admin' => ['user'],
];
$rbac = new Rbac();
$rbac->setCreateMissingRoles(TRUE); // not work in any case
foreach ($roles as $role) {
$rbacRole = new Role($role);
$rbacRole->addPermission($permissions[$role]);
// add roles hierarchy recursive
foreach ($hierarchy[$role] as $parent) {
$rbacRole->addParent(new Role($parent));
}
$rbac->addRole($rbacRole);
}
$userRole = new Role('user');
var_dump($rbac->isGranted($userRole, 'view'));
We have several roles, permissions & roles hierarchy. First we load all them into RBAC class. Second, we try to take current user role & check if he can do something. But the code above give me No role with name "user" could be found. Why?
add child |user| to role |admin|
role |user| try to |view| - granted
role |user| try to |edit| - denied
role |admin| try to |view| - granted
role |admin| try to |edit| - granted