jefremassingue/spatie-permission-generate

v1.0.0 2023-04-01 20:14 UTC

This package is auto-updated.

Last update: 2024-05-10 23:50:28 UTC


README

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.

Installation

You can install the package via composer:

composer require jefremassingue/spatie-permission-generate

Usage

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');
    

Instalation in Laravel

// 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'
KeyDescriptionExample
SPG_CONTROLLERS_ROOT_PATHThe root path where the controllers are located. You have to use `/`app/Http/Controllers
SPG_IGNORE_CLASSES_FILESThe classes that should be ignored (separated by comma). You have to use `\`'Controller,Admin\PermissionGeneratorController,Helper\Upload'
SPG_CONTROLLER_CLASSES_SUFFIXThe suffixes for the controller classes.'_controller,Controller'
SPG_IGNORE_METHODS_AND_FUNCTIONSThe methods and functions that should be ignored (separated by comma).'__construct,pay'
SPG_DEFAULT_GUARDThe default guard.'web'

Testing

To run the tests, execute the following command:

composer test

Example

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

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 jefremassingue.jm@gmail.com or albinomateve@gmail.com instead of using the issue tracker.

Credits

License

The MIT License (MIT). Please see License File for more information.

PHP Package Boilerplate

This package was generated using the PHP Package Boilerplate.