PHP code example of rotexsoft / versatile-acl

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')
             );



var_dump( $usersVaclObj->isAllowed('frankwhite', 'approve', 'comment') ); // === true
var_dump( $usersVaclObj->isAllowed('frankwhite', 'delete', 'comment') ); // === true
var_dump( $usersVaclObj->isAllowed('frankwhite', 'approve', 'post') ); // === true
var_dump( $usersVaclObj->isAllowed('frankwhite', 'delete', 'post') ); // === true



var_dump( $usersVaclObj->isAllowed('ginawhite', 'approve', 'comment') ); // === true
var_dump( $usersVaclObj->isAllowed('ginawhite', 'delete', 'comment') ); // === true
var_dump( $usersVaclObj->isAllowed('ginawhite', 'approve', 'post') ); // === false
var_dump( $usersVaclObj->isAllowed('ginawhite', 'delete', 'post') ); // === false

// Assuming we have the post record below and the user record for 'ginawhite' below
$postRecord = [
    'id' => 2,
    'body' => 'Some random post',
    'creators_id' => 'ginawhite',
    'last_updaters_id' => 'ginawhite',
    'date_created' => '2019-08-01 13:43:21',
    'last_updated' => '2019-08-01 13:43:21',
    'is_approved' => '0',
];

$userRecord = [
    'id' => 'ginawhite',
    'password' => 'TydlfEUSqnVMu'
];

// Here's how we would check if 'ginawhite' can approve and delete posts she has created
var_dump( 
    $usersVaclObj->isAllowed(
        'ginawhite', 
        'approve', 
        'post',
        null,
        $userRecord,
        $postRecord
    )
); // === true

var_dump( 
    $usersVaclObj->isAllowed(
        'ginawhite', 
        'delete', 
        'post',
        null,
        $userRecord,
        $postRecord
    )
); // === true



var_dump( $usersVaclObj->isAllowed('johndoe', 'approve', 'comment') ); // === true
var_dump( $usersVaclObj->isAllowed('johndoe', 'delete', 'comment') ); // === true
var_dump( $usersVaclObj->isAllowed('johndoe', 'approve', 'post') ); // === false
var_dump( $usersVaclObj->isAllowed('johndoe', 'delete', 'post') ); // === false

// Assuming we have the post record below and the user record for 'johndoe' below
$postRecord2 = [
    'id' => 2,
    'body' => 'Some random post',
    'creators_id' => 'johndoe',
    'last_updaters_id' => 'johndoe',
    'date_created' => '2019-08-01 13:43:21',
    'last_updated' => '2019-08-01 13:43:21',
    'is_approved' => '0',
];

$userRecord2 = [
    'id' => 'johndoe',
    'password' => 'TydlfEUSqnVMu'
];

var_dump( 
    $usersVaclObj->isAllowed(
        'johndoe', 
        'approve', 
        'post',
        null,
        $userRecord2,
        $postRecord2
    )
); // === true

var_dump(
    $usersVaclObj->isAllowed(
        'johndoe', 
        'delete', 
        'post',
        null,
        $userRecord2,
        $postRecord2
    )
); // === true



var_dump( $usersVaclObj->isAllowed('janedoe', 'approve', 'comment') ); // === false
var_dump( $usersVaclObj->isAllowed('janedoe', 'delete', 'comment') ); // === false
var_dump( $usersVaclObj->isAllowed('janedoe', 'approve', 'post') ); // === true
var_dump( $usersVaclObj->isAllowed('janedoe', 'delete', 'post') ); // === true

// Assuming we have the comment record below and the user record for 'janedoe' below
$commentRecord3 = [
    'id' => 1,
    'post_id' => 2,
    'commenter_id' => 'janedoe',
    'comment' => 'Some random comment',
    'date_created' => '2019-08-01 13:43:21',
    'last_updated' => '2019-08-01 13:43:21',
    'is_approved' => '0',
];

$userRecord3 = [
    'id' => 'janedoe',
    'password' => 'TydlfEUSqnVMu'
];

var_dump(
    $usersVaclObj->isAllowed(
        'janedoe', 
        'approve', 
        'comment',
        null,
        $userRecord3,
        $commentRecord3
    )
);  // === true

var_dump(
    $usersVaclObj->isAllowed(
        'janedoe', 
        'delete', 
        'comment',
        null,
        $userRecord3,
        $commentRecord3
    )
); // === true


// all comments including those not owned by jackbauer
var_dump( $usersVaclObj->isAllowed('jackbauer', 'approve', 'comment') ); // === false
var_dump( $usersVaclObj->isAllowed('jackbauer', 'delete', 'comment') ); // === false

// all posts including those not owned by jackbauer
var_dump( $usersVaclObj->isAllowed('jackbauer', 'approve', 'post') ); // === false
var_dump( $usersVaclObj->isAllowed('jackbauer', 'delete', 'post') ); // === false

// Assuming we have the post and comment records below and the user record for 'jackbauer' below
$commentRecord5 = [
    'id' => 1,
    'post_id' => 2,
    'commenter_id' => 'jackbauer',
    'comment' => 'Some random comment',
    'date_created' => '2019-08-01 13:43:21',
    'last_updated' => '2019-08-01 13:43:21',
    'is_approved' => '0',
];

$postRecord5 = [
    'id' => 2,
    'body' => 'Some random post',
    'creators_id' => 'jackbauer',
    'last_updaters_id' => 'jackbauer',
    'date_created' => '2019-08-01 13:43:21',
    'last_updated' => '2019-08-01 13:43:21',
    'is_approved' => '0',
];

$userRecord5 = [
    'id' => 'jackbauer',
    'password' => 'TydlfEUSqnVMu'
];

// comment owned by jackbauer
var_dump(
    $usersVaclObj->isAllowed(
        'jackbauer', 
        'approve', 
        'comment',
        null,
        $userRecord5,
        $commentRecord5
    )
); // === true

// comment owned by jackbauer
var_dump(
    $usersVaclObj->isAllowed(
        'jackbauer', 
        'delete', 
        'comment',
        null,
        $userRecord5,
        $commentRecord5
    )
); // === true

// post owned by jackbauer
var_dump(
    $usersVaclObj->isAllowed(
        'jackbauer', 
        'approve', 
        'post',
        null,
        $userRecord5,
        $postRecord5
    )
); // === true

// post owned by jackbauer
var_dump(
    $usersVaclObj->isAllowed(
        'jackbauer', 
        'delete', 
        'post',
        null,
        $userRecord5,
        $postRecord5
    )
); // === true


// all comments including those not owned by jillbauer
var_dump( $usersVaclObj->isAllowed('jillbauer', 'approve', 'comment') ); // === false
var_dump( $usersVaclObj->isAllowed('jillbauer', 'delete', 'comment') ); // === false

// all posts including those not owned by jillbauer
var_dump( $usersVaclObj->isAllowed('jillbauer', 'approve', 'post') ); // === false
var_dump( $usersVaclObj->isAllowed('jillbauer', 'delete', 'post') ); // === false

// Assuming we have the post and comment records below and the user record for 'jackbauer' below
$commentRecord6 = [
    'id' => 1,
    'post_id' => 2,
    'commenter_id' => 'jillbauer',
    'comment' => 'Some random comment',
    'date_created' => '2019-08-01 13:43:21',
    'last_updated' => '2019-08-01 13:43:21',
    'is_approved' => '0',
];

$postRecord6 = [
    'id' => 2,
    'body' => 'Some random post',
    'creators_id' => 'jillbauer',
    'last_updaters_id' => 'jillbauer',
    'date_created' => '2019-08-01 13:43:21',
    'last_updated' => '2019-08-01 13:43:21',
    'is_approved' => '0',
];

$userRecord6 = [
    'id' => 'jillbauer',
    'password' => 'TydlfEUSqnVMu'
];

// comment owned by jillbauer
var_dump(
    $usersVaclObj->isAllowed(
        'jillbauer', 
        'approve', 
        'comment',
        null,
        $userRecord6,
        $commentRecord6
    )
); // === true

// comment owned by jillbauer
var_dump(
    $usersVaclObj->isAllowed(
        'jillbauer', 
        'delete', 
        'comment',
        null,
        $userRecord6,
        $commentRecord6
    )
); // === true

// post owned by jillbauer
var_dump(
    $usersVaclObj->isAllowed(
        'jillbauer', 
        'approve', 
        'post',
        null,
        $userRecord6,
        $postRecord6
    )
); // === true

// post owned by jillbauer
var_dump(
    $usersVaclObj->isAllowed(
        'jillbauer', 
        'delete', 
        'post',
        null,
        $userRecord6,
        $postRecord6
    )
); // === true


$sAcl = new VersatileAcl();

// enable logging of internal activities
$sAcl->enableAuditTrail(true);

// enable verbose logging of internal activities
$sAcl->enableVerboseAudit(true); 

// call some methods
$sAcl->addEntity('jblow');
$sAcl->removeEntity('jblow');

echo 'Outputing verbose Audit Trail: ' . PHP_EOL . PHP_EOL;
echo $sAcl->getAuditTrail();

// Clear the contents of the audit trail
$sAcl->clearAuditTrail();

// disable verbose logging of internal activities
$sAcl->enableVerboseAudit(false); 

// call some methods
$sAcl->addEntity('jblow');
$sAcl->removeEntity('jblow');

echo 'Outputing non-verbose Audit Trail: ' . PHP_EOL . PHP_EOL;
echo $sAcl->getAuditTrail();