metko / metkontrol
Pakage permission
Requires
- php: >=7.0
- illuminate/auth: ~5.3.0|~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0
- illuminate/container: ~5.3.0|~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0
- illuminate/contracts: ~5.3.0|~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0
- illuminate/database: ~5.4.0|~5.5.0|~5.6.0|~5.7.0|~5.8.0
Requires (Dev)
- orchestra/testbench: ~3.4.2|~3.5.0|~3.6.0|~3.7.0
- phpunit/phpunit: ^5.7|6.2|^7.0
- predis/predis: ^1.1
This package is auto-updated.
Last update: 2025-01-09 14:41:51 UTC
README
Simple personal package to handle roles & permissions inside a Laravel app. Compatible with Laravel 5.8.
Installation
Via composer:
composer require metko/metkontrol
Deploy the config & migrations file
php artisan vendor:publish --provider="Metko\Metkontrol\MetkontrolServiceProvider"
And finaly make the migration
php artisan migrate
Optionally, you can specify the firsts roles and permissions you want in the config file. By default, it will use the following ones :
'roles' => ['Member', 'Author', 'Moderator', 'Admin', 'Super Admin'], 'permissions' => ['A permission', 'Second permission', 'third permission'],
Blockquote
Duplicate the seeder class file
php artisan vendor:publish --provider="Metko\Metkontrol\MetkontrolServiceProvider" --tag="seeds"
Run composer auto-load
composer dump-autoload
And seed!
php artisan db:seed --class=MetkontrolTableSeeder
Usage
Just add the traits to the model you want to have role and permissions. (Note that you can use only the Role if you want, but not the opposite).
use Metko\Metkontrol\MetkontrolRole, Metko\Metkontrol\MetkontrolPermission, Metko\Metkontrol\MetkontrolCache; // To update the cache when a model instance is created or updated
Roles
Attach a roles to a model
$role = Role::find(1); $model->attachRole($role); // Role class // or $model->attachRole(1); // Role ID $model->attachRole('Author') // Role Name or slug // You can also pass an array $model->attachRole([$role, 1, 'author']); // Or a piped string $model->attachRole("role-name|2|moderator|Super admin");
Remove roles
$role = Role::find(1); $model->removeRole($role); // or $model->removeRole(1); $model->removeRole('role name') // Name or slug // or $model->removeRole([$role, 1, 'role name']); $model->removeRole("1|Moderator"); // or $model->removeRole(); // Will remove all the role
Check if the model has a specific role
$role = Role::find(1); $model->attachRole($role); $model->hasRole($role); // True // or $model->hasRole(1); // True $model->hasRole("Author"); // True $model->hasRole("author"); // True $model->hasRole('wrong role name') // False
Check if the model has one of the role
$model->hasAnyRole('test|3|a-slugged-role');
Check if the model has all the given role
$model->hasAllRoles([$role, 3, "A new role"]);
Permissions
Give permissions to a moddel
$permission = Permission::find(1); $model->givePermissionTo($permission);
Like for the role, you can p.ass an ID, a name, a slug, an array of mixed elements (string, model, int) or a piped string
Remove permissions
$model->removePermissionTo('permission name') // Name or slug // or $model->removePermissionTo('permission1 name|permission2 slug'); $model->removePermissionTo([$permission1, $permission2]); $model->removePermissionTo(); // Will remove all the permission
Check if it has a specific permission
$model->hasAnyPermission($permission1) // Return bool
Or
$model->hasPermissionTo($permission1) // Return bool
If it has one of the permissions
$model->hasAnyPermission("permission1|another-one|4") // Return bool
If it has all the permission
$model->hasAllPermissions([$permission1, "second-permission"]) // Return bool
You can also check if the model have a specific permission directly from the permission model or the role model
$model->hasDirectPermission($permission1) // Return bool $model->hasDirectPermissionViaRole($permission1) // Return bool
Middlewares
You can use the middleware to check if the user has the given roles
Route::get('/', 'homeController@index')->middleware('hasRole:author'); Route::get('/', 'homeController@index')->middleware('hasRole:author|writer');
Or a permission
Route::get('/', 'homeController@index')->middleware('hasPermission:delete-account'); Route::get('/', 'homeController@index')->middleware('hasPermission:create-news|edit-blog');
Blades
You can use blade template to check if the user has a role
@role('author') // Only author can see it @elserole('member') // Only member can see it @endrole
You can use blade template to check if the user has a one of the roles
@hasanyrole('author|3') // you can also pass a array // Only author and role with ID to 3 can see it @endhasanyrole
You can use blade template to check if the user has all the role
@hasallrole('author|3') // you can also pass a array // Only author and role with ID to 3 can see it @endhasallrole
You can use blade template to check if the user has all the role
@unlessrole(1) // you can also pass a array // People without the role 1 can see this line @endunlessrole
Or a permission
Route::get('/', 'homeController@index')->middleware('hasPermission:delete-account'); Route::get('/', 'homeController@index')->middleware('hasPermission:create-news|edit-blog');
Or both
Route::get('/', 'homeController@index')->middleware('hasRoleOrPermission:delete-account'); Route::get('/', 'homeController@index')->middleware('hasRoleOrPermission:create-news|Author');
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email metko@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.