2017 © Pedro Peláez
 

library simpleacl

Simple Access Control List (ACL) for PHP.

image

alexshelkov/simpleacl

Simple Access Control List (ACL) for PHP.

  • Thursday, February 15, 2018
  • by alexshelkov
  • Repository
  • 13 Watchers
  • 90 Stars
  • 17,334 Installations
  • PHP
  • 4 Dependents
  • 0 Suggesters
  • 19 Forks
  • 0 Open issues
  • 29 Versions
  • 10 % Grown

The README.md

Simple Access Control List (ACL) for PHP., (*1)

Test Status Coverage Status, (*2)


Install

Using composer

Add following in your composer.json:, (*3)

{
    "require": {
        "alexshelkov/simpleacl": "2.*"
    }
}
Manual

Download library and register PSR-0 compatible autoloader., (*4)


Usage

Basic usage
Theory

There is 4 kind of objects: Rules, Roles, Resources and Acl which holds list of Rules. Some Rule can grant access for some Role to some Resource., (*5)

Create rules

Lets create "View" Rule, and with with it grant access for "User" to "Page" (note: all names are case sensitive):, (*6)

$view = new Rule('View');
$view->setRole(new Role('User'));
$view->setResource(new Resource('Page'));
$view->setAction(true); // true means that we allow access

var_dump((bool)$view->isAllowed('User', 'Page')); // true
Add rules

There is not much sense in rules without Acl. So we need to add rules in it. In next example we add few rules in Acl and see whats happens., (*7)

$acl = new Acl();

$user = new Role('User');
$admin = new Role('Admin');

$siteFrontend = new Resource('SiteFrontend');
$siteBackend = new Resource('SiteBackend');

$acl->addRule($user, $siteFrontend, new Rule('View'), true);
$acl->addRule($admin, $siteFrontend, 'View', true); // you can use string as rule
$acl->addRule($admin, $siteBackend, 'View', true);

var_dump($acl->isAllowed('User', 'SiteFrontend', 'View')); // true
var_dump($acl->isAllowed('User', 'SiteBackend', 'View')); // false
var_dump($acl->isAllowed('Admin', 'SiteFrontend', 'View')); // true
var_dump($acl->isAllowed('Admin', 'SiteBackend', 'View')); // true

They are various way to add rules to Acl, addRule method accepts from one to four arguments, so you can also add rules like this:, (*8)

<?php
// before add $view rule to Acl you can set it action, role or resource
$acl->addRule($view);

// where is true -- is action
$acl->addRule($view, true);

// in that case action must be set before adding rule
$acl->addRule($user, $siteBackend, $view);
Roles and resource inheritance

As you maybe notice in previous example we have some duplication of code, because both "User" and "Admin" was allowed to "View" "SiteFrontend" we added 2 rules. But it is possible to avoid this using roles inheritance., (*9)

$acl = new Acl();

$user = new Role('User');
$admin = new Role('Admin');
$user->addChild($admin); // add user's child

$siteFrontend = new Resource('SiteFrontend');
$siteBackend = new Resource('SiteBackend');

$acl->addRule($user, $siteFrontend, 'View', true);
$acl->addRule($admin, $siteBackend, 'View', true);

var_dump($acl->isAllowed('User', 'SiteFrontend', 'View')); // true
var_dump($acl->isAllowed('User', 'SiteBackend', 'View')); // false
var_dump($acl->isAllowed('Admin', 'SiteFrontend', 'View')); // true
var_dump($acl->isAllowed('Admin', 'SiteBackend', 'View')); // true

Inheritance works for resources too., (*10)

Using callbacks

You can create more complex rules using callbacks., (*11)

$acl = new Acl();

$user = new Role('User');
$siteFrontend = new Resource('SiteFrontend');

$acl->addRule($user, $siteFrontend, 'View', function (SimpleAcl\RuleResult $ruleResult) {
    echo $ruleResult->getNeedRoleName() . "\n";
    echo $ruleResult->getNeedResourceName() . "\n";
    echo $ruleResult->getPriority() . "\n";
    echo $ruleResult->getRule()->getRole()->getName() . "\n";
    echo $ruleResult->getRule()->getResource()->getName() . "\n";

    return true;
});


var_dump($acl->isAllowed('User', 'SiteFrontend', 'View')); // true

// Outputs:
// User
// SiteFrontend
// 0
// User
// SiteFrontend
// bool(true)
Using role and resource aggregates

It is possible to check access not for particular Role or Resource, but for objects which aggregate them. These kind of objects must implement, respectively, SimpleAcl\Role\RoleAggregateInterface and SimpleAcl\Role\ResourceAggregateInterface., (*12)

You can use SimpleAcl\Role\RoleAggregate and SimpleAcl\Role\ResourceAggregate as object which allow aggregation., (*13)

$acl = new Acl();

$user = new Role('User');
$admin = new Role('Admin');

$all = new RoleAggregate;
$all->addRole($user);
$all->addRole($admin);

$siteFrontend = new Resource('SiteFrontend');
$siteBackend = new Resource('SiteBackend');

$acl->addRule($user, $siteFrontend, 'View', true);
$acl->addRule($admin, $siteBackend, 'View', true);

var_dump($acl->isAllowed($all, 'SiteFrontend', 'View')); // true
var_dump($acl->isAllowed($all, 'SiteBackend', 'View')); // true

You can have access to role and resource aggregates in callbacks., (*14)

$acl->addRule($user, $siteFrontend, 'View', function (SimpleAcl\RuleResult $ruleResult) {
    var_dump($ruleResult->getRoleAggregate());
    var_dump($ruleResult->getResourceAggregate());
});

var_dump($acl->isAllowed($all, 'SiteFrontend', 'View')); // true

For more help check out wiki pages., (*15)

The Versions

15/02 2018

dev-master

9999999-dev https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

15/02 2018

2.0.24

2.0.24.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

13/10 2015

2.0.23

2.0.23.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

12/10 2015

2.0.22

2.0.22.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

The Development Requires

by Alex Shelkovskiy

authorization acl permission

12/08 2014

2.0.21

2.0.21.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

16/09 2013

2.0.20

2.0.20.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

16/09 2013

2.0.19

2.0.19.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

15/08 2013

2.0.18

2.0.18.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

19/05 2013

2.0.17

2.0.17.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

09/05 2013

2.0.16

2.0.16.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

08/05 2013

2.0.15

2.0.15.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

05/03 2013

2.0.14

2.0.14.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

05/03 2013

2.0.13

2.0.13.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

04/03 2013

2.0.12

2.0.12.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

28/02 2013

2.0.11

2.0.11.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

26/02 2013

2.0.10

2.0.10.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

26/02 2013

2.0.9

2.0.9.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

24/02 2013

2.0.8

2.0.8.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

24/02 2013

2.0.7

2.0.7.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

24/02 2013

2.0.6

2.0.6.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

24/02 2013

2.0.5

2.0.5.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

22/02 2013

2.0.4

2.0.4.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

21/02 2013

2.0.3

2.0.3.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

20/02 2013

2.0.2

2.0.2.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

19/02 2013

2.0.1

2.0.1.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

19/02 2013

2.0.0

2.0.0.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

16/02 2013

1.0.2

1.0.2.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

11/02 2013

1.0.1

1.0.1.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

authorization acl permission

28/10 2012

1.0.0

1.0.0.0 https://github.com/alexshelkov/SimpleAcl

Simple Access Control List (ACL) for PHP.

  Sources   Download

The Requires

  • php >=5.3.0

 

by Alex Shelkovskiy

acl