Skip to content

Commit 5dd7039

Browse files
author
Nur Alam
committed
permission generate by panel wise, permission cache bug fixed, read me updated
1 parent df6c7b8 commit 5dd7039

File tree

4 files changed

+200
-99
lines changed

4 files changed

+200
-99
lines changed

README.md

Lines changed: 98 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ The package comes with a view builder to show generated permission names in orga
9393

9494
- `make(string $view, array $data = [])`: set your view.
9595
- `withPermissions(array $permissions)`: pass generated permission names.
96-
- `markRolePermissions(string $roleName,arra $rolePermissions,string $permissionsSaveUrl = null)`: This method helps you to mark the stored role's permissions, you can define a url where you can store/update the permissions of the role
96+
- `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 role
9797
- `render()`: it render the view along with predefined data ($permissionCards, $permissionScripts), display these data in your blade. See the blade file example below
9898

9999
### Submiting permissions can be get by
@@ -179,6 +179,14 @@ class RoleController extends Controller
179179
}
180180
}
181181
```
182+
## Panel
183+
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.
184+
185+
Get the permission names for custom panel.
186+
187+
RadiateCode\PermissionNameGenerator\Permissions::make()->panel('your_custom_panel_name')->fromRoutes()->get();
188+
189+
> Note: Panel only works for routes
182190
183191
## Configuration
184192

@@ -187,94 +195,107 @@ Config the **config/permission-generator.php** file.
187195
### 1. Splitter
188196
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**
189197
```php
190-
/**
191-
* Split route name by defined needle
192-
*/
193198
'route-name-splitter-needle' => '.',
194199
```
195200
### 2. Custom Permissions
196-
You can defined custom new permissions, add extra permissions to existing one.
201+
You can defined custom new permissions, add extra permissions to existing one. You can add custom-permission for custom panel.
197202
```php
198-
/**
199-
* Custom permissions
200-
*/
201203
'custom-permissions' => [
202-
//
204+
'panels' => [
205+
'user' => [
206+
// custom-permissions
207+
],
208+
// 'admin' => [
209+
// // custom-permission
210+
//],
211+
]
203212
],
204213
```
205214
> Example
206-
> ```php
207-
> 'custom-permissions' = [
208-
> 'user-permission' => ['active-user','inactive-user'],
209-
> 'bonus-permission' => [
210-
> [
211-
> 'name' => 'approve-own-department',
212-
> 'text' => 'Approve Own Department'
213-
> ],
214-
> ]
215-
> ]
216-
>
217-
>```
218-
> 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 example `bonus-permission` is an exisitng permission, we add custom `approve-own-department` extra permission to it.
215+
```php
216+
'custom-permissions' = [
217+
'panels' => [
218+
'user' => [
219+
'user-permission' => ['active-user','inactive-user'],
220+
'bonus-permission' => [
221+
[
222+
'name' => 'approve-own-department',
223+
'text' => 'Approve Own Department'
224+
],
225+
]
226+
],
227+
// 'admin' => [
228+
// // custom-permission
229+
//],
230+
]
231+
]
232+
```
233+
> 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 example `bonus-permission` is an exisitng permission, we add custom `approve-own-department` extra permission to it.
219234
>
220235
### 3. Permission Generate Controllers
221-
From which controller's routes permission names will be generate, define it here. This config play vital role to generate permissions because permissions will be generated only for defined controllers.
236+
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.
237+
**Controller could be fully qualified class name or namespace-prefix.**
222238

223239
```php
224-
/**
225-
* Permission generate controller's namespace
226-
*
227-
* By Default permission names will be generated from all controller's routes
228-
*/
229240
'permission-generate-controllers' => [
230-
'App\Http\Controllers',
231-
232-
// sample
233-
// 'App\Http\Controllers\Api',
234-
// App\Http\Controllers\DepartmentController::class,
235-
// App\Http\Controllers\DesignationController::class,
241+
'panels' => [
242+
'user' => [
243+
'App\Http\Controllers',
244+
245+
// sample
246+
// 'App\Http\Controllers\Api',
247+
// App\Http\Controllers\DepartmentController::class,
248+
// App\Http\Controllers\DesignationController::class,
249+
],
250+
// 'admin' => [
251+
// 'App\Http\Controllers\Admin',
252+
//],
253+
]
236254
],
237255
```
238256
### 4. Exclude Routes
239-
Exclude routes by defining controller namespace. Here auth and EmployeeProfileController related routes will be excluded from being generated as permission names.
240-
257+
Exclude routes by controller. By default all auth controller's routes will be excluded from being generated as permission names for user panel.
258+
**Controller could be fully qualified class name or namespace-prefix.**
241259
```php
242-
/**
243-
* Exclude routes by controller's namespace
244-
*/
245260
'exclude-controllers' => [
246-
'App\Http\Controllers\Auth', // exclude routes of all auth controllers
247-
248-
// sample
249-
App\Http\Controller\Employee\EmployeeProfileController::class, // exclude routes of EmployeeProfileController
261+
'panels' => [
262+
'user' => [
263+
'App\Http\Controllers\Auth', // exclude routes of all auth controllers
264+
// App\Http\Controller\Employee\EmployeeProfileController::class,
265+
],
266+
// 'admin' => [
267+
// 'App\Http\Controllers\Admin\Auth',
268+
//],
269+
]
250270
],
251271
```
252272

253273
**Or,** we can exclude routes by route name
254274

255275
```php
256-
/**
257-
* Exclude routes by route name
258-
*/
259-
'exclude-routes' => [
260-
// sample
261-
'register.user',
262-
'employee.profile',
263-
....
264-
....
276+
'exclude-routes' => [
277+
'panels' => [
278+
'user' => [
279+
// sample
280+
'register.user',
281+
'employee.profile',
282+
....
283+
....
284+
],
285+
// 'admin' => [
286+
// route.name,
287+
//],
288+
]
265289
],
266290
```
267291
### 6. Cache Permissions
268292
Caching the permission names
269293

270294
```php
271-
/**
272-
* Cache the rendered permission names
273-
*/
274-
'cache-permissions' => true,
295+
'cache-permissions' => true,
275296
```
276297
### 7. Permissions Section
277-
Permissions can be organised by section, example admin section, employee section, settings setion etc.
298+
Permissions can be grouped by section, example admin section, employee section, settings setion etc.
278299
```php
279300
/**
280301
* Parmissions can be organised by section (ex: adminland, settings, employee managment etc)
@@ -294,19 +315,26 @@ Permissions can be organised by section, example admin section, employee section
294315
* ]
295316
*/
296317
'permissions-section' => [
297-
// sample
298-
'adminland' => [
299-
'users-permissions',
300-
'roles-permissions'
301-
],
302-
'settings' => [
303-
'email-settings-permissions',
304-
DepartmentController::class, // if permission is from routes
305-
DesignationController::class, // if permission is from routes
306-
OrganisationController::class // if permission is from routes
307-
],
308-
......,
309-
......,
318+
'panels' => [
319+
'user' => [
320+
// sample
321+
'adminland' => [
322+
'users-permissions', // all the user permissions
323+
'roles-permissions' // all the role permissions
324+
],
325+
'settings' => [
326+
'email-settings-permissions', // all the permissions of email settings
327+
DepartmentController::class, // all the permissions of department
328+
DesignationController::class, // all the permissions of designation
329+
OrganisationController::class // all the permissions of organisation
330+
],
331+
......,
332+
......,
333+
],
334+
// 'admin' => [
335+
//
336+
//],
337+
]
310338
]
311339
```
312340
Samepe Output:

config/permission-generator.php

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
<?php
2-
3-
2+
/**
3+
* In an application we may have permissions like 'create-post', 'edit-post', and these permission are hardcoded. Using the package
4+
* We can dynamically generate these permissions from route names or defining resource name
5+
*
6+
* Note: permissions could be generate for multiple panel, for example one application may have User panel and Admin panel, both may need different permissions
7+
* So we can achieve that by defining multiple panel, By default it is 'user' panel, assuming that all the permissions for user panel.
8+
* If you want different permissions for different panel you can generate that by defining another panel.
9+
*/
410
return [
511
/**
612
* Split route name by defined needle
@@ -9,36 +15,68 @@
915

1016
/**
1117
* Custom permissions
18+
*
19+
* Here you can add custom permissions for individual panel, you can add multiple panel ex: admin panel
1220
*/
1321
'custom-permissions' => [
14-
22+
'panels' => [
23+
'user' => [
24+
// custom-permissions
25+
],
26+
// 'admin' => [
27+
// // custom-permission
28+
//],
29+
]
1530
],
1631

1732
/**
18-
* Permission generate controller's namespace
33+
* Permission generate controller's
1934
*
20-
* By Default permissions will be generated from all controller's routes
35+
* By Default permissions will be generated from all controller's routes for user panel
2136
*/
2237
'permission-generate-controllers' => [
23-
'App\Http\Controllers',
38+
'panels' => [
39+
'user' => [
40+
'App\Http\Controllers',
41+
],
42+
// 'admin' => [
43+
// 'App\Http\Controllers\Admin',
44+
//],
45+
]
2446
],
2547

2648
/**
27-
* Exclude routes by controller's namespace
49+
* Exclude routes by controller's
2850
*
29-
* By default all auth controller's routes will be excluded from being generated as permission names
51+
* By default all auth controller's routes will be excluded from being generated as permission names for user panel
3052
*
31-
* [Note: Exclude routes by defining App\Http\Controller\SomeController::class or namespace-prefix]
53+
* [Note: routes can be excluded by defining App\Http\Controller\SomeController::class or namespace-prefix 'App\Http\Controllers\Auth']
3254
*/
3355
'exclude-controllers' => [
34-
'App\Http\Controllers\Auth',
56+
'panels' => [
57+
'user' => [
58+
'App\Http\Controllers\Auth',
59+
],
60+
// 'admin' => [
61+
// 'App\Http\Controllers\Admin\Auth',
62+
//],
63+
]
3564
],
3665

3766
/**
3867
* Exclude routes by route name
68+
*
69+
* Panel wise you can exclude routes
3970
*/
4071
'exclude-routes' => [
41-
// route.name
72+
'panels' => [
73+
'user' => [
74+
// route.name,
75+
],
76+
// 'admin' => [
77+
// // route.name,
78+
//],
79+
]
4280
],
4381

4482
/**
@@ -64,7 +102,7 @@
64102
],
65103

66104
/**
67-
* Parmissions can be organised by section (ex: adminland, settings, employee managment etc)
105+
* Parmissions can be grouped by section (ex: adminland, settings, employee managment etc)
68106
*
69107
* sample format: key as section name, value as generated permissions-title
70108
* [
@@ -81,6 +119,13 @@
81119
* ]
82120
*/
83121
'permissions-section' => [
84-
122+
'panels' => [
123+
'user' => [
124+
125+
],
126+
// 'admin' => [
127+
//
128+
//],
129+
]
85130
]
86131
];

0 commit comments

Comments
 (0)