jumbodroid/phprbac

PHP RBAC is a wrapper around the OWASP/rbac package to support the Laravel framework

v2.0.3 2022-05-02 05:53 UTC

This package is auto-updated.

Last update: 2024-10-30 02:28:58 UTC


README

phprbac is wrapper around the OWASP/rbac package that adds support for the Laravel framework

Leverage phprbac to add NIST Level 2 Authorization to your Laravel projects.

Add the provider to config/app.php providers array

Jumbodroid\PhpRbac\ServiceProviders\PhpRbacServiceProvider::class

Run php artisan vendor:publish --provider="Jumbodroid\PhpRbac\ServiceProviders\PhpRbacServiceProvider" to publish the config and migration files.

Usage

The complete documentation can be found at phprbac.net. Just replace:

  1. Rbac->{Permissions} with Rbac->permissions()
  2. Rbac->{Roles} with Rbac->roles()
  3. Rbac->{Users} with Rbac->users()
    To get an instance of the Rbac class, use Rbac->getInstance()

Roles

  1. Create new role
$role_id = Rbac::roles()->add($title, $description);  
$role_id = Rbac::roles()->add($title, $description, $parent_id);  
  1. Find role title
$role = Rbac::roles()->getTitle($role_id);
  1. Find list of permissions assigned to a given role
$role_permissions = Rbac::roles()->permissions($role_id, true);  
  1. Unassign all permissions assigned to a given role
Rbac::roles()->unassignPermissions($role_id);  
  1. Assign permission to a role
Rbac::roles()->assign($role_id, $perm_id);  
  1. Remove a role and all its descendants
$ok = Rbac::roles()->remove($role_id, true);  
  1. Removes a role
Rbac::roles()->remove($role->ID, true);  

Permissions

  1. Create a new permission
$perm_id = Rbac::permissions()->add($perm_title, $perm_desc, 1);  
  1. Check if a given permission exists
$perm_id = Rbac::permissions()->returnId($perm_title);  
  1. Unassign all roles assigned to a given permission
Rbac::permissions()->unassignRoles($perm_id);  
  1. Removes a given permission
Rbac::permissions()->remove($perm_id, true);  

Users

  1. Unassign a given role from all users
Rbac::roles()->unassignUsers($role_id);  
  1. Check if a given user has a given role
$assigned = Rbac::users()->hasRole($role['ID'], $user->id);  
  1. Retrieves all roles assigned to a given user
$assigned = Rbac::users()->allRoles($user_id);  
  1. Assign a given role to a user
Rbac::users()->assign($role_id, $user_id);  
  1. Unassign a given role from a user
Rbac::users()->unassign($role_id, $user_id);  

UserRoles

RolePermissions