1. Go to this page and download the library: Download rotexsoft/versatile-acl 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/ */
rotexsoft / versatile-acl example snippets
use VersatileAcl\VersatileAcl;
$groupsVaclObj = new VersatileAcl();
$groupsVaclObj
->addEntity('admin')
// | Group Name | Resource | Action | Allowed |
// |---------------------|----------|---------|---------|
// | admin | all | all | yes |
// Permission below will allow an entity whose ID
// is 'admin' to perform any action on any resource
// in an application
->addPermission(
'admin',
\VersatileAcl\GenericPermission::getAllActionsIdentifier(),
\VersatileAcl\GenericPermission::getAllResourcesIdentifier(),
true
);
$groupsVaclObj
->addEntity('comments-moderators')
// | Group Name | Resource | Action | Allowed |
// |---------------------|----------|---------|---------|
// | comments-moderators | comment | approve | yes |
// Permission below allows an entity whose ID is
// 'comments-moderators' to approve comments made
// on a blog post
->addPermission('comments-moderators', 'approve', 'comment', true)
// | Group Name | Resource | Action | Allowed |
// |---------------------|----------|---------|---------|
// | comments-moderators | comment | delete | yes |
// Permission below allows an entity whose ID is
// 'comments-moderators' to delete comments made
// on a blog post
->addPermission('comments-moderators', 'delete', 'comment', true);
$groupsVaclObj
->addEntity('posts-moderators')
// | Group Name | Resource | Action | Allowed |
// |---------------------|----------|---------|---------|
// | posts-moderators | post | approve | yes |
// Permission below allows an entity whose ID is
// 'posts-moderators' to approve any blog post
// created in your application
->addPermission('posts-moderators','approve', 'post', true)
// | Group Name | Resource | Action | Allowed |
// |---------------------|----------|---------|---------|
// | posts-moderators | post | delete | yes |
// Permission below allows an entity whose ID is
// 'posts-moderators' to delete any blog post
// created in your application
->addPermission('posts-moderators','delete', 'post', true);
// We will create an owners group entity that will
// contain permissions for the comments-owners and
// the posts-owners groups
$groupsVaclObj
->addEntity('owners')
// | Group Name | Resource | Action | Allowed |
// |---------------------|----------|---------|---------|
// | comments-owners | comment | all | yes |
// Permission below allows an entity to both
// approve and delete comments made on blog
// posts created by the entity whose ID is
// 'owners'
->addPermission(
'owners',
\VersatileAcl\GenericPermission::getAllActionsIdentifier(),
'comment',
true,
function(array $userRecord=[], array $commentRecord=[]){
return isset($userRecord['id'])
&& isset($commentRecord['commenter_id'])
&& $userRecord['id'] === $commentRecord['commenter_id'];
}
)
// | Group Name | Resource | Action | Allowed |
// |---------------------|----------|---------|---------|
// | posts-owners | post | all | yes |
// Permission below allows an entity to both
// approve and delete blog posts created by
// the entity whose ID is 'owners'
->addPermission(
'owners',
\VersatileAcl\GenericPermission::getAllActionsIdentifier(),
'post',
true,
function(array $userRecord=[], array $blogPostRecord=[]){
return isset($userRecord['id'])
&& isset($blogPostRecord['creators_id'])
&& $userRecord['id'] === $blogPostRecord['creators_id'];
}
);
$usersVaclObj = new VersatileAcl();
$usersVaclObj->addEntity('frankwhite')
->addEntity('ginawhite')
->addEntity('johndoe')
->addEntity('janedoe')
->addEntity('jackbauer')
->addEntity('jillbauer');
// add 'frankwhite' to the admin group
$usersVaclObj->getEntity('frankwhite')
->addParent(
$groupsVaclObj->getEntity('admin')
);
// add 'ginawhite' to the comments-moderators group
$usersVaclObj->getEntity('ginawhite')
->addParent(
$groupsVaclObj->getEntity('comments-moderators')
);
// add 'johndoe' to the comments-moderators group
$usersVaclObj->getEntity('johndoe')
->addParent(
$groupsVaclObj->getEntity('comments-moderators')
);
// add 'janedoe' to the posts-moderators group
$usersVaclObj->getEntity('janedoe')
->addParent(
$groupsVaclObj->getEntity('posts-moderators')
);
// Now let's model the two group memberships
// below for each user
// | Group | User |
// |---------------------|-----------|
// | comments-owners | all |
// | posts-owners | all |
$usersVaclObj->getEntity('frankwhite')
->addParent(
$groupsVaclObj->getEntity('owners')
); // frankwhite's membership in the admin group
// already grants him permission to perform any
// action on any resource, so this membership is
// redundant for him
$usersVaclObj->getEntity('ginawhite')
->addParent(
$groupsVaclObj->getEntity('owners')
);
$usersVaclObj->getEntity('johndoe')
->addParent(
$groupsVaclObj->getEntity('owners')
);
$usersVaclObj->getEntity('janedoe')
->addParent(
$groupsVaclObj->getEntity('owners')
);
$usersVaclObj->getEntity('jackbauer')
->addParent(
$groupsVaclObj->getEntity('owners')
);
$usersVaclObj->getEntity('jillbauer')
->addParent(
$groupsVaclObj->getEntity('owners')
);