<?php
use Laminas\Permissions\Rbac\Role;
use Laminas\Permissions\Rbac\Rbac;
define('R', 'read');
define('W', 'write');
// create roles
$roleAdmin = new Role('Admin');
$roleUser = new Role('User');
$roleAnonymous = new Role('Anonymous');
// permissions
$roleAdmin->addPermission(W);
$roleUser->addPermission(R);
// add child roles / hierarchy
$roleUser->addChild($roleAnonymous);
$roleAdmin->addChild($roleUser);
$rbac = new Rbac();
$rbac->addRole($roleUser);
$rbac->addRole($roleAdmin);
$rbac->addRole($roleAnonymous);
if ($rbac->isGranted($roleAdmin, R)) {
echo 'Admin has access and read permission from User role.' . PHP_EOL;
}
if ($rbac->isGranted($roleUser, W)) {
echo 'User has access and write permission.' . PHP_EOL;
}