First, I want to say that I find the utter lack of online source code demos for PHPRBAC to be troubling. But now this github issue will be one of the primary example code snippets on the Internet, so I'm hoping you can get this to work by showing me the error of my ways!
The following code mostly works, except for two failures which you'll see below. However, it takes an incredibly long time (30-40 seconds on an Intel i7). I want to know 1) how to fix the failures, 2) how to increase the performance dramatically and 3) any other tips you can give me.
Thank you so much! Time is of the essence.
Success: User 1 has the 'intent to leave' permission.
Success: User 1 has the 'profile-blocks-1' permission.
Failure! User 1 has the 'profile-blocks-2' permission.
Success! 'survey items-3' is an unknown/invalid permission.
Success: User 2 has the 'intent to leave' permission.
Failure! User 2 has the 'profile-blocks-1' permission.
Success: User 2 has the 'profile-blocks-n' permission.
Success! 'survey items-3' is an unknown/invalid permission.
<?php
// PHP-RBAC Proof of Concept
require_once 'PhpRbac/autoload.php';
header('Content-Type: text/plain');
$rbac = new PhpRbac\Rbac();
$commonPerms = [
'intent-to-leave',
'referrals-public-view',
'referrals-teams-view',
];
$role1Perms = [
'access level-individual_aggregate'.
'risk-indicator',
'profile-blocks-1',
'profile-blocks-4',
'profile-blocks-n',
'profile-items-2',
'profile-items-n',
'survey-blocks-2',
'survey-blocks-4',
'survey-blocks-n',
'survey-items-2',
'survey-items-n',
'notes-public-view',
'notes-teams-view',
'contacts-public-view',
'contacts-teams-view',
'booking-public-view',
'booking-teams-view',
];
$role2Perms = [
'access-level_aggregate',
'profile-blocks-2',
'profile-blocks-4',
'profile-blocks-n',
'profile-items-2',
'profile-items-n',
'survey-blocks-2',
'survey-blocks-4',
'survey-blocks-n',
'survey-items-2',
'survey-items-n',
'notes-public-create',
'notes-teams-create',
'contacts-public-view',
'contacts-private-create',
'contacts-teams-view',
'booking-public-create',
'booking-teams-create',
'referrals-private-create',
];
$role3Perms = [
'access-level_individual_aggregate',
'risk-indicator',
'referrals-public-create',
'referrals-private-create',
'referrals-teams-create',
'receive-referrals',
];
$commonRoleId = $rbac->Roles->add('common_role', 'Common permissions for all users');
foreach ($commonPerms as $info) {
$permId = $rbac->Permissions->add($info, 'Default description', $commonRoleId);
$rbac->Roles->assign('common_role', $permId);
}
$role1Id = $rbac->Roles->add('role1', 'Role 1', $commonRoleId);
foreach ($role1Perms as $permName) {
$rbac->Permissions->add($permName, 'Default description', $role1Id);
$rbac->Roles->assign('role1', $permName);
}
$role2Id = $rbac->Roles->add('role2', 'Role 2', $commonRoleId);
foreach ($role2Perms as $permName) {
$rbac->Permissions->add($permName, 'Default description', $role2Id);
$rbac->Roles->assign('role2', $permName);
}
// Create User
$users = [
[
'id' => 1,
'username' => 'admin',
'roles' => [
'role1'
]
],
[
'id' => 2,
'username' => 'editor',
'roles' => [
'common_role',
'role2'
]
],
];
// Assign the proper roles.
foreach ($users as $user) {
if (!empty($user['roles']) && is_array($user['roles'])) {
foreach ($user['roles'] as $role) {
$rbac->Users->assign($role, $user['id']);
}
}
}
$tests = [
$users[0]['id'] => [
'intent to leave' => true,
'profile-blocks-1' => true,
'profile-blocks-2' => false,
'survey items-3' => false,
],
$users[1]['id'] => [
'intent to leave' => true,
'profile-blocks-1' => false,
'profile-blocks-n' => true,
'survey items-3' => false,
],
];
// Validate if a user has a particular permission.
foreach ($tests as $userId => $test) {
foreach ($test as $permission => $hasPermission) {
$permissionVerb = $hasPermission ? 'has' : 'doesn\'t have';
$noPermissionVerb = $hasPermission ? 'doesn\'t have' : 'has';
try {
if ($rbac->check($permission, $userId) === $hasPermission) {
echo "Success: User {$userId} $permissionVerb the '{$permission}' permission.\n";
} else {
echo "Failure! User {$userId} $noPermissionVerb the '{$permission}' permission.\n";
}
}
catch(Exception $e) {
$status = $hasPermission ? 'Failure' : 'Success';
echo "$status! '{$permission}' is an unknown/invalid permission.\n";
}
}
}