Download the PHP package radiatecode/laravel-permission-name-generator without Composer
On this page you can find all versions of the php package radiatecode/laravel-permission-name-generator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download radiatecode/laravel-permission-name-generator
More information about radiatecode/laravel-permission-name-generator
Files in radiatecode/laravel-permission-name-generator
Package laravel-permission-name-generator
Short Description Laravel package to generate permission names from route names
License MIT
Informations about the package laravel-permission-name-generator
Laravel Permission Name Generator
This package will generate permission names from route names or resources. In many application we create static permission names (ex: create-post, edit-post, delete-post) to check user's accessability, using the package can helps you to generate permission names dynamically. Permission names can be generate panel wise.
Upgrade Note: If you are upgrade to v0.4 then you have to upgrade the older permission-generator config file
Requirements
- PHP >= 7.1
- Laravel 5.7|6.x|7.x|8.x|9.x|10.x
- JQuery [for view]
-
Bootstrap [for view]
Installation
You can install the package via composer:
composer require radiatecode/laravel-permission-name-generator
Publish config file
php artisan vendor:publish --provider="RadiateCode\PermissionNameGenerator\PermissionNameServiceProvider" --tag="permission-generator-config"
Publish permission view files (optional)
php artisan vendor:publish --provider="RadiateCode\PermissionNameGenerator\PermissionNameServiceProvider" --tag="permission-views"
It can be usefull when you want to customise the permissions view
Usage
Here are some routes
Now get permissions names from that routes
RadiateCode\PermissionNameGenerator\Permissions::make()->fromRoutes()->get();
Output
PermissionGenerator trait [Optional]
From controller if you want to set prmission group title, excluded permissions or append permissions to another group then the trait becomes handy. How we use it, implement the WithPermissionGenerator contracts in the controller, then use the PermissionGenerator trait.
Available methods in PermissionGenerator trait
permissionsExclude(...$methods)
: use to exculde a route from being generated as permission name.permissionsTitle(string $title)
: Use to set permissions group titlepermissionsAppendTo(string $key)
: Permissions append to another permission group
Example
Output:
Permission Append Example:
Output:
PermissionGenerator trait is optional. Because if no permission group title defined, then this package dynamically generate a title based on controller name, routes can be excluded in the config.
Permission View Builder
The package comes with a view builder to show generated permission names in organised way.
Builder available methods:
make(string $view, array $data = [])
: set your view.withPermissions(array $permissions)
: pass generated permission names.markRolePermissions(string $roleName,arra $rolePermissions,string $permissionsSaveUrl = null)
: This method helps you to mark the stored role's permissions in view so that you can identify which permissions are belongs to the role, you can define a url where you can store/update the permissions of the rolerender()
: it render the view along with predefined data ($permissionCards, $permissionScripts), display these data in your blade. See the blade file example below
Submiting permissions can be get by
Example of permission view
In controller:
In app/role/permissions.blade.php file:
Permissions view
Create the permission savings route
Panel
Permissions could be generate for multiple panel, for example one application may have User panel and Admin panel, both may need different permissions. So we can achieve that by defining multiple panel, By default it is 'user' panel, assuming that all the permissions for user panel. But if you want different permissions for different panel you can generate that by defining another panel in the config.
Get the permission names for custom panel.
RadiateCode\PermissionNameGenerator\Permissions::make()->panel('your_custom_panel_name')->fromRoutes()->get();
Note: Panel only works for routes
Configuration
Config the config/permission-generator.php file.
1. Splitter
If route name contains any special char then split the name by that char. It will use to generate permission text. For example if route name is create.post then permission text would be Create Post
2. Custom Permissions
You can defined custom new permissions, add extra permissions to existing one. You can add custom-permission for custom panel.
Example
Note: notice the
user-permission
key which contains only permission name, if no text key pass the package dynamically make a text for the permission name. You can also add extra permissions to exisiting permission, for examplebonus-permission
is an exisitng permission, we add customapprove-own-department
extra permission to it.3. Permission Generate Controllers
This config play vital role to generate permissions because permissions will be generated only for defined controllers. Permission names will be generate, from only defined controller's route names. By Default permission names will be generated from all controller's routes for user panel. You can define custom panel and it's permission generate controllers. Controller could be fully qualified class name or namespace-prefix.
4. Exclude Routes
Exclude routes by controller. By default all auth controller's routes will be excluded from being generated as permission names for user panel. Controller could be fully qualified class name or namespace-prefix.
Or, we can exclude routes by route name
6. Cache Permissions
Caching the permission names
7. Permissions Section
Permissions can be grouped by section, example admin section, employee section, settings setion etc.
Samepe Output:
Alternatively generate Permissions
The package allows you to generate permission names by defining resource names.
Example
Wonder how it generate, the resource permission names generate using actions, which defined in the
config
file php use RadiateCode\PermissionNameGenerator\Permissions;
$permissions = Permissions::make()->fromResources([ 'users' => /extra actions/['delete-selected','active'], 'posts' // no extra actions ])->get();
// output [ 'users-permission' => [ [ 'name' => 'create-users', 'text' => 'Create Users' ], [ 'name' => 'edit-users', 'text' => 'Edit Users' ], [ 'name' => 'show-users', 'text' => 'Show Users' ], [ 'name' => 'delete-users', 'text' => 'Delete Users' ], [ 'name' => 'view-users', 'text' => 'View Users' ], [ // extra 'name' => 'delete-selected-users', 'text' => 'Delete Selected Users' ], [ // extra 'name' => 'active-users', 'text' => 'Active Users' ], ], 'posts-permission' => [ [ 'name' => 'create-posts', 'text' => 'Create Posts' ], [ 'name' => 'edit-posts', 'text' => 'Edit Posts' ], [ 'name' => 'show-posts', 'text' => 'Show Posts' ], [ 'name' => 'delete-posts', 'text' => 'Delete Posts' ], [ 'name' => 'view-posts', 'text' => 'View Posts' ], ], ]
### Permission View Builder
**Example**
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
## Security
If you discover any security related issues, please email [[email protected]](mailto:[email protected]) instead of using the issue tracker.
## Credits
- [Noor Alam](https://github.com/radiatecode)
- [All Contributors](https://github.com/radiatecode/laravel-route-permission/contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.