PHP code example of fsi / acl

1. Go to this page and download the library: Download fsi/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/ */

    

fsi / acl example snippets

 php


use FSi\Component\ACL;

$acl = new ACL();
$acl->addPermission($somePermisssion);

...

$acl->addResource($someResource);

...

$acl->addRole($someRole);

...

$acl->addACE($someACE);

...

 php


if ($acl->isAllowed($someRole, $someResource, $somePermission)) {
    
    ...
    
}
 php


$userRole = RoleSimple::factory('user');
$newsEditorRole = RoleSimple::factory('newsEditor');
$articleEditorRole = RoleSimple::factory('articleEditor');
$publisherRole = RoleSimple::factory('publisher');
$newsResource = ResourceSimple::factory('newsClass');
$articleResource = ResourceSimple::factory('articleClass');
$view = PermissionSimple::factory('view');
$edit = PermissionSimple::factory('edit');
$publish = PermissionSimple::factory('publish');

$acl = new ACL();
$acl->addPermission($view);
$acl->addPermission($edit);
$acl->addPermission($publish);
$acl->addResource($newsResource);
$acl->addResource($articleResource);
$acl->addRole($userRole);
$acl->addRole($newsEditorRole);
$acl->addRole($articleEditorRole);
$acl->addRole($publisherRole, array($userRole, $newsEditorRole, $articleEditorRole));
$acl->addACE(new ACEAllow($userRole, $newsResource, $view));
$acl->addACE(new ACEAllow($userRole, $articleResource, $view));
$acl->addACE(new ACEAllow($newsEditorRole, $newsResource, $edit));
$acl->addACE(new ACEAllow($articleEditorRole, $articleResource, $edit));
$acl->addACE(new ACEDeny($userRole, $newsResource, $publish));
$acl->addACE(new ACEDeny($userRole, $articleResource, $publish));

$acl->isAllowed($publisherRole, $newsResource, $publish)
// returns false

$acl->isAllowed($publisherRole, $articleResource, $publish)
// returns false

$acl->isAllowed($publisherRole, $newsResource, $edit)
// returns true

$acl->isAllowed($publisherRole, $articleResource, $edit)
// returns true