PHP code example of gobline / acl

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

    

gobline / acl example snippets


$memberArea = new Gobline\Acl\Resource('/member-area', new Gobline\Acl\Matcher\StartsWithMatcher());

$acl->addRole('member')
	->allow('member', $memberArea, '*');
    
$adminArea = new Gobline\Acl\Resource('/admin-area', new Gobline\Acl\Matcher\StartsWithMatcher());

$acl->addRole('admin')
	->allow('admin', $adminArea, '*');

$acl->isAllowed('guest', '/member-area/edit/profile'); // returns false
$acl->isAllowed('member', '/member-area/edit/profile'); // returns true
$acl->isAllowed('member', '/admin-area/user/list'); // returns false

$acl = new Gobline\Acl\Acl();

$acl->addRole('guest');

$roleGuest = new Gobline\Acl\Role('guest');
$acl->addRole($roleGuest);

$acl->allow('guest', 'page', 'view'); // the role "guest" is now allowed to "view" the "page" resource

$acl->allow('member', 'page', ['view', 'create', 'edit']); // the role "member" is allowed to "view", "create" and "edit" the "page" resource

$acl->allow('guest', '*', 'view'); // the role "guest" is now allowed to "view" any resource

$acl->allow('admin', 'page', '*'); // the role "admin" is now allowed to access any privilege on the "page" resource

$acl->allow('superadmin', '*', '*'); // the role "superadmin" is now allowed to access any privilege on any resource

$acl->isAllowed('guest', 'page', 'edit'); // returns false

$acl->isAllowed('admin', 'page', 'edit'); // returns true

$acl->isAllowed('admin', 'user', 'edit'); // returns false

$acl->isAllowed('superadmin', 'user', 'edit'); // returns true

$resource = new Gobline\Acl\Resource('/home/john', new Gobline\Acl\Matcher\StartsWithMatcher());

$acl->addRole('john')
	->allow('john', $resource, 'read');

$acl->isAllowed('john', '/home/john/file.txt', 'read'); // returns true
$acl->isAllowed('john', '/home/john/file.txt', 'write'); // returns false
$acl->isAllowed('john', '/home/matthew/file.txt', 'read'); // returns false

$resource = new Gobline\Acl\Resource('%^/page/(.*?)/view%', new RegexMatcher());

$acl->addRole('guest')
	->allow('guest', $resource, '*');

$acl->isAllowed('guest', '/page/42/view')); // returns true
$acl->isAllowed('guest', '/page/42/edit')); // returns false

$acl->addRole('guest')
  ->addRole('member', 'guest') // "member" inherits the rules of "guest"
  ->addRole('moderator', 'member'); // "moderator" inherits the rules of "member"
  
$acl->allow('guest', 'page', 'view') // guests can only view pages
  ->allow('member', 'page', ['create', 'edit']) // members can view, create and edit pages
  ->deny('moderator', 'page', 'create') // moderators cannot create a new page
  ->allow('moderator', 'page', 'delete'); // moderators can view, edit and delete pages

$roles = new Gobline\Acl\Roles(); // registry of roles

$roles->add('guest')
  ->add('member', 'guest')
  ->add('moderator', 'member');

$acl1 = new Gobline\Acl\Acl($roles);

// defining rules for $acl1

$acl2 = new Gobline\Acl\Acl($roles);

// defining rules for $acl2