Skip to content

Commit a1f09f6

Browse files
author
Nur Alam
committed
resource based permission generator added, permission from routes move to a generator class, permission class refactored
1 parent 3748fda commit a1f09f6

File tree

10 files changed

+297
-162
lines changed

10 files changed

+297
-162
lines changed

README.md

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,19 @@ Publish default permission view files (optional)
2323

2424
# Usage
2525

26+
Here are some defined routes for department
27+
28+
![Stats](img/department-rouets.png)
29+
### Now get permissions names from defined routes
30+
31+
RadiateCode\PermissionNameGenerator\Permissions::make()->get();
32+
33+
**Output**
34+
35+
![Stats](img/permissions.png)
36+
37+
> Notice the key `department-permissions` it generated from `DepartmentController` and permissions are generated from DepartmentController's routes.
38+
2639
## PermissionGenerator trait [Optional]
2740
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.
2841

@@ -50,16 +63,6 @@ class OfficeController extends Controller implements WithPermissionGenerator
5063

5164
> **PermissionGenerator** trait is optional. Because if no group permission title defined, then this package dynamically generate a title based on controller name. Routes can be excluded in the config file in order to tell the package not to generate those routes as permission names.
5265
53-
## Get permissions names
54-
55-
RadiateCode\PermissionNameGenerator\Permissions::make()->get();
56-
57-
**Output**
58-
59-
![Stats](img/permissions.png)
60-
61-
> Notice the key `department-permissions` it generated from `DepartmentController` and permissions are generated from DepartmentController's routes.
62-
6366
## Permission View Builder Facade
6467
The package comes with predefined a view with permission names
6568

@@ -77,7 +80,7 @@ The package comes with predefined a view with permission names
7780
> $request->get('permissions'); // array of permissions
7881
> ```
7982
80-
## Example
83+
### Example
8184
**In controller:**
8285
8386
```php
@@ -184,7 +187,7 @@ Config the **config/permission-generator.php** file.
184187
> Note: notice the `user-permission` key which contains only permission name, the package dynamically make a title for the permission name.
185188
>
186189
187-
3. We can define controller by it's namespace, it could be whole namespace or it could be sub/prefix of controller namespace. This config play vital role to generate permissions because permissions will be generated only for our defined controllers.
190+
3. Define controller by it's namespace, it could be whole namespace or it could be sub/prefix of controller namespace. This config play vital role to generate permissions because permissions will be generated only for defined controllers.
188191
189192
```php
190193
/**

config/permission-generator.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,14 @@
5757
*
5858
* [NT: Predefined permission cards works on bootstrap]
5959
*/
60-
'card-size-class' => 'col-md-3 col-lg-3 col-sm-12',
60+
'card-size-class' => 'col-md-3 col-lg-3 col-sm-12',
61+
62+
63+
'resource-permission-tags' => [
64+
'create',
65+
'edit',
66+
'show',
67+
'delete',
68+
'view-list'
69+
],
6170
];

img/department-rouets.png

59.1 KB
Loading

resources/views/permission.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</div>
1111
</div>
1212

13-
@foreach($routes as $key => $values)
13+
@foreach($permissions as $key => $values)
1414
<div class="{{ config('permission-generator.card-size-class') }}">
1515
<div class="card permission-card">
1616
<div class="card-header permission-header">
@@ -25,7 +25,7 @@
2525
</div>
2626
<div class="card-body">
2727
<div class="row">
28-
<div class="{{ $key }}-routes-checkbox">
28+
<div class="{{ $key }}-permissions-checkbox">
2929
<ul style="list-style-type: none">
3030
@foreach($values as $route)
3131
<li>

resources/views/scripts.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var permission_id = $(this).attr('id');
44
var isChecked = $(this).is(':checked');
55
6-
$('.' + permission_id + '-routes-checkbox ul li').each(function () {
6+
$('.' + permission_id + '-permissions-checkbox ul li').each(function () {
77
$(this).find(':checkbox').prop('checked', isChecked);
88
});
99
});

src/Html/Builder.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,12 @@ public function view(string $view, array $data = [])
5050
->with('permissionScripts', $this->scripts());
5151
}
5252

53-
protected function render(): string
53+
protected function render(array $permissions = []): string
5454
{
55-
dd(Permissions::make()->get());
56-
5755
return View::make(
5856
'permission-generator::permission',
5957
[
60-
'routes' => Permissions::make()->get(),
58+
'permissions' => Permissions::make()->fromRoutes()->get(),
6159
'roleName' => $this->roleName,
6260
'rolePermissions' => $this->rolePermissions,
6361
]
@@ -68,4 +66,4 @@ protected function scripts(): string
6866
{
6967
return View::make('permission-generator::scripts', ['url' => $this->url])->render();
7068
}
71-
}
69+
}

src/Permissions.php

Lines changed: 37 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,15 @@
33

44
namespace RadiateCode\PermissionNameGenerator;
55

6-
7-
use Illuminate\Support\Arr;
8-
use Illuminate\Support\Str;
6+
use Closure;
97
use Illuminate\Support\Facades\Cache;
10-
use Illuminate\Support\Facades\Route;
11-
use RadiateCode\PermissionNameGenerator\Contracts\WithPermissionGenerator;
128
use RadiateCode\PermissionNameGenerator\Enums\Constant;
9+
use RadiateCode\PermissionNameGenerator\Services\ResourcePermissionGenerator;
10+
use RadiateCode\PermissionNameGenerator\Services\RoutePermissionGenerator;
1311

1412
class Permissions
1513
{
16-
private $controllerNamespacePrefixes = [];
17-
18-
private $globalExcludeControllers = [];
19-
20-
protected $onlyPermissions = [];
14+
protected $onlyPermissionsNames = [];
2115

2216
protected $permissions = [];
2317

@@ -26,115 +20,63 @@ class Permissions
2620
public function __construct()
2721
{
2822
$this->splitter = config('permission-generator.route-name-splitter-needle');
29-
30-
$this->generate();
3123
}
3224

3325
public static function make(): Permissions
3426
{
3527
return new self();
3628
}
3729

38-
public function get(): array
39-
{
40-
return $this->getCachedPermissions();
41-
}
42-
43-
public function getOnlyPermissions()
44-
{
45-
if (!$this->hasCachedPermissions()) {
46-
return $this->onlyPermissions;
47-
}
48-
49-
return Cache::get(Constant::CACHE_ONLY_PERMISSIONS);
30+
public function fromAttributes(){
31+
5032
}
5133

52-
protected function generate(): Permissions
34+
public function fromResources(array $resources)
5335
{
5436
if ($this->hasCachedPermissions()) {
5537
return $this;
5638
}
5739

58-
$this->controllerNamespacePrefixes = config(
59-
'permission-generator.controller-namespace-prefixes'
60-
);
61-
62-
$this->globalExcludeControllers = config(
63-
'permission-generator.exclude-controllers'
64-
);
40+
$resourceGenerator = (new ResourcePermissionGenerator($resources))->generate();
6541

66-
$globalExcludedRoutes = config('permission-generator.exclude-routes');
67-
68-
$routes = Route::getRoutes();
69-
70-
$tempRoutes = [];
71-
72-
foreach ($routes as $route) {
73-
$routeName = $route->getName();
74-
75-
// exclude routes which defined in the config
76-
if (in_array($routeName, $globalExcludedRoutes)) {
77-
continue;
78-
}
42+
$this->permissions = $resourceGenerator['permissions'];
43+
$this->onlyPermissionsNames = $resourceGenerator['only_permission_names'];
7944

80-
$actionName = $route->getActionName();
81-
82-
$actionExtract = explode('@', $actionName);
83-
84-
$controller = $actionExtract[0];
85-
86-
// $routeMiddlewares = $route->gatherMiddleware();
87-
88-
if (
89-
$controller == 'Closure'
90-
|| !$this->isControllerValid($controller)
91-
|| $this->isExcludedController($controller)
92-
) {
93-
continue;
94-
}
95-
96-
97-
$controllerInstance = app('\\' . $controller);
98-
99-
// if the controller use the WithPermissible interface then find the excluded routes
100-
if ($controllerInstance instanceof WithPermissionGenerator) {
101-
$controllerMethod = $actionExtract[1];
45+
$this->customPermissions();
46+
// $this->cachePermissions();
10247

103-
$excludeMethods = $controllerInstance->getExcludeMethods();
48+
return $this;
49+
}
10450

105-
if (in_array($controllerMethod, $excludeMethods)) {
106-
continue;
107-
}
108-
}
51+
public function fromRoutes()
52+
{
53+
if ($this->hasCachedPermissions()) {
54+
return $this;
55+
}
10956

110-
$tempPluckRoutes = Arr::pluck($tempRoutes, 'slug');
57+
$routePermissionGenerator = (new RoutePermissionGenerator())->generate();
11158

112-
// check is the current route store in temp routes in order to avoid duplicacy
113-
if (in_array($routeName, $tempPluckRoutes)) {
114-
continue;
115-
}
59+
$this->permissions = $routePermissionGenerator['permissions'];
60+
$this->onlyPermissionsNames = $routePermissionGenerator['only_permission_names'];
11661

117-
// permission group title
118-
$title = $this->generatePermissionTitle($controllerInstance);
119-
120-
$key = strtolower(Str::slug($title, "-"));
62+
$this->customPermissions();
63+
//$this->cachePermissions();
12164

122-
$this->permissions[$key][] = [
123-
'name' => $routeName, // permission name
124-
'title' => ucwords(str_replace($this->splitter, ' ', $routeName)), // permission title
125-
];
65+
return $this;
66+
}
12667

127-
$this->onlyPermissions[] = $routeName;
68+
public function get(): array
69+
{
70+
return $this->getCachedPermissions();
71+
}
12872

129-
$tempRoutes = $this->permissions[$key];
73+
public function getOnlyPermissionsNames()
74+
{
75+
if (!$this->hasCachedPermissions()) {
76+
return $this->onlyPermissionsNames;
13077
}
13178

132-
// add custom permissions to rendered permissions
133-
$this->customPermissions();
134-
135-
$this->cachePermissions();
136-
137-
return $this;
79+
return Cache::get(Constant::CACHE_ONLY_PERMISSIONS);
13880
}
13981

14082
protected function customPermissions(): Permissions
@@ -144,7 +86,7 @@ protected function customPermissions(): Permissions
14486
if (is_array($customPermissions) && !empty($customPermissions)) {
14587
foreach ($customPermissions as $key => $permission) {
14688
// when the permission only contains permission name
147-
if (array_key_exists(0, $permission) && is_array($permission)) {
89+
if (array_key_exists(0, $permission) && is_array($permission)) {
14890
foreach ($permission as $item) {
14991
$this->permissions[$key][] = [
15092
'name' => $item,
@@ -163,52 +105,6 @@ protected function customPermissions(): Permissions
163105
return $this;
164106
}
165107

166-
protected function generatePermissionTitle($controllerInstance)
167-
{
168-
// if the controller use the WithPermissible interface then get the title
169-
if ($controllerInstance instanceof WithPermissionGenerator) {
170-
$title = $controllerInstance->getPermissionTitle();
171-
172-
if (!empty($title)) {
173-
return $title;
174-
}
175-
}
176-
177-
// Or, generate permission title from controller name
178-
$controllerName = class_basename($controllerInstance);
179-
180-
// place white space between controller (PascalCase) name
181-
$name = preg_replace('/([a-z])([A-Z])/s', '$1 $2', $controllerName);
182-
183-
if (Str::contains($controllerName, 'Controller')) {
184-
return str_replace('Controller', 'Permissions', $name);
185-
}
186-
187-
return $name . ' Permissions';
188-
}
189-
190-
protected function isExcludedController($controller): bool
191-
{
192-
foreach ($this->globalExcludeControllers as $prefix) {
193-
if (Str::contains($controller, $prefix)) {
194-
return true;
195-
}
196-
}
197-
198-
return false;
199-
}
200-
201-
protected function isControllerValid($controller): bool
202-
{
203-
foreach ($this->controllerNamespacePrefixes as $prefix) {
204-
if (Str::contains($controller, $prefix)) {
205-
return true;
206-
}
207-
}
208-
209-
return false;
210-
}
211-
212108
protected function getCachedPermissions()
213109
{
214110
if (!$this->hasCachedPermissions()) {
@@ -235,7 +131,7 @@ protected function cachePermissions()
235131

236132
Cache::put(
237133
Constant::CACHE_ONLY_PERMISSIONS,
238-
$this->onlyPermissions,
134+
$this->onlyPermissionsNames,
239135
now()->addDay()
240136
);
241137
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace RadiateCode\PermissionNameGenerator\Services;
4+
5+
class AttributePermissionGenerator
6+
{
7+
8+
}

0 commit comments

Comments
 (0)