Generate basic permissions for spatie permissions package for all your models at once using one artisan command.
This package contains a GeneratePermissionsProvider
that you can use in your packages to easily register the config file and the artisan command.
If you are using spatie permissions package, you need to create permissions for roles manually, common permissions for all models are access (list), create, edit and delete
This package will create all the basic permissions for all models using only one artisan command.
php artisan permissions:generate
If you have the following models [Category, Product] as example, after running the generate command, the following rows will be inserted in your permissions table
list category
create category
edit category
delete category
list product
create product
edit product
delete product
Note: You can modify some values inside the config file, check Working with config file section below.
composer require zakariajawas/permissions-generator
You should publish the config/permissionsgenerator.php config file with:
php artisan vendor:publish --provider="ZakariaJawas\PermissionsGenerator\GeneratePermissionsProvider"
In config/permissionsgenerator.php file you can modify the following values.
- Modify this value to add, update or delete a permission.
These are the basic permissions.
'permissions' => ['list', 'create', 'edit', 'delete']
For example to add access permission and change edit to update you can do this.
'permissions' => ['access', 'list', 'create', 'update', 'delete']
- By default the package will generate the permissions for all project models, you might want to exclude specific one or more models so you can do this.
'exclude' => [
App\Models\Session::class, //no permissions generated for Session model
],
- If you have static permissions which are not related to models for example export to pdf or access all data or maybe a page which doesn't have a model you can add these permisisons in $staticPermissions array
'staticPermissions' => ['export pdf', 'access dashboard']
Note:-
- You have to add the full model class path.
- You don't have to exclude other packages models, this package won't create permissions for them.
- Default permissions name is
permission + model name
, if you want to add a prefix you can specify it here.
'prefix' => 'can',
Result will be can create product
instead of create product
The MIT License (MIT). Please see License File for more information.
Zakaria Jawas @zakariajawas
If you spot a problem you can open an issue on the Github page, or alternatively, you can contact me via [email protected]
Support the library by twitting in
If you enjoy it, please make this library better 👍