You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[](https://packagist.org/packages/radiatecode/laravel-route-permission)
[](https://packagist.org/packages/radiatecode/laravel-permission-name-generator)
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)
6
-
7
-
## Example
8
-
### Generate permission view
9
-

10
-
11
-
**In controller:**
12
-
13
-
```php
14
-
class RoleController extends Controller
15
-
{
16
-
public function permissionsShow($id)
17
-
{
18
-
$role = Role::query()->findOrFail($id);
19
-
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
public function permissionStore(Request $request,$id)
47
-
{
48
-
$role = Role::find($id);
49
-
50
-
$role->role_permissions = json_encode($request->get('permissions')); // get the submitted permissions
51
-
$role->save();
52
-
53
-
return response()->json('success',201);
54
-
}
55
-
}
56
-
```
5
+
This package will generate permission names from routes. 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.
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()`.
27
+
While this package generate permission names from route names, in some cases we might need to exclude some routes so that it won't generate as permission names. To do so implement the **WithPermissionGenerator** contracts in the controller, then use the **PermissionGenerator** trait.
28
+
29
+
Available methods in **PermissionGenerator** trait
79
30
31
+
-`permissionExcludeMethods()` : use to exculde a route from being generated as permission name.
32
+
-`permissionGroupTitle()`: Use to set group title for permissions
33
+
34
+
**Example**
80
35
```php
81
36
use App\Http\Controllers\Controller;
82
37
use RadiateCode\LaravelRoutePermission\Contracts\WithPermissionGenerator;
@@ -88,121 +43,198 @@ class OfficeController extends Controller implements WithPermissionGenerator
->permissionExcludeMethods('index','listDatatable'); // when necessary exclude specific routes by the controller methods
46
+
$this->permissionGroupTitle('Office Crud Permission')->permissionExcludeMethods('index','listDatatable'); // index and listDatatable associate routes won't be generated as permission names
93
47
}
94
48
}
95
49
```
96
50
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
51
+
> **PermissionGenerator** 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.
100
52
101
-
You can get permissible routes And make your own permissions view in order to set role permissions.
> 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
61
## 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].
62
+
The package comes with predefined a view with permission names
113
63
114
-
See the above [example](#example)
64
+
[**PermissionViewBuilder** facade].
115
65
116
66
**Builder methods:**
117
67
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.
68
+
-`view(string $view, array $data = [])`: set your view, this method will render your view with two predefined keys (permissionCards, permissionScripts) and put those keys according to you view layout
69
+
70
+
-`withRolePermissions(string $roleName,arra $rolePermissions,string $permissionsSaveUrl = null)`: This method helps you to pre-checked the permissions of a role, in the 3rd arg you can define a url where you can save the role permissions
71
+
72
+
### Submiting permissions can be get by
121
73
122
-
> **Note:** When submit the permissions from predefined view to any post routes you need to get the permissions by
123
74
> ```php
124
75
> $request->get('permissions'); // array of permissions
public function permissionStore(Request $request,$id)
112
+
{
113
+
$role = Role::find($id);
114
+
115
+
$role->role_permissions = json_encode($request->get('permissions')); // get the submitted permissions
116
+
$role->save();
117
+
118
+
return response()->json('success',201);
119
+
}
120
+
}
121
+
```
122
+
123
+
**In app/role/permissions.blade.php file:**
124
+
```html
125
+
@extends('layouts.app')
126
+
127
+
@section('content')
128
+
<divclass="row">
129
+
<divclass="col-12">
130
+
<divclass="card card-dark">
131
+
<divclass="card-header">
132
+
<h3class="card-title">Role Permissions</h3>
133
+
</div>
134
+
<divclass="card-body">
135
+
<!-- the key generated by permission view builder of permission name generator -->
136
+
{!! $permissionCards !!}
137
+
</div>
138
+
</div>
139
+
</div>
140
+
</div>
141
+
@endsection
142
+
143
+
@push('js')
144
+
<!-- the key generated by permission view builder of permission name generator -->
145
+
{!! $permissionScripts !!}
146
+
@endpush
147
+
```
148
+
149
+
> The layout is only for demo purpose, you should only notice the `$permissionCards` and `$permissionScripts` variables, and put those acording to your view layout.
127
150
128
151
## Configuration
129
152
130
-
Config the **config/route-permission.php** file.
153
+
Config the **config/permission-generator.php** file.
131
154
132
-
Define the name of the permission middlewares. The package recognise the route as permissible route by these middlewares.
155
+
1. If route name contains any special char then split the the name by that char. It will use to generate route title. For example if route name is **create.post** then it's title would be **Create Post**
133
156
```php
134
157
/**
135
-
* Permission middlewares
136
-
*
137
-
* [nt: define the middlewares by which we applied permissions]
158
+
* Split route name by defined needle
138
159
*/
139
-
'permission-middlewares' => [
140
-
// 'auth', 'role-permissions'
141
-
],
160
+
'route-name-splitter-needle' => '.',
142
161
```
143
-
If route name contains any special char then split the the name by that char. It will use to generate route title. For example if route name is **create.designation** then it's title would be **Create Designation**
162
+
163
+
2. You can defined custom permissions
144
164
```php
145
165
/**
146
-
* Split route name by defined character
166
+
* Custom permissions
147
167
*/
148
-
'route-name-splitter' => '.',
168
+
'custom-permissions' => [
169
+
//
170
+
],
149
171
```
150
-
Exclude routes by route name. If we don't want to include any routes as permissible routes then we can exclude those in here.
3. Each route associate with controllers, so you have to define the controller namespace so that the generator can generate permission names from those route associate controllers. By default all the controllers associate routes will be generated as permission names.
181
+
151
182
```php
152
183
/**
153
-
* Exclude routes by route name
184
+
* Define controller namespace
185
+
*
186
+
* [NT: permissions will be generated from those controller which contains the defined prefix]
154
187
*/
155
-
'exclude-routes' => [
156
-
// route.name
188
+
'controller-namespace-prefixes' => [
189
+
'App\Http\Controllers',
157
190
],
158
191
```
159
-
Exclude routes by controller. If we want to exclude all routes of a controller then we can exclude it here
192
+
4.Exclude routes by controller. If we want to exclude routes of a controller then we can exclude it. By default all auth related routes will be excluded from being generated as permission names.
160
193
161
194
```php
162
-
/**
163
-
* Exclude routes by controller
195
+
/**
196
+
* Exclude routes by controller or controller namespace-prefix
164
197
*
165
-
* [NT: We can exclude routes by controllers. All the routes associated with controller will be excluded]
198
+
* [NT: We can exclude routes by defining controller name or namespace-prefix. All the routes associated with controller will be excluded]
166
199
*/
167
-
'exclude-controllers' => [
168
-
/**
169
-
* exclude every route which associate with WelcomeController
170
-
*/
171
-
// WelcomeController::class
200
+
'exclude-controllers' => [
201
+
/*
202
+
* exclude every route which associate with the prefix namespace
203
+
*/
204
+
'App\Http\Controllers\Auth',
172
205
],
173
206
```
174
-
Caching the permissible routes
207
+
208
+
5.**Or,** we can exclude routes by route name
209
+
175
210
```php
176
-
/**
177
-
* Cache the permissible routes
211
+
/**
212
+
* Exclude routes by route name
178
213
*/
179
-
'cache-routes' => [
180
-
'cacheable' => true,
181
-
'cache-driver' => env('CACHE_DRIVER', 'file')
214
+
'exclude-routes' => [
215
+
// route.name
182
216
],
183
217
```
184
-
Buttons generate in the permission view
218
+
219
+
6. Caching the permission names
220
+
185
221
```php
186
222
/**
187
-
* permission button used to checked / unchecked all routes
'<buttontype="button"class="btn btn-warning"onclick="uncheckAll()"><iclass="fa fa-square"></i> Uncheck All </button>',
195
-
'<buttontype="button"class="btn btn-success save-btn"onclick="saveRolePermissions()"title="save role permission"><iclass="save-loader fa fa-save"></i> Save </button>',
225
+
'cache-permissions' => [
226
+
'cacheable' => true,
227
+
'cache-driver' => env('CACHE_DRIVER', 'file'),
196
228
],
197
229
```
198
-
Permission card size (bootstrap grid)
230
+
7.Permission card size (bootstrap grid)
199
231
```php
200
232
/**
201
233
* Permission card size
202
234
*
203
-
* [nt: permissible card only works on bootstrap]
235
+
* [NT: Predefined permission cards works on bootstrap]
0 commit comments