Spatie Permission Generate is a package that generates permissions for spatie/laravel-permission based on the directory, controller class name, and methods.
For instance, Admin\UserController with index method => will have the permission admin-user-index
.
You can install the package via composer:
composer require jefremassingue/spatie-permission-generate
Before using the package, you need to install and configure spatie/laravel-permission (https://github.com/spatie/laravel-permission).
// Run synchronizelPermission method to generate permissions
$hasGenarate = \Jefre\SpatiePermissionGenerate\SpatiePermissionGenerate::synchronizelPermission();
// Change the guard name for permissions with a given prefix
$numPermissionsUpdated = \Jefre\SpatiePermissionGenerate\SpatiePermissionGenerate::changeGuardWithPrefix('api-', 'api');
// Set the following keys in your `.env` file:
SPG_CONTROLLERS_ROOT_PATH='app/Http/Controllers'
SPG_IGNORE_CLASSES_FILES='Controller,Helper\Upload, Other classes you want ignore'
SPG_CONTROLLER_CLASSES_SUFFIX='Controller,_controller'
SPG_IGNORE_METHODS_AND_FUNCTIONS='__construct'
SPG_DEFAULT_GUARD='web'
Key | Description | Example |
---|---|---|
SPG_CONTROLLERS_ROOT_PATH | The root path where the controllers are located. You have to use `/` | app/Http/Controllers |
SPG_IGNORE_CLASSES_FILES | The classes that should be ignored (separated by comma). You have to use `\` | 'Controller,Admin\PermissionGeneratorController,Helper\Upload' |
SPG_CONTROLLER_CLASSES_SUFFIX | The suffixes for the controller classes. | '_controller,Controller' |
SPG_IGNORE_METHODS_AND_FUNCTIONS | The methods and functions that should be ignored (separated by comma). | '__construct,pay' |
SPG_DEFAULT_GUARD | The default guard. | 'web' |
To run the tests, execute the following command:
composer test
Consider a file located in /app/Http/Controllers/API/UserController.php
:
<?php
namespace App\Http\Controllers\API;
...
class UserController extends Controller
{
public function __construct() {}
public function index(){}
public function create(){}
public function store(Request $request){}
public function show($id){}
public function edit($id){}
public function update(Request $request, $id){}
public function destroy($id){}
}
The generated permissions would be:
api-user-index
api-user-create
api-user-store
api-user-show
api-user-edit
api-user-update
api-user-destroy
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email jefremassingue.jm@gmail.com or albinomateve@gmail.com instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the PHP Package Boilerplate.