PHP code example of doc88 / flux-role-permission
1. Go to this page and download the library: Download doc88/flux-role-permission library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
doc88 / flux-role-permission example snippets
composer
'providers' => [
...
Doc88\FluxRolePermission\FluxRolePermissionServiceProvider::class,
]
php artisan vendor:publish
php artisan migrate
use Doc88\FluxRolePermission\Traits\HasRolePermissions;
class User {
use HasRolePermissions;
}
// All Registered Permissions
Permission::all();
// Return (array):
[
[
"id" => 3
"name" => "List Companies"
"slug" => "list-companies"
],
[
"id" => 4
"name" => "Delete Companies"
"slug" => "delete-companies"
]
]
// Get the Permission
Permission::get('list-users');
// Return (array):
[
"id" => 3
"name" => "List Users"
"slug" => "list-users"
]
// Creating a new Permission
Permission::create('create-companies');
// Return (boolean):
true or false
// Deleting a Permission
Permission::delete('list-companies');
// Return (boolean):
true or false
// List all the User's Permissions
Permission::list($user);
// Return (array):
[
[
"id" => 3
"name" => "List Companies"
"slug" => "list-companies"
],
[
"id" => 4
"name" => "Delete Companies"
"slug" => "delete-companies"
],
[
"id" => 5
"name" => "Create Company"
"slug" => "create-companies"
]
]
// Checking if the user has permission
Permission::has(auth()->user(), 'list-companies');
// Return (boolean):
true or false
// Grants permission for the User
Permission::register(auth()->user(), 'list-companies');
// Return (boolean):
true or false
// Revokes permission
Permission::revoke(auth()->user(), 'list-companies');
// Return (boolean):
true or false
$user = User::find(1);
// User's Permissions
$user->listPermissions();
// Return (array):
[
[
"id" => 3
"name" => "List Companies"
"slug" => "list-companies"
],
[
"id" => 4
"name" => "Delete Companies"
"slug" => "delete-companies"
],
[
"id" => 5
"name" => "Create Company"
"slug" => "create-companies"
]
]
$user = User::find(1);
// Checking if the user has permission
$user->hasPermission('list-companies');
// Return (boolean):
true or false
$user = User::find(1);
// Grants permission to the User
$user->registerPermission('list-companies');
// Return (boolean):
true or false
$user = User::find(1);
// Revokes permission
$user->revokePermission('list-companies');
// Return (boolean):
true or false
// All Registered Roles
Role::all();
// Return (array):
[
[
"id" => 3
"name" => "Companies"
"slug" => "companies"
],
[
"id" => 4
"name" => "Users"
"slug" => "users"
]
]
// Get the role
Role::get('users');
// Return (array):
[
"id" => 5
"name" => "Users"
"slug" => "users"
"permissions" => {
[
0 => array:3 [
"id" => 3
"name" => "List Users"
"slug" => "list-users"
],
1 => array:3 [
"id" => 4
"name" => "Delete Users"
"slug" => "delete-users"
],
2 => array:3 [
"id" => 5
"name" => "Create Users"
"slug" => "create-users"
]
]
}
]
// Creating a new Role
Role::create('Users');
// Creating a new Role with permissions
Role::create('users', ['list-users', 'create-users', 'delete-users']);
// Return (array):
[
"id": 7,
"name": "Users",
"slug": "users"
]
// Updating a Role
Role::update('users', ['slug' => 'new-slug', 'permissions' => ['list-users', 'create-users']]);
// Return (array):
[
"id" => 4
"name" => "New Slug"
"slug" => "new-slug"
"permissions" => {
[
"id" => 7
"name" => "List Users"
"slug" => "list-users"
],
[
"id" => 8
"name" => "Create Users"
"slug" => "create-users"
]
}
]
// Deleting a Role
Role::addPermission('sales', 'list-sales');
// Return (boolean):
true or false
// Deleting a Role
Role::removePermission('sales', 'list-sales');
// Return (boolean):
true or false
// Deleting a Role
Role::delete('sales');
// Return (boolean):
true or false
// List all the User's Roles
Role::list($user);
// Return (array):
[
[
"id" => 3
"name" => "Companies"
"slug" => "companies"
],
[
"id" => 4
"name" => "Sales"
"slug" => "sales"
]
]
// Checking if the user has role
Role::has(auth()->user(), 'companies');
// Return (boolean):
true or false
// Grants role for the User
Role::register(auth()->user(), 'companies');
// Return (boolean):
true or false
// Revokes role
Role::revoke(auth()->user(), 'companies');
// Return (boolean):
true or false
$user = User::find(1);
// User's Roles
$user->listRoles();
// Return (array):
[
[
"id" => 3
"name" => "Companies"
"slug" => "companies"
],
[
"id" => 4
"name" => "Sales"
"slug" => "sales"
]
]
$user = User::find(1);
// Checking if the user has role
$user->hasRole('companies');
// Return (boolean):
true or false
$user = User::find(1);
// Grants role to the User
$user->registerRole('companies');
// Return (boolean):
true or false
$user = User::find(1);
// Revokes role
$user->revokeRole('companies');
// Return (boolean):
true or false
protected $routeMiddleware = [
...
'permissions' => Doc88\FluxRolePermission\Http\Middleware\FluxRolePermission::class,
...
]
// routes/web.php
Route::get('list-companies', 'CompanyController@index')->middleware('permissions:list-companies');
// or
Route::group(['middleware' => 'permissions:list-companies'], function () {
Route::get('list-companies', 'CompanyController@index');
});