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
Requires
- php: >=7.3
- illuminate/config: >=7.11
- owasp/phprbac: ^2.0
Requires (Dev)
- mockery/mockery: ^1.3.1
- phpunit/phpunit: ^8.5|^9.3.3
- symfony/var-dumper: >=5.2.0
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:
Rbac->{Permissions}
withRbac->permissions()
Rbac->{Roles}
withRbac->roles()
Rbac->{Users}
withRbac->users()
To get an instance of the Rbac class, useRbac->getInstance()
Roles
- Create new role
$role_id = Rbac::roles()->add($title, $description); $role_id = Rbac::roles()->add($title, $description, $parent_id);
- Find role title
$role = Rbac::roles()->getTitle($role_id);
- Find list of permissions assigned to a given role
$role_permissions = Rbac::roles()->permissions($role_id, true);
- Unassign all permissions assigned to a given role
Rbac::roles()->unassignPermissions($role_id);
- Assign permission to a role
Rbac::roles()->assign($role_id, $perm_id);
- Remove a role and all its descendants
$ok = Rbac::roles()->remove($role_id, true);
- Removes a role
Rbac::roles()->remove($role->ID, true);
Permissions
- Create a new permission
$perm_id = Rbac::permissions()->add($perm_title, $perm_desc, 1);
- Check if a given permission exists
$perm_id = Rbac::permissions()->returnId($perm_title);
- Unassign all roles assigned to a given permission
Rbac::permissions()->unassignRoles($perm_id);
- Removes a given permission
Rbac::permissions()->remove($perm_id, true);
Users
- Unassign a given role from all users
Rbac::roles()->unassignUsers($role_id);
- Check if a given user has a given role
$assigned = Rbac::users()->hasRole($role['ID'], $user->id);
- Retrieves all roles assigned to a given user
$assigned = Rbac::users()->allRoles($user_id);
- Assign a given role to a user
Rbac::users()->assign($role_id, $user_id);
- Unassign a given role from a user
Rbac::users()->unassign($role_id, $user_id);