Code Monkey home page Code Monkey logo

spice-acl's Introduction

spice-acl Build Status#

Access Control List component of Spice Framework

Usage

use Spice\Acl\Role\Role;
use Spice\Acl\Handler\Handler;

$handler = new Handler();
$user = new Role('user');
$admin = new Role('admin', $user);

$handler->addRole($user);
$handler->addRole($admin);

$handler->addResource('my_resource');

$handler->allow('user', 'my_resource');

$handler->check('user', 'my_resource'); // will pass
$handler->check('admin', 'my_resource'; // will also pass, because 'admin' role extends 'user' role

##Resources Resources are parts of your system whose access you want to restrict.

###Creating Resources Resources are created just giving its name to a Handler object:

$handler->addResource('my_resource');

###Giving permissions to access a Resource

$handler = new Handler();
$user = new Role('user');

$handler->addRole($user);
$handler->addResource('my_resource');

$handler->allow('user', 'my_resource');

Notice that you MUST add a resource to the Handler before you being able to allow access to it, otherwise, an \InvalidArgumentException will be raised:

$handler = new Handler();
$user = new Role('user');

$handler->addRole($user);

$handler->allow('user', 'my_resource'); // exception thrown!!!

Roles

Roles are the actors of your system. You can have as many roles as you need.

Creating Roles

The default implementation of Spice\Acl\Role\RoleInterface is Spice\Acl\Role\Role:

use Spice\Acl\Role\Role;
$user = new Role('user');

Notice that you MUST add a Role to the Handler before you being able to allow it to access a resource, otherwise, an \InvalidArgumentException will be raised:

$handler = new Handler();
$user = new Role('user');

$handler->addResource('my_resource');

$handler->allow('user', 'my_resource'); // exception thrown!!!

Extending Roles

A role can extend another one if there is a second parameter of type RoleInterface on Role constructor:

$user = new Role('user');
$admin = new Role('admin', $user);

A role extending another one will inherit its permissions by default, but you can ovewrite them as you need:

$handler = new Handler();
$user = new Role('user');
$admin = new Role('admin', $user);

$handler->addRole($user);
$handler->addRole($admin);

$handler->addResource('my_resource');
$handler->addResource('my_secret_resource');
$handler->addResource('just_for_regular_users_resource');

$handler->allow('user', 'my_resource');
$handler->allow('user', 'just_for_regular_users_resource');
$handler->allow('admin', 'my_secret_resource');
$handler->deny('admin', 'just_for_regular_users_resource');

$handler->check('user', 'my_resource'); // will pass
$handler->check('admin', 'my_resource'; // will pass
$handler->check('admin', 'my_secret_resource'; // will pass
$handler->check('user', 'my_secret_resource'; // will NOT pass
$handler->check('user', 'just_for_regular_users_resource'); // will pass
$handler->check('admin', 'just_for_regular_users_resource'; // will NOT pass

In the above example, admin role inherits user role permissions. For this reason, it is able to access my_resource. To overwrite this permissions, you have to deny the role admin to access a resource.

Checking Access Permissions

If a given role does not have permission to access a resource, when you call the check method, an Exception of type Spice\Acl\DeniedAccessException will be raised:

$handler = new Handler();
$user = new Role('user');
$admin = new Role('admin', $user);

$handler->addRole($user);
$handler->addRole($admin);

$handler->addResource('my_resource');
$handler->addResource('my_secret_resource');
$handler->addResource('just_for_regular_users_resource');

$handler->allow('user', 'my_resource');
$handler->allow('user', 'just_for_regular_users_resource');
$handler->allow('admin', 'my_secret_resource');
$handler->deny('admin', 'just_for_regular_users_resource');

try {
	$handler->check('user', 'my_secret_resoruce');
} catch (DeniedAccessException $e) {
    die('Permission Denied!');
}

spice-acl's People

Watchers

 avatar  avatar

Forkers

hbarcelos

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.