PHPackages                             alexshelkov/simpleacl - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. alexshelkov/simpleacl

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

alexshelkov/simpleacl
=====================

Simple Access Control List (ACL) for PHP.

2.0.25(2y ago)11081.9k↓24.7%231BSD-3-ClausePHPPHP &gt;=5.3.0

Since Oct 28Pushed 2y ago12 watchersCompare

[ Source](https://github.com/alexshelkov/SimpleAcl)[ Packagist](https://packagist.org/packages/alexshelkov/simpleacl)[ Docs](https://github.com/alexshelkov/SimpleAcl)[ RSS](/packages/alexshelkov-simpleacl/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (30)Used By (1)

Simple Access Control List (ACL) for PHP.

[![Test Status](https://github.com/alexshelkov/SimpleAcl/actions/workflows/test.yml/badge.svg)](https://github.com/alexshelkov/SimpleAcl/actions/workflows/test.yml)[![Coverage Status](https://camo.githubusercontent.com/9ff2fed2775bb955538ee3e0eea483ab362e08cd391430b31954c6aa8d8720a7/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f616c65787368656c6b6f762f53696d706c6541636c2f62616467652e7376673f6272616e63683d6d617374657226736572766963653d676974687562)](https://coveralls.io/github/alexshelkov/SimpleAcl?branch=master)

---

#### Install

[](#install)

##### Using composer

[](#using-composer)

Add following in your composer.json:

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

##### Manual

[](#manual)

Download library and register PSR-0 compatible autoloader.

---

#### Usage

[](#usage)

##### Basic usage

[](#basic-usage)

###### Theory

[](#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*.

###### Create rules

[](#create-rules)

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

```
$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

[](#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.

```
$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:

```
