Skip to content

Commit ae00374

Browse files
author
Nur Alam
committed
section permission method refactored
1 parent cc0a771 commit ae00374

File tree

5 files changed

+120
-37
lines changed

5 files changed

+120
-37
lines changed

README.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ From which controller's routes permission names will be generate, define it here
228228
*/
229229
'permission-generate-controllers' => [
230230
'App\Http\Controllers',
231-
// 'App\Http\Controllers\Api',
232231
232+
// sample
233+
// 'App\Http\Controllers\Api',
233234
// App\Http\Controllers\DepartmentController::class,
234235
// App\Http\Controllers\DesignationController::class,
235236
],
@@ -243,6 +244,8 @@ Exclude routes by defining controller namespace. Here auth and EmployeeProfileCo
243244
*/
244245
'exclude-controllers' => [
245246
'App\Http\Controllers\Auth', // exclude routes of all auth controllers
247+
248+
// sample
246249
App\Http\Controller\Employee\EmployeeProfileController::class, // exclude routes of EmployeeProfileController
247250
],
248251
```
@@ -254,6 +257,7 @@ Exclude routes by defining controller namespace. Here auth and EmployeeProfileCo
254257
* Exclude routes by route name
255258
*/
256259
'exclude-routes' => [
260+
// sample
257261
'register.user',
258262
'employee.profile',
259263
....
@@ -269,6 +273,42 @@ Caching the permission names
269273
*/
270274
'cache-permissions' => true,
271275
```
276+
### 7. Permissions Section
277+
Permissions can be organised by section, example admin section, employee section, settings setion etc.
278+
```php
279+
/**
280+
* Parmissions can be organised by section (ex: adminland, settings, employee managment etc)
281+
*
282+
* sample format: key as section name, value as generated permissions-title
283+
* [
284+
* 'adminland' => [
285+
* 'employee-permissions',
286+
* 'bonus-permissions'
287+
* ],
288+
* 'settings' => [
289+
* 'office-permissions',
290+
* 'designation-permissions',
291+
* 'email-settings-permissions',
292+
* 'rules-permissions'
293+
* ],
294+
* ]
295+
*/
296+
'permissions-section' => [
297+
// sample
298+
'admin' => [
299+
'users-permissions',
300+
'roles-permissions'
301+
],
302+
'settings' => [
303+
'email-settings-permissions',
304+
RuleController::class, // if permission is from routes
305+
NotificationController::class // if permission is from routes
306+
],
307+
......,
308+
......,
309+
]
310+
```
311+
272312
## Alternatively generate Permissions
273313
The package allows you to generate permission names by defining resource names.
274314

config/permission-generator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
/**
6767
* Parmissions can be organised by section (ex: adminland, settings, employee managment etc)
6868
*
69-
* format: key as section name, value as generated permissions-title
69+
* sample format: key as section name, value as generated permissions-title
7070
* [
7171
* 'adminland' => [
7272
* 'employee-permissions',
@@ -75,8 +75,8 @@
7575
* 'settings' => [
7676
* 'office-permissions',
7777
* 'designation-permissions',
78-
* 'email-settings-permissions,
79-
* 'rules-permissions
78+
* 'email-settings-permissions',
79+
* 'rules-permissions'
8080
* ],
8181
* ]
8282
*/

src/Permissions.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ public function fromRoutes()
6363
public function get(): array
6464
{
6565
if (!$this->hasCachedPermissions()) {
66-
ksort($this->permissions);
67-
68-
$this->sectionPermissions(); // if any
69-
7066
return $this->permissions;
7167
}
7268

@@ -108,32 +104,6 @@ protected function customPermissions(): Permissions
108104
return $this;
109105
}
110106

111-
protected function sectionPermissions()
112-
{
113-
$permissionsSection = config('permission-generator.permissions-section');
114-
115-
if (empty($permissionsSection)) {
116-
return $this;
117-
}
118-
119-
$sectionWisePermissions = [];
120-
121-
foreach ($permissionsSection as $section => $permissions) {
122-
foreach ($permissions as $permission) {
123-
$sectionWisePermissions[$section]['section'] = str_replace(['\'', '/', '"', ',', ';', '<', '>', '.', '_', '-', ':'], ' ', $section);
124-
$sectionWisePermissions[$section]['permissions'][$permission] = $this->permissions[$permission];
125-
126-
unset($this->permissions[$permission]);
127-
}
128-
129-
ksort($sectionWisePermissions[$section]['permissions']);
130-
}
131-
132-
$this->permissions = array_merge($sectionWisePermissions, $this->permissions);
133-
134-
return $this;
135-
}
136-
137107
protected function hasCachedPermissions(): bool
138108
{
139109
return config('permission-generator.cache-permissions')

src/Services/ResourcePermissionGenerator.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,41 @@ public function generate()
5454
}
5555
}
5656

57+
ksort($resourcePermissions);
58+
5759
return [
58-
'permissions' => $resourcePermissions,
60+
'permissions' => $this->sectionPermissions($resourcePermissions),
5961
'only_permission_names' => $onlyPermissionNames
6062
];
6163
}
64+
65+
protected function sectionPermissions($generatedPermissions)
66+
{
67+
$permissionsSection = config('permission-generator.permissions-section');
68+
69+
if (empty($permissionsSection)) {
70+
return $generatedPermissions;
71+
}
72+
73+
$sectionWisePermissions = [];
74+
75+
foreach ($permissionsSection as $section => $permissions) {
76+
foreach ($permissions as $permissionsTitleKey) {
77+
if (array_key_exists($permissionsTitleKey, $generatedPermissions)) {
78+
$sectionWisePermissions[$section]['section'] = str_replace(['\'', '/', '"', ',', ';', '<', '>', '.', '_', '-', ':'], ' ', $section);
79+
$sectionWisePermissions[$section]['permissions'][$permissionsTitleKey] = $generatedPermissions[$permissionsTitleKey];
80+
81+
unset($generatedPermissions[$permissionsTitleKey]);
82+
}
83+
}
84+
85+
if (!empty($sectionWisePermissions)) {
86+
ksort($sectionWisePermissions[$section]['permissions']);
87+
}
88+
}
89+
90+
$generatedPermissions = array_merge($sectionWisePermissions, $generatedPermissions);
91+
92+
return $generatedPermissions;
93+
}
6294
}

src/Services/RoutePermissionGenerator.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,54 @@ public function generate()
8484

8585
$onlyPermissionNames[] = $routeName;
8686
}
87-
87+
88+
ksort($permissions);
89+
8890
return [
89-
'permissions' => $permissions,
91+
'permissions' => $this->sectionPermissions($permissions),
9092
'only_permission_names' => $onlyPermissionNames
9193
];
9294
}
9395

96+
protected function sectionPermissions($generatedPermissions)
97+
{
98+
$permissionsSection = config('permission-generator.permissions-section');
99+
100+
if (empty($permissionsSection)) {
101+
return $generatedPermissions;
102+
}
103+
104+
$sectionWisePermissions = [];
105+
106+
foreach ($permissionsSection as $section => $permissions) {
107+
foreach ($permissions as $permissionsTitle) {
108+
// check is the permissions title is key or class
109+
if (class_exists($permissionsTitle)) {
110+
$permissionsTitleClassInstance = app("\\" . $permissionsTitle);
111+
112+
$title = $this->generatePermissionTitle($permissionsTitleClassInstance);
113+
114+
$permissionsTitle = strtolower(Str::slug($title, "-"));
115+
}
116+
117+
if (array_key_exists($permissionsTitle, $generatedPermissions)) {
118+
$sectionWisePermissions[$section]['section'] = str_replace(['\'', '/', '"', ',', ';', '<', '>', '.', '_', '-', ':'], ' ', $section);
119+
$sectionWisePermissions[$section]['permissions'][$permissionsTitle] = $generatedPermissions[$permissionsTitle];
120+
121+
unset($generatedPermissions[$permissionsTitle]);
122+
}
123+
}
124+
125+
if (!empty($sectionWisePermissions)) {
126+
ksort($sectionWisePermissions[$section]['permissions']);
127+
}
128+
}
129+
130+
$generatedPermissions = array_merge($sectionWisePermissions, $generatedPermissions);
131+
132+
return $generatedPermissions;
133+
}
134+
94135
protected function generateKey($controllerInstance)
95136
{
96137
$key = $this->appendPermissionKey($controllerInstance);

0 commit comments

Comments
 (0)