Skip to content

Commit f945268

Browse files
author
Nur Alam
committed
premission append added, resource permission generate refactored, permission view refactored, config refactored, read me updated
1 parent dda17aa commit f945268

15 files changed

+337
-171
lines changed

README.md

Lines changed: 270 additions & 77 deletions
Large diffs are not rendered by default.

config/permission-generator.php

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,64 +11,55 @@
1111
* Custom permissions
1212
*/
1313
'custom-permissions' => [
14-
//
14+
1515
],
1616

1717
/**
18-
* Define controller namespace
18+
* Permission generate controller's namespace
1919
*
2020
* By Default permissions will be generated from all controller's routes
21-
*
22-
* [Note: permissions will be generated from those controller which contains the defined whole or prefix of controller namespace]
2321
*/
24-
'controller-namespace-prefixes' => [
22+
'permission-generate-controllers' => [
2523
'App\Http\Controllers',
2624
],
2725

2826
/**
29-
* Exclude routes by route name
30-
*/
31-
'exclude-routes' => [
32-
// route.name
33-
],
34-
35-
/**
36-
* Exclude routes by controller whole namespace or sub/prefix of controller namespace
27+
* Exclude routes by controller's namespace
3728
*
3829
* By default all auth controller's routes will be excluded from being generated as permission names
3930
*
40-
* [Note: We can exclude routes by defining controller name or namespace-prefix. All the routes associated with controller will be excluded]
31+
* [Note: Exclude routes by defining App\Http\Controller\SomeController::class or namespace-prefix]
4132
*/
4233
'exclude-controllers' => [
43-
// exclude every route which associate with the prefix of controller namespace
4434
'App\Http\Controllers\Auth',
4535
],
4636

4737
/**
48-
* Cache the rendered permission names
38+
* Exclude routes by route name
4939
*/
50-
'cache-permissions' => [
51-
'cacheable' => true,
52-
'cache-driver' => env('CACHE_DRIVER', 'file'),
40+
'exclude-routes' => [
41+
// route.name
5342
],
5443

5544
/**
56-
* Permission card size
57-
*
58-
* [NT: Predefined permission cards works on bootstrap]
45+
* Cache the rendered permission names
5946
*/
60-
'card-size-class' => 'col-md-3 col-lg-3 col-sm-12',
47+
'cache-permissions' => true,
6148

6249
/**
63-
* These tage used to generate permissions on given resources
50+
* ---------------------------------------------------------------------------------------------------------
51+
* This config only used if you want to generate permission names from resources instead of routes
52+
* ---------------------------------------------------------------------------------------------------------
53+
*
54+
* These actions used to generate permissions on given resources
6455
*
65-
* [Ex: create-posts,'edit-posts','view-list-posts' etc]
56+
* [Ex: If resource is posts, then permission will be (create-posts,'edit-posts','view-posts') etc]
6657
*/
67-
'resource-permission-tags' => [
58+
'resource-actions' => [
6859
'create',
6960
'edit',
7061
'show',
7162
'delete',
72-
'view-list'
63+
'view',
7364
],
7465
];

img/append-permissions.png

54.7 KB
Loading

img/department-rouets.png

-59.1 KB
Binary file not shown.

img/permissible-routes-output.png

-58 KB
Binary file not shown.

img/permission-title.png

29.2 KB
Loading

img/permissions.png

-124 KB
Loading

img/routes.png

127 KB
Loading

resources/views/permission.blade.php

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

1313
@foreach($permissions as $key => $values)
14-
<div class="{{ config('permission-generator.card-size-class') }}">
14+
<div class="col-md-3 col-lg-3 col-sm-12">
1515
<div class="card permission-card">
1616
<div class="card-header permission-header">
1717
<div class="card-title">
@@ -30,7 +30,7 @@
3030
@foreach($values as $route)
3131
<li>
3232
<input type="checkbox" name="permissions[]" value="{{ $route['name'] }}" id="{{ $route['name'] }}" {{ in_array($route['name'],$rolePermissions) ? 'checked' : '' }}>
33-
<label class="form-check-label" for="{{ $route['name'] }}">{{ $route['title'] }}</label>
33+
<label class="form-check-label" for="{{ $route['name'] }}">{{ $route['text'] }}</label>
3434
</li>
3535
@endforeach
3636
</ul>

src/Facades/PermissionsView.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
use RadiateCode\PermissionNameGenerator\Html\Builder;
1212

1313
/**
14-
* @method static Builder withRolePermissions(string $roleName, array $rolePermissions, string $permissionsSaveUrl = null)
15-
* @method static Application|Factory|View view(string $view, array $data = [])
14+
* @method static Builder make(string $view, array $data = [])
15+
* @method static Builder withPermissions(array $resources)
16+
* @method static Builder markRolePermissions(string $roleName, array $rolePermissions, string $permissionsSaveUrl = null)
17+
* @method static Application|Factory|View render()
1618
*
1719
* @see Builder
1820
*/

src/Html/Builder.php

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,22 @@ class Builder
1616

1717
protected $url = null;
1818

19-
protected string $source = 'routes';
19+
protected array $permissions = [];
2020

21-
protected array $resources = [];
21+
protected string $view;
22+
23+
protected array $viewData = [];
2224

2325
/**
26+
* Mark or tick the stored role's permissions
27+
*
2428
* @param string $roleName
2529
* @param array $rolePermissions
2630
* @param string|null $permissionsSaveUrl // role permissions save url
2731
*
2832
* @return $this
2933
*/
30-
public function withRolePermissions(
34+
public function markRolePermissions(
3135
string $roleName,
3236
array $rolePermissions,
3337
string $permissionsSaveUrl = null
@@ -42,52 +46,46 @@ public function withRolePermissions(
4246
}
4347

4448
/**
45-
* Permissions generate from route names
49+
* Permissions
4650
*
51+
* @param array $permissions
4752
* @return Builder
4853
*/
49-
public function fromRoutes()
54+
public function withPermissions(array $permissions)
5055
{
51-
$this->source = 'routes';
56+
$this->permissions = $permissions;
5257

5358
return $this;
5459
}
5560

5661
/**
57-
* Permissions generate from resources
62+
* @param string $view
63+
* @param array $data
5864
*
59-
* @param array $resources
6065
* @return Builder
6166
*/
62-
public function fromResources(array $resources)
67+
public function make(string $view, array $data = [])
6368
{
64-
$this->source = 'resources';
69+
$this->view = $view;
6570

66-
$this->resources = $resources;
71+
$this->viewData = $data;
6772

6873
return $this;
6974
}
7075

71-
/**
72-
* @param string $view
73-
* @param array $data
74-
*
75-
* @return Application|Factory|\Illuminate\Contracts\View\View
76-
*/
77-
public function view(string $view, array $data = [])
76+
public function render()
7877
{
79-
return \view($view, $data)
80-
->with('permissionCards', $this->render())
78+
return \view($this->view, $this->viewData)
79+
->with('permissionCards', $this->cards())
8180
->with('permissionScripts', $this->scripts());
8281
}
8382

84-
protected function render(array $permissions = []): string
83+
protected function cards(): string
8584
{
86-
8785
return View::make(
8886
'permission-generator::permission',
8987
[
90-
'permissions' => $this->resolvePermissions(),
88+
'permissions' => $this->permissions,
9189
'roleName' => $this->roleName,
9290
'rolePermissions' => $this->rolePermissions,
9391
]
@@ -98,13 +96,4 @@ protected function scripts(): string
9896
{
9997
return View::make('permission-generator::scripts', ['url' => $this->url])->render();
10098
}
101-
102-
protected function resolvePermissions(): array
103-
{
104-
if ($this->source == 'routes') {
105-
return Permissions::make()->fromRoutes()->get();
106-
}
107-
108-
return Permissions::make()->fromResources($this->resources)->get();
109-
}
11099
}

src/PermissionNameServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ public function boot()
4343
__DIR__.'/../resources/views' => resource_path(
4444
'views/vendor/permission-generator'
4545
),
46-
]
46+
],
47+
'permission-views'
4748
);
4849

4950
$this->publishes(

src/Permissions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ protected function customPermissions(): Permissions
8686
if (!is_array($item)) {
8787
$this->permissions[$key][] = [
8888
'name' => $item,
89-
'title' => ucwords(str_replace($this->splitter, ' ', $item)),
89+
'text' => ucwords(str_replace($this->splitter, ' ', $item)),
9090
];
9191

9292
continue;
@@ -114,7 +114,7 @@ protected function getCachedPermissions()
114114

115115
protected function hasCachedPermissions(): bool
116116
{
117-
return config('permission-generator.cache-permissions.cacheable')
117+
return config('permission-generator.cache-permissions')
118118
&& Cache::has(Constant::CACHE_PERMISSIONS);
119119
}
120120

src/Services/ResourcePermissionGenerator.php

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,53 +13,43 @@ public function __construct(array $resources)
1313

1414
public function generate()
1515
{
16-
$tags = config(
17-
'permission-generator.resource-permission-tags'
16+
$actions = config(
17+
'permission-generator.resource-actions'
1818
);
1919

2020
$resourcePermissions = [];
2121

2222
$onlyPermissionNames = [];
2323

24-
foreach ($this->resources as $resource) {
24+
foreach ($this->resources as $resource => $val) {
2525

26-
// check is resource has includes key, if so then merge includes tags with global tags
27-
if (
28-
is_array($resource)
29-
&& array_key_exists('includes', $resource)
30-
&& !empty($resource['includes'])
31-
) {
32-
$tags = array_merge($tags, $resource['includes']);
26+
if (is_string($resource)) {
27+
if (is_array($val) && !empty($val)) {
28+
$actions = array_merge($actions, $val);
29+
}
30+
} else {
31+
$resource = $val;
3332
}
3433

35-
foreach ($tags as $tag) {
36-
// check is the resource has excludes key, if so then exclude the tag form generating permissions
37-
if (
38-
is_array($resource)
39-
&& array_key_exists('excludes', $resource)
40-
&& !empty($resource['excludes'])
41-
&& in_array($tag, $resource['excludes'])
42-
) {
43-
continue;
44-
}
34+
foreach ($actions as $action) {
4535

46-
$key = $resource . '-permission';
36+
$key = $resource . '-permissions';
4737

4838
// remove this special char from tag, and make it slugable
49-
$tagSlug = str_replace(['\'', '/', '"', ',', ';', '<', '>', '.', '_'], '-', $tag);
39+
$actionSlug = str_replace(['\'', '/', '"', ',', ';', '<', '>', '.', '_'], '-', $action);
5040

5141
// remove hyphens
52-
$tagTitle = str_replace('-', ' ', $tagSlug);
42+
$actionTitle = str_replace('-', ' ', $actionSlug);
5343

5444
// generate permission name
55-
$permissionName = $tagSlug . '-' . $resource;
56-
$permissionTitle = ucwords("{$tagTitle} {$resource}");
45+
$permissionName = $actionSlug . '-' . $resource;
46+
$permissionTitle = ucwords("{$actionTitle} {$resource}");
5747

5848
$onlyPermissionNames[] = $permissionName;
5949

6050
$resourcePermissions[$key][] = [
6151
'name' => $permissionName,
62-
'title' => $permissionTitle,
52+
'text' => $permissionTitle,
6353
];
6454
}
6555
}

src/Services/RoutePermissionGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class RoutePermissionGenerator
1616
public function generate()
1717
{
1818
$this->controllerNamespacePrefixes = config(
19-
'permission-generator.controller-namespace-prefixes'
19+
'permission-generator.permission-generate-controllers'
2020
);
2121

2222
$this->globalExcludeControllers = config(
@@ -84,7 +84,7 @@ public function generate()
8484

8585
$permissions[$key][] = [
8686
'name' => $routeName, // permission name
87-
'title' => ucwords(str_replace($splitter, ' ', $routeName)), // permission title
87+
'text' => ucwords(str_replace($splitter, ' ', $routeName)), // permission title
8888
];
8989

9090
$onlyPermissionNames[] = $routeName;

0 commit comments

Comments
 (0)