PHP code example of 4msar / laravel-role-manager

1. Go to this page and download the library: Download 4msar/laravel-role-manager 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/ */

    

4msar / laravel-role-manager example snippets


'providers' => [
    // ...
    MSAR\RoleManager\RoleManagerServiceProvider::class,
];

return [

    /**
    * Permission List 
    * Here, you can declare your needed permission, the package will be add this permission automatically
    * to the database( via middle-ware).
    * key name is the permission name and value is the details name showing for management.
    */
    "add_permission_dynamically" => false, // Make true if you want to add permission from bellow permissions array
    "permissions" => [
        // 'permission_name' => 'permission_details'
        'view_home' => 'View Admin Dashboard',
        'view_users' => 'View Admin Users',
    ],

    'unauthorized_action' => '',

    'unauthorized_action_type' =>  [
        'route' => [
            'name' => 'welcome',
            'data' => [],
        ],
        'url' => [
            'name' => '/login',
            'data' => [],
        ],
        'abort' => [
            'type' => '403',
        ],
        'dump' => [
            'data' => 'Dumping a unauthorized message.',
        ]
    ],

    "database" => [
        "role_table" => "roles",
        "permission_table" => "permissions",
        "role_permission_table" => "permission_role",
    ],
];

use Illuminate\Foundation\Auth\User as Authenticatable;
use MSAR\RoleManager\Traits\HasPermission;;

class User extends Authenticatable
{
    use HasPermission;

    // ...
}

// .....
'has_permission' => \MSAR\RoleManager\Middlewares\RoleMiddleware::class,

use MSAR\RoleManager\Models\Role;
use MSAR\RoleManager\Models\Permission;

$role = Role::create(['name' => 'writer']);
$permission = Permission::create(['name' => 'edit_articles', 'title' => 'Edit Articles']);

$role = \MSAR\RoleManager\Models\Role::where('name', 'Admin')->first();
$permissions = $request->permissions; // [1,2] | checkbox array input | permissions ids
$role->permissions()->sync($permissions);

Route::get('/users', 'UserController@index')->middleware('has_permission:view_users')->name('home');

@hasrole(role)
@elsehasrole(another_role)
@endhasrole

@ucan(permission)
@elseucan(another_permission)
@enducan

rmHasRole($role); // pipe string | array | string
rmHasPermission($permission); // pipe string | array | string
bash
php artisan vendor:publish --provider="MSAR\RoleManager\RoleManagerServiceProvider"
bash
php artisan migrate