Laravel Roles and Permissions package.
$ composer require marksihor/laravel-permissions -vvv
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
To publish configs run the command:
$ php artisan vendor:publish --provider="MarksIhor\\LaravelPermissions\\PermissionsServiceProvider" --tag=configs
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')*/;
});
1 . Run migrations
php artisan migrate
- Add next line to $routeMiddleware array of your Kernel.php file
'permission' => \MarksIhor\LaravelPermissions\Http\Middleware\PermissionMiddleware::class,
- Add role relationship to your user model
public function role()
{
return $this->belongsTo('MarksIhor\LaravelPermissions\Models\Role');
}
- Use it on route like this
Route::get('/', 'UserController@index')->middleware('permission:view users');
- Or in other places like this
app('auth')->user()->role->hasPermission($permissionName); // bool
MIT