Roles
Alexya's Role Based Access System utilities., (*1)
Contents
, (*2)
Creating permissions
Permissions are the way to authorized users to perform certain actions., (*3)
The class \Alexya\Roles\Permission
represents a permission that can be assigned to a specific role., (*4)
A permission consists of an identifier, a title and a status flag., (*5)
You can easily extend this class to provide more functionality such as alternative names, ranks..., (*6)
For example, in a file system, each user should have permissions to read/write to certain files:, (*7)
<?php
namespace Application\RBAC\Permissions;
use Alexya\Roles\Permission;
class Read extends Permission
{
/**
* Constructor.
*/
public function __construct()
{
parent::__construct(1, "read", Permission::STATUS_INHERITED);
}
}
class Write extends Permission
{
/**
* Constructor.
*/
public function __construct()
{
parent::__construct(2, "write", Permission::STATUS_INHERITED);
}
}
, (*8)
Creating roles
Roles are the containers of various permissions., (*9)
The class \Alexya\Roles\Role
represents a role by itself., (*10)
It has an identifier for the role, a title and an array of Permission
with the granted permissions for this role.
Alternatively, a role can have a parent role for inheritance., (*11)
It also has an integer representing the priority of the role, the bigger the number, the most priority it has., (*12)
The method hasPermission
accepts an identifier of a permission or an instance of the permission and checks that this role has been granted with the permission., (*13)
For a shorter version, you can use can
, 'cuz shorter > longer., (*14)
Example:, (*15)
namespace Application\RBAC\Roles;
use Alexya\Roles\Role;
class CanRead extends Role
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct(1, "can_read", 1);
}
}
class CanWrite extends Role
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct(2, "can_write", 1);
}
}
class CanReadAndWrite extends Role
{
/**
* Constructor.
*/
public function __construct()
{
parent::__construct(3, "can_read_and_write", 2);
}
}
, (*16)
Adding permissions to roles
Once you have the permissions and roles you need to specify which roles have granted which permissions., (*17)
A permission can be enabled, disabled or inherited. If a permission is inherited the role will check the status
of that permission in the parent role, if there's no parent role it will be treated as disabled., (*18)
Adding permissions to roles is as easy as calling the addPermission
method with the permission to add., (*19)
Example:, (*20)
$canRead = new CanRead();
$canRead->addPermission(new Read());
$canWrite = new CanWrite();
$canWrite->addPermission(new Write());
$canReadAndWrite = new CanReadAndWrite();
$canReadAndWrite->addPermission(new Read());
$canReadAndWrite->addPermission(new Write());
// Alternatively you could have set the parent role like this:
// $canReadAndWrite = new CanReadAndWrite();
// $canReadAndWrite->parent = $canRead;
// $canReadAndWrite->addPermission(new Write());
To check if a role has granted a certain permission you can use the hasPermission
or can
methods:, (*21)
$canRead->can("read"); // true
$canRead->can(2); // false (Write permission has ID 2)
$canRead->can(new Read()); // true
The method getPermission
returns a permission from the role:, (*22)
$read = $canRead->getPermission(new Read()); // The Read permission sent through `addPermission`
$write = $canRead->getPermission("write"); // null;
, (*23)
Creating users
Now that we have the roles and permissions we need users to assign them., (*24)
The class \Alexya\Roles\User
represents an user, where the roles are assigned., (*25)
It's the class that you should extend in order to add roles to your users as it provides the hasPermission
and can
methods for checking if this user has any permission granted., (*26)
It also has the methods addRole
and removeRole
to add/remove roles., (*27)
Example:, (*28)
$user = new User();
$user->addRole(2);
$user->can(new Read()); // false
$user->can(new Write()); // true
$user->addRole(new CanRead());
$user->can("read"); // true
$user->can(new Write()); // true
$user->getRole("can_write")
->getPermission("write")
->status = Permission::STATUS_DISABLED;
$user->can(new Read()); // true
$user->can(new Write()); // false
$user->addRole(new CanReadAndWrite());
$user->can(new Read()); // true
$user->can(new Write()); // true because the priority of `CanReadAndWrite` is bigger than the `CanWrite` priority.