This package allows you to manage user permissions and roles in a database for Laravel project.
You can install the package via composer:
composer require obrainwave/access-tree
You can publish and run the migrations with:
php artisan vendor:publish --tag="accesstree-migrations"
php artisan migrate
Create a Root User:
After running migration, a new column is_root_user
will be added to the users
table. To create a user that can override all permissions and roles in your application, set the column to be true
for the particular user.
You can publish the config file with:
php artisan vendor:publish --tag="accesstree-config"
Create a Permission
$data = [
'name' => 'Add User',
'status' => 1 or 0
];
$create = createAccess($data, 'permission');
echo $create;
Update a Permission
$data = [
'data_id' => permission id from Obrainwave\AccessTree\Models\Permission // 3,
'name' => 'Add User',
'status' => 1 or 0
];
$update = updateAccess($data, 'permission');
echo $update;
Create Roles
$data = [
'name' => 'Admin',
'status' => 1 or 0
];
$permission_ids = array of permission ids from Obrainwave\AccessTree\Models\Permission // array(1, 5, 4);
$create = createAccess($data, 'role', $permission_ids);
echo $create;
Update Roles
$data = [
'data_id' => role id // 5,
'name' => 'Admin Staff',
'status' => 1 or 0
];
$permission_ids = array of permission ids from Obrainwave\AccessTree\Models\Permission // array(10, 6, 3);
$update = updateAccess($data, 'role', $permission_ids);
echo $update;
Create User Role
$roles = array of roles from Obrainwave\AccessTree\Models\Role // array(2, 5);
$user_id = id of a user from App\Models\User // 1;
$user_role = createUserRole($roles, $user_id);
echo $user_role;
Update User Role
$roles = array of roles from Obrainwave\AccessTree\Models\Role // array(2, 5);
$user_id = id of a user from App\Models\User // 5;
$user_role = updateUserRole($roles, $user_id);
echo $user_role;
Check User Access or Permission
checkPermission(string slug_of_permission) // returns true or false
or
checkPermissions(array slug_of_permissions) // returns true or false
Call the above functions anywhere in your application. Example of usage:
@if(checkPermission('add_user')
// Do some stuff
@else
throw new \Exception("Access Forbidden", 1);
@endif
@if(checkPermissions(['add_user', 'view_user'])
// Do some stuff
@else
throw new \Exception("Access Forbidden", 1);
@endif
Check if a User is a Root User
isRootUser(int user_id) // returns true or false
Fetch Permissions
fetchPermissions(int $status) // active = 1 or inactive = 0
// $status is optional if is empty all permissions will be fetched
Fetch Roles
fetchRoles(int $status) // active = 1 or inactive = 0
// $status is optional if is empty all roles will be fetched
Fetch User Roles
fetchUserRoles(int $user_id)
// $user_id is id of the user from App\Models\User
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.