smorken / roles
Roles helper for Laravel
Requires
- php: ^8.1
- illuminate/auth: ^10.0|^11.0
- illuminate/console: ^10.0|^11.0
- illuminate/routing: ^10.0|^11.0
- illuminate/support: ^10.0|^11.0
- smorken/controller: ^1.0
- smorken/domain: ^1.0
Requires (Dev)
- fakerphp/faker: ^1.15.0
- larastan/larastan: ^v2.9.8
- laravel/browser-kit-testing: ^7.0
- mockery/mockery: ^1.0
- orchestra/testbench: ^8.0|^9.0
- phpunit/phpunit: ^10.0|^11.0
- smorken/components: ^1.0
- smorken/docker: *
- dev-master / 10.x-dev
- v10.4.3
- v10.4.2
- v10.4.1
- v10.4.0
- v10.3.3
- v10.3.2
- v10.3.1
- v10.3.0
- v10.2.6
- v10.2.5
- v10.2.4
- v10.2.3
- v10.2.2
- v10.2.1
- v10.2.0
- v10.1.5
- v10.1.4
- v10.1.3
- v10.1.2
- v10.1.1
- v10.1.0
- v10.0.2
- v10.0.1
- v10.0.0
- 9.x-dev
- v9.1.3
- v9.1.2
- v9.1.1
- v9.1
- v9.0
- 8.x-dev
- v8.0.1
- v8.0
- 6.x-dev
- v6.4.1
- v6.4
- v6.3
- v6.2
- v6.1
- v6.0
This package is auto-updated.
Last update: 2024-11-12 18:45:57 UTC
README
License
This software is open-sourced software licensed under the MIT license
The Laravel framework is open-sourced software licensed under the MIT license
Requirements
- PHP 7.2+
- Composer
Installation
Add to your Laravel app composer.json
"require": {
"smorken/roles": "^6.0"
}
$ composer update
The service provider should automatically register itself.
If not, add service provider to config/app.php
'providers' => [
...
\Smorken\Roles\ServiceProvider::class,
Publish the needed files (if you are planning to override them)
$ php artisan vendor:publish --provider="Smorken\Roles\ServiceProvider" --tag=views #config also available
Edit the config/roles.php
if you want to have different roles allowed initially.
Run the migrations (might need to dump-autoload again)
$ php artisan db:seed --class="RoleSeeder"
Use
This is super simple scaling set of roles. Any role with a higher level will have access to any role below it.
Authorization is handled via Gates. You can create your own or use the built-in static method in
Smorken\Roles\ServiceProvider
to add them to the AuthServiceProvider
\App\Providers\AuthServiceProvider
<?php
/**
* Register any authentication / authorization services.
*
* @return void
*/
public function boot()
{
// ...
$this->registerPolicies();
\Smorken\Roles\ServiceProvider::defineGates($this->app);
}
If you use the default method, each role will be assigned a gate named "role-[role code]".
The gates are available in blade files via the blade shortcut
@can('role-role-code')
eg. @can('role-super-admin')
.
Gates are available on routes:
<?php
Route::group([
'prefix' => 'admin',
'middleware' => ['auth', 'can:role-admin'],
'namespace' => 'Admin'
],
function () {
});
Gate facade:
<?php
//boolean check
if (Gate::allows('role-manage')) {
}
if (Gate::denies('role-super-admin')) {
}
if (Gate::forUser($user)->allows('role-admin')) {
}
//authorize check (throw AuthorizationException on failure)
Gate::authorize('role-manage');