bunthoeuntok / laravel-simple-permission
Laravel simple permission package
dev-master
2023-07-03 04:29 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.13.0
- staudenmeir/laravel-adjacency-list: ^1.12
Requires (Dev)
- laravel/pint: ^1.0
- orchestra/testbench: ^7.0
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2025-03-09 07:46:42 UTC
README
This package is developed to help laravel developer to implement siple role and permission.
Installation
You can install the package via composer:
composer require bunthoeuntok/laravel-simple-permission
You can publish config, migrations and middleware by running:
php artisan permission:install
Then migrate tables by running:
php artisan migrate
This is the contents of the published config file:
return [ // Menu level structure 'menu_levels' => [ 'module', 'sub-module', 'page' ], // Cache key 'cache_key' => 'permissions', // Menu structure to import 'data' => [ ... ] ];
Usage
Step 1
You need to use HasRole
trait in User.php
model
use Bunthoeuntok\SimplePermission\Traits\HasRole; class User extends Authenticatable { ... use HasRole; ... }
Step 2
Register PermissionMiddleware.php
in Kernel.php
protected $routeMiddleware = [ ... 'role.permission' => \App\Http\Middleware\PermissionMiddleware::class ];
Step 3
Use naming route middleware in route file.
- Note: When you defined a route you should provide it with
name
, which match to route_name in menu action.
// Index action Route::get('users', [UserController::class, 'index'])->middleware(['role.psermission', 'auth'])->name('users.index'); // Delete action Route::get('users/{user}', [UserController::class, 'destroy'])->middleware(['role.psermission', 'auth'])->name('users.destroy');
What we can do
- Create a role and assign user a role
// Create a role $role = Bunthoeuntok\SimplePermission\Models\Role::create([ 'role_name' => 'Admin', 'is_admin' => false; ]); // Create user $user = User::factory()->create(); // Assign role to a user $user->assignRole($role);
actions
- Create menu and its actions
$menu = Bunthoeuntok\SimplePermission\Models\Menu::create([ 'menu_name' => 'User', 'level' => 'page'; // base on menu_levels in simple-permissions.php config ]); // Create menu actions $menu->actions()->saveMany([ new Bunthoeuntok\SimplePermission\Models\Menu([ 'action_name' => 'index', 'route_name' => 'modules.root-pages.index', 'default' => true, ]), new Bunthoeuntok\SimplePermission\Models\Menu([ 'action_name' => 'delete', 'route_name' => 'modules.root-pages.delete', ]) ]);
- Or by running command
php artisan permission:import
, this command will import menu structure, which was setdata
key in simple-permission.php config.
return [ // Menu structure to import 'data' => [ [ 'menu_name' => 'User', 'level' => 'page', 'actions' => [ [ 'action_name' => 'index', 'route_name' => 'users.index', 'default' => true, ], [ 'action_name' => 'delete', 'route_name' => 'users.delete', ], ] ], ], ]
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
The MIT License (MIT). Please see License File for more information.