Skip to content

Commit f6e9f90

Browse files
committed
read me updated
1 parent f7fa996 commit f6e9f90

File tree

1 file changed

+74
-67
lines changed

1 file changed

+74
-67
lines changed

README.md

Lines changed: 74 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,43 @@
1-
# Laravel Route Permission
1+
# Laravel Permission Name Generator
22
[![Latest Version on Packagist](https://img.shields.io/packagist/v/radiatecode/laravel-route-permission.svg?style=flat-square)](https://packagist.org/packages/radiatecode/laravel-route-permission)
33
[![Total Downloads](https://img.shields.io/packagist/dt/radiatecode/laravel-route-permission.svg?style=flat-square)](https://packagist.org/packages/radiatecode/laravel-route-permission)
44

5-
In role-permission base authorization we generally add permissions to a db table, then assign the permissions to a user or a role.
6-
This package will generate permissions from route names, so no need for a permission db table. we can assign these permissions to a user, or a particular role
7-
to authorize user actions. There is a pre-built generator to generate role base permission view.
5+
This package will generate permission names from route names. Permissions are grouped by controller name. These permission names can be usefull for authorization (role-permission base system)
86

97
## Example
108
### Generate permission view
119
![Stats](img/permission-view.png)
1210

11+
**In controller:**
12+
1313
```php
1414
class RoleController extends Controller
1515
{
16-
public function permissions($id)
16+
public function permissionsShow($id)
1717
{
18-
$role = Role::find($id);
18+
$role = Role::query()->findOrFail($id);
1919

20-
return view('app.role.permission')
21-
->with('permissions',PermissionViewBuilder::withRolePermissions($role->role_name,json_decode($role->role_permissions))->permissionView())
22-
->with('permission_scripts',PermissionViewBuilder::permissionScripts(route('role.permissions',$id)));
20+
return PermissionsView::withRolePermissions(
21+
$role->role_name,
22+
json_decode($role->role_permissions), // assume role permissions stored as json encoded
23+
route('create-role-permission', $role->id) // permission save url for a role
24+
)->view('app.role.permissions');
2325
}
2426
}
2527
```
2628
**In view (blade) file:**
2729
```html
2830
<div class="permissions">
29-
{!! $permissions !!}
31+
{!! $permissionCards !!}
3032
</div>
3133

3234
......
3335
<!-- generate scripts -->
34-
{!! $permission_scripts !!}
36+
{!! $permissionScripts !!}
3537
```
3638
**Saving permissions for a role:**
3739
```php
38-
Route::post('/role/permissions/{id}',[RoleController::class,'permissionStore'])->name('role.permissions');
40+
Route::post('/role/{id}/permissions/create',[RoleController::class,'permissionStore'])->name('create-role-permission');
3941
```
4042
```php
4143
use \Illuminate\Http\Request;
@@ -44,7 +46,8 @@ class RoleController extends Controller
4446
public function permissionStore(Request $request,$id)
4547
{
4648
$role = Role::find($id);
47-
$role->role_accesses = json_encode($request->get('permissions')); // get the submitted permissions
49+
50+
$role->role_permissions = json_encode($request->get('permissions')); // get the submitted permissions
4851
$role->save();
4952

5053
return response()->json('success',201);
@@ -59,13 +62,69 @@ class RoleController extends Controller
5962
# Installation
6063
You can install the package via composer:
6164

62-
composer require radiatecode/laravel-route-permission
65+
composer require radiatecode/laravel-permission-name-generator
6366

6467
Publish config file
6568

66-
php artisan vendor:publish --provider="RadiateCode\LaravelRoutePermission\PermissionServiceProvider" --tag="route-permission-config"
69+
php artisan vendor:publish --provider="RadiateCode\PermissionNameGenerator\PermissionNameServiceProvider" --tag="permission-generator-config"
70+
71+
Publish default permission view files (optional)
72+
73+
php artisan vendor:publish --provider="RadiateCode\PermissionNameGenerator\PermissionNameServiceProvider"
6774

6875
# Usage
76+
77+
## PermissionGenerator trait [Optional]
78+
While this package generate permission names from route names, in some cases we might need to exclude some permission names. To do so implement the **WithPermissionGenerator** contracts in the controller, then use the **PermissionGenerator** trait. We can use `permissionExcludeMethods()` to exclude permissions by route associative method. We can also define permission group name `permissionGroupTitle()`.
79+
80+
```php
81+
use App\Http\Controllers\Controller;
82+
use RadiateCode\LaravelRoutePermission\Contracts\WithPermissionGenerator;
83+
use RadiateCode\LaravelRoutePermission\Traits\PermissionGenerator;
84+
85+
class OfficeController extends Controller implements WithPermissionGenerator
86+
{
87+
use PermissionGenerator;
88+
89+
public function __construct()
90+
{
91+
$this->permissionGroupTitle('Office Crud Permission')
92+
->permissionExcludeMethods('index','listDatatable'); // when necessary exclude specific routes by the controller methods
93+
}
94+
}
95+
```
96+
97+
> **PermissionGenerator** trait is optional. Because if no permissible title defined, then this package dynamically generate a title based on controller name, And routes/permissions can be excluded in the config file.
98+
99+
## Get Permissions
100+
101+
You can get permissible routes And make your own permissions view in order to set role permissions.
102+
103+
RadiateCode\PermissionNameGenerator\Permissions::make()->get();
104+
105+
**Output**
106+
107+
![Stats](img/permissible-routes-output.png)
108+
109+
> Under the hood it gets all the routes which registered in **web.php** and only take those routes which has permission middleware. The permissible routes grouped by controller.
110+
111+
## Permission View Builder Facade
112+
If you don't want to make permission view by your own, then you can use predefined permissions view [**PermissionViewBuilder** facade].
113+
114+
See the above [example](#example)
115+
116+
**Builder methods:**
117+
118+
- permissionView() : generate bootstrap permissions card based on permissible routes, and config defined action buttons.
119+
- withRolePermissions($roleName,$rolePermissions) : it is used to select all the permissions that have access to a particular role.
120+
- permissionScripts($url = null) : generate functions for check all and uncheck all buttons. The **$url** param used to submit the selected permissions for specific role.
121+
122+
> **Note:** When submit the permissions from predefined view to any post routes you need to get the permissions by
123+
> ```php
124+
> $request->get('permissions'); // array of permissions
125+
> ```
126+
127+
69128
## Configuration
70129
71130
Config the **config/route-permission.php** file.
@@ -145,58 +204,6 @@ Permission card size (bootstrap grid)
145204
*/
146205
'card-size-class' => 'col-md-3 col-lg-3 col-sm-12',
147206
```
148-
149-
## Permissible trait [Optional]
150-
Controller basis we can define permission title, exclude routes by methods. First implement the **WithPermissible**
151-
Interface in a controller, then use the **Permissible** trait.
152-
153-
```php
154-
use App\Http\Controllers\Controller;
155-
use RadiateCode\LaravelRoutePermission\Contracts\WithPermissionGenerator;
156-
use RadiateCode\LaravelRoutePermission\Traits\PermissionGenerator;
157-
158-
class OfficeController extends Controller implements WithPermissionGenerator
159-
{
160-
use PermissionGenerator;
161-
162-
public function __construct()
163-
{
164-
$this->permissibleTitle('Office Crud Permission')
165-
->permissionExcludeMethods('index','listDatatable'); // when necessary exclude specific routes by the controller methods
166-
}
167-
}
168-
```
169-
170-
> Permissible trait is optional. Because if no permissible title defined, then this package dynamically generate a title based on controller name, And routes can be excluded in the config file.
171-
172-
## Permissible routes
173-
174-
You can get permissible routes And make your own permissions view in order to set role permissions.
175-
176-
RadiateCode\LaravelRoutePermission\PermissibleRoutes::getRoutes
177-
178-
**Output**
179-
180-
![Stats](img/permissible-routes-output.png)
181-
182-
> Under the hood it gets all the routes which registered in **web.php** and only take those routes which has permission middleware. The permissible routes grouped by controller.
183-
184-
## Permission View Builder Facade
185-
If you don't want to make permission view by your own, then you can use predefined permissions view [**PermissionViewBuilder** facade].
186-
187-
See the above [example](#example)
188-
189-
**Builder methods:**
190-
191-
- permissionView() : generate bootstrap permissions card based on permissible routes, and config defined action buttons.
192-
- withRolePermissions($roleName,$rolePermissions) : it is used to select all the permissions that have access to a particular role.
193-
- permissionScripts($url = null) : generate functions for check all and uncheck all buttons. The **$url** param used to submit the selected permissions for specific role.
194-
195-
> **Note:** When submit the permissions from predefined view to any post routes you need to get the permissions by
196-
> ```php
197-
> $request->get('permissions'); // array of permissions
198-
> ```
199-
200207
## Contributing
201208
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
202209

0 commit comments

Comments
 (0)