marksihor/laravel-permissions

Laravel permissions.

dev-master 2021-09-13 09:08 UTC

This package is auto-updated.

Last update: 2021-10-13 09:25:36 UTC


README

Laravel Roles and Permissions package.

Installing

$ composer require marksihor/laravel-permissions -vvv

Migrations

This step is optional, if you want to customize the tables, you can publish the migration files:

$ php artisan vendor:publish --provider="MarksIhor\\LaravelPermissions\\PermissionsServiceProvider" --tag=migrations

Configs

To publish configs run the command:

$ php artisan vendor:publish --provider="MarksIhor\\LaravelPermissions\\PermissionsServiceProvider" --tag=configs

Controllers

This step is optional, if you want to get crud controller (instead of use built in routes) run the command:

$ php artisan vendor:publish --provider="MarksIhor\\LaravelPermissions\\PermissionsServiceProvider" --tag=controllers

If You published the controller, add the routes to your routes file:

Route::group(['as' => 'roles', 'prefix' => 'roles'/*, 'middleware' => 'permission:view roles'*/], function () {
    Route::get('/', 'RoleController@index');
    Route::get('/{model}', 'RoleController@show');
    Route::get('/{model}/permissions', 'RoleController@permissions')/*->middleware('permission:view roles')*/;
    Route::post('/', 'RoleController@store')/*->middleware('permission:create roles')*/;
    Route::patch('/{model}', 'RoleController@update')/*->middleware('permission:update roles')*/;
    Route::delete('/{model}', 'RoleController@delete')/*->middleware('permission:delete roles')*/;
    Route::patch('/{model}/permissions/{action}', 'RoleController@updatePermissions')
        ->where('action', '(attach|detach|update)')
        /*->middleware('permission:update roles')*/;
});

Usage

1 . Run migrations

php artisan migrate
  1. Add next line to $routeMiddleware array of your Kernel.php file
    'permission' => \MarksIhor\LaravelPermissions\Http\Middleware\PermissionMiddleware::class,
  1. Add role relationship to your user model
public function role()
{
    return $this->belongsTo('MarksIhor\LaravelPermissions\Models\Role');
}
  1. Use it on route like this
Route::get('/', 'UserController@index')->middleware('permission:view users');
  1. Or in other places like this
app('auth')->user()->role->hasPermission($permissionName); // bool

License

MIT