|
1 | | -# laravel-permission |
2 | | -An easy and flexible laravel authorization and roles permission management |
| 1 | +# Laravel permission |
| 2 | +An easy and flexible Laravel authorization and roles permission management |
3 | 3 |
|
4 | | -most of this work is based on this article: |
5 | | -https://medium.com/swlh/laravel-authorization-and-roles-permission-management-6d8f2043ea20 |
| 4 | +## Why we need a Laravel permission package |
| 5 | + |
| 6 | + |
| 7 | +## How to use |
| 8 | +The idea is to use this package as easy and as flexible as possible. |
| 9 | +<br> |
| 10 | +This package creates a list of all your routes and assigns these permissions list to user roles. |
| 11 | +<br> |
| 12 | +Although the `laravel-permission` package does most of the work, you could easily extend it and implement your authorization system. |
| 13 | + |
| 14 | + |
| 15 | +### Installation |
| 16 | +Start with installing the package through the composer. Run this command in your terminal: |
| 17 | +``` |
| 18 | +$ composer install amir/laravel-permission |
| 19 | +``` |
| 20 | + |
| 21 | +After that, you need to run the migration files: |
| 22 | +``` |
| 23 | +$ php artisan migrate |
| 24 | +``` |
| 25 | + |
| 26 | +### How to authorize user |
| 27 | +This package adds a `role_id` to the `users` table. |
| 28 | +Roles are stored in the `roles` table. You can assign a role to a user in your administrator panel or by creating a seed file. |
| 29 | +<br> |
| 30 | +Then, you only need to assign `auth.role` middleware to your routes. |
| 31 | + |
| 32 | +### Assign a route to a role |
| 33 | +Besides middleware and other route settings, you can use a `role` key in your route groups to assign a role to your routes. |
| 34 | +<br> |
| 35 | +You can put your routes for a role in a `Route` group like this: |
| 36 | +```php |
| 37 | +Route::group([ |
| 38 | + 'middleware' => 'auth.role', |
| 39 | + 'prefix' => ..., |
| 40 | + 'role' => 'admin', |
| 41 | + ... |
| 42 | +],function (){ |
| 43 | + ... |
| 44 | + Route::get('/home', 'HomeController@index')->name('home'); |
| 45 | + Route::get('/product', 'ProductController@index'); |
| 46 | + ... |
| 47 | +}); |
| 48 | +``` |
| 49 | +Of course, you can have as many as route groups like this. |
| 50 | +<br> |
| 51 | +Then you need to run this artisan command to register all permissions: |
| 52 | +``` |
| 53 | +$ php artisan permissions:generate |
| 54 | +``` |
| 55 | +This command will register all permissions and assign permissions to the roles. |
| 56 | +<br> |
| 57 | +If you add a `fresh` option to this command, it will delete all data and generate fresh permissions data: |
| 58 | +``` |
| 59 | +$ php artisan permissions:generate --fresh |
| 60 | +``` |
| 61 | +<br> |
| 62 | +Now only users with the proper role can access the route assigned to them. |
| 63 | +<br> |
| 64 | +Don't forget that this package does not handle assigning roles to the users. You need to handle this in your administration panel or anywhere else you handle your users. |
| 65 | +<br> |
| 66 | +Again, if you want to add permissions to the routes manually it is not necessary to add `role` key in your route group. You can easily assign permissions to the roles in your administration panel or create another seed file for that. |
| 67 | + |
| 68 | +### How to create roles |
| 69 | +The `php artisan permissions:generate` command will make all roles defined in the routes if they are not exist. |
| 70 | +<br> |
| 71 | +Also, You can create a seeder to fill the `roles` table. It takes only a `name` field. |
| 72 | +<br> |
| 73 | +Your `RolesSeeser` file can look like this. |
| 74 | +```php |
| 75 | +Role::firstOrCreate(['name' => 'admin']); |
| 76 | +Role::firstOrCreate(['name' => 'customer']); |
| 77 | +``` |
| 78 | +Don't forget to import the `Role` model in your seeder. |
| 79 | +```php |
| 80 | +use Amir\Permission\Models\Role; |
| 81 | +``` |
| 82 | + |
| 83 | +### How to clear permissions |
| 84 | +To clear registered permissions you can run this command: |
| 85 | +``` |
| 86 | +$ php artisan permissions:clear |
| 87 | +``` |
| 88 | + |
| 89 | +You can use this command to clear all permissions data for a specific role |
| 90 | +``` |
| 91 | +$ php artisan permissions:clear --roles role1 role2 |
| 92 | +``` |
| 93 | + |
| 94 | +To erase only permissions list, run `permissions:clear` command with this option: |
| 95 | +``` |
| 96 | +$ php artisan permissions:clear --tables permissions |
| 97 | +``` |
| 98 | + |
| 99 | +To clear all roles: |
| 100 | +``` |
| 101 | +$ php artisan permissions:clear --tables roles |
| 102 | +``` |
| 103 | + |
| 104 | +To clear only permissions role relation: |
| 105 | +``` |
| 106 | +$ php artisan permissions:clear --tables permission_role |
| 107 | +``` |
| 108 | +This command erases all permissions assigned to roles, so you can regenerate permissions |
| 109 | + |
| 110 | +Also, you can use these options in combination: |
| 111 | +``` |
| 112 | +$ php artisan permissions:clear --roles admin --tables permission_role |
| 113 | +``` |
| 114 | + |
| 115 | + |
| 116 | +## About |
| 117 | +I used this Laravel permission management method in my projects for a while. It made manging Laravel permission easy and flexible for me. I hope it helps you as well. All pull requests are welcome. |
| 118 | +<br> |
| 119 | +Most of the work is based on my article about [Laravel authorization and roles permission management](https://medium.com/swlh/laravel-authorization-and-roles-permission-management-6d8f2043ea20 |
| 120 | +) |
0 commit comments