2017 © Pedro PelĂĄez
 

library rbac

Role Based Access Control

image

web-complete/rbac

Role Based Access Control

  • Monday, February 12, 2018
  • by mvkasatkin
  • Repository
  • 1 Watchers
  • 3 Stars
  • 77 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 6 Versions
  • 1 % Grown

The README.md

WebComplete RBAC

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version License, (*1)

Tiny flexible RBAC implementation with no dependencies., (*2)

Role-based-access-control (RBAC) is a policy neutral access control mechanism defined around roles and privileges. The components of RBAC such as role-permissions, user-role and role-role relationships make it simple to perform user assignments., (*3)

Installation

composer require web-complete/rbac

Usage

  • Initiate with resource object. Resource object can be a FileResource or a RuntimeResource. You can also create any necessary resource (Mysql, Redis, Bongo etc) by extending AbstractResource or implementing ResourceInterface.
$resource = new FileResource($path . '/rbac.data');
$rbac = new Rbac($resource);
  • Create permissions hierarchy
$p1 = $rbac->createPermission('post:create', 'Can create posts');
$p2 = $rbac->createPermission('post:moderate', 'Can moderate posts');
$p3 = $rbac->createPermission('post:update', 'Can update posts');
$p4 = $rbac->createPermission('post:delete', 'Can delete posts');
$p2->addChild($p3); // moderator can also update
$p2->addChild($p4); // and delete posts
  • Create role hierarchy
$adminRole = $rbac->createRole('admin');
$moderatorRole = $rbac->createRole('moderator');
$authorRole = $rbac->createRole('author');
$adminRole->addChild($moderatorRole); // admin has all moderator's rights
  • Bind roles and permissions
...
$moderatorRole->addPermission($p2);
...
  • Persist state
$rbac->save();
  • Checking access rights
if($rbac->getRole($user->role)->checkAccess('post:moderate') {
    ... // User can moderate posts
}
// or add to your user's class something like:
$user->can('post:moderate') 

Rules

Sometimes it's not enough to simple check the permission. For example, an author can edit and delete only his own posts. For that case you can create a rule by implementing RuleInterface with one method «execute»:, (*4)


class AuthorRule implements WebComplete\rbac\entity\RuleInterface { /** * @param array|null $params * * @return bool */ public function execute($params): bool { // @var Post $post if($post = $params['post'] ?? null) { return $post->authorId === ($params['userId'] ?? null); } return false; } }
  • Configure RBAC
$p5 = $rbac->createPermission('post:author:update', 'Author can update his posts');
$p6 = $rbac->createPermission('post:author:delete', 'Author can delete his posts');
$p5->setRuleClass(AuthorRule::class);
$p6->setRuleClass(AuthorRule::class);
$authorRole->addPermission($p5);
$authorRole->addPermission($p6);
  • And then check rights with parameters
if($rbac->checkAccess('post:author:delete', ['userId' => $userId, 'post' => $post]) {
    ... // The user is author of the post and can delete it
}

The Versions

12/02 2018

dev-master

9999999-dev

Role Based Access Control

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

by Maxim Kasatkin
by Sergey Saraikin

authorization acl rbac

12/02 2018

2.0.1

2.0.1.0

Role Based Access Control

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

by Maxim Kasatkin
by Sergey Saraikin

authorization acl rbac

12/02 2018

v2.x-dev

2.9999999.9999999.9999999-dev

Role Based Access Control

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

by Maxim Kasatkin
by Sergey Saraikin

authorization acl rbac

12/02 2018

2.0.0

2.0.0.0

Role Based Access Control

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

by Maxim Kasatkin
by Sergey Saraikin

authorization acl rbac

23/09 2017

1.0.1

1.0.1.0

Role Based Access Control

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

by Maxim Kasatkin
by Sergey Saraikin

authorization acl rbac

23/09 2017

1.0.0

1.0.0.0

Role Based Access Control

  Sources   Download

MIT

The Requires

  • php >=7.0.0

 

The Development Requires

by Maxim Kasatkin
by Sergey Saraikin

authorization acl rbac