Skip to content

Commit f7fa996

Browse files
committed
permission cache refactored, custome permission added in config
1 parent 3bf9538 commit f7fa996

File tree

7 files changed

+125
-86
lines changed

7 files changed

+125
-86
lines changed

config/permission-generator.php

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@
22

33

44
return [
5+
/**
6+
* Split route name by defined needle
7+
*/
8+
'route-name-splitter-needle' => '.',
9+
10+
/**
11+
* Custom permissions
12+
*/
13+
'custom-permissions' => [
14+
//
15+
],
16+
517
/**
618
* Define controller namespace
719
*
@@ -11,15 +23,10 @@
1123
'App\Http\Controllers',
1224
],
1325

14-
/**
15-
* Split route name by defined needle
16-
*/
17-
'route-name-splitter-needle' => '.',
18-
1926
/**
2027
* Exclude routes by route name
2128
*/
22-
'exclude-routes' => [
29+
'exclude-routes' => [
2330
// route.name
2431
],
2532

@@ -28,7 +35,7 @@
2835
*
2936
* [NT: We can exclude routes by defining controller name or namespace-prefix. All the routes associated with controller will be excluded]
3037
*/
31-
'exclude-controllers' => [
38+
'exclude-controllers' => [
3239
/*
3340
* exclude every route which associate with the prefix namespace
3441
*/
@@ -38,15 +45,15 @@
3845
/**
3946
* Cache the permissible routes
4047
*/
41-
'cache-permissions' => [
42-
'cacheable' => true,
43-
'cache-driver' => env('CACHE_DRIVER', 'file')
48+
'cache-permissions' => [
49+
'cacheable' => true,
50+
'cache-driver' => env('CACHE_DRIVER', 'file'),
4451
],
4552

4653
/**
4754
* Permission card size
4855
*
4956
* [NT: Permissible card only works on bootstrap]
5057
*/
51-
'card-size-class' => 'col-md-3 col-lg-3 col-sm-12',
58+
'card-size-class' => 'col-md-3 col-lg-3 col-sm-12',
5259
];

resources/views/scripts.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function saveRolePermissions() {
6666
}
6767
6868
function checkBoxValues() {
69-
var arr = $('input[type=checkbox]:checked').map(function () {
69+
var arr = $('input[name="permissions[]"]:checked').map(function () {
7070
return this.value;
7171
}).get();
7272

src/Console/PermissionCacheClearCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ class PermissionCacheClearCommand extends Command
2929
*/
3030
public function handle()
3131
{
32-
Cache::forget(Constant::CACHE_PERMISSIONS_KEY);
32+
Cache::forget(Constant::CACHE_PERMISSIONS);
3333

34-
Cache::forget(Constant::CACHE_ROUTES_COUNT_KEY);
34+
Cache::forget(Constant::CACHE_ONLY_PERMISSIONS);
3535

3636
$this->info('laravel-permission-name-generator caches are cleared successfully');
3737
}

src/Enums/Constant.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
class Constant
88
{
9-
public const CACHE_ROUTES_COUNT_KEY = 'routes-count';
9+
public const CACHE_PERMISSIONS = 'permissions';
1010

11-
public const CACHE_PERMISSIONS_KEY = 'permissions';
11+
public const CACHE_ONLY_PERMISSIONS = 'only-permissions';
1212
}

src/Facades/PermissionsView.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
namespace RadiateCode\PermissionNameGenerator\Facades;
55

66

7+
use Illuminate\Contracts\View\View;
8+
use Illuminate\Contracts\View\Factory;
79
use Illuminate\Support\Facades\Facade;
10+
use Illuminate\Contracts\Foundation\Application;
811
use RadiateCode\PermissionNameGenerator\Html\Builder;
912

1013
/**
11-
* @method static Builder withRolePermissions(string $roleName, array $rolePermissions)
12-
* @method static Builder addManualPermission(string $key, array $permissions)
13-
* @method static string view()
14-
* @method static string scripts($url = null)
14+
* @method static Builder withRolePermissions(string $roleName, array $rolePermissions, string $permissionsSaveUrl = null)
15+
* @method static Application|Factory|View view(string $view, array $data = [])
1516
*
1617
* @see Builder
1718
*/

src/Html/Builder.php

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
namespace RadiateCode\PermissionNameGenerator\Html;
55

66
use Illuminate\Support\Facades\View;
7+
use Illuminate\Contracts\View\Factory;
8+
use Illuminate\Contracts\Foundation\Application;
79
use RadiateCode\PermissionNameGenerator\Permissions;
810

911
class Builder
@@ -12,52 +14,56 @@ class Builder
1214

1315
protected $roleName = '';
1416

15-
protected $manualPermissions = [];
17+
protected $url = null;
1618

19+
/**
20+
* @param string $roleName
21+
* @param array $rolePermissions
22+
* @param string|null $permissionsSaveUrl // role permissions save url
23+
*
24+
* @return $this
25+
*/
1726
public function withRolePermissions(
1827
string $roleName,
19-
array $rolePermissions
28+
array $rolePermissions,
29+
string $permissionsSaveUrl = null
2030
): Builder {
2131
$this->rolePermissions = $rolePermissions;
2232

2333
$this->roleName = $roleName;
2434

35+
$this->url = $permissionsSaveUrl;
36+
2537
return $this;
2638
}
2739

2840
/**
29-
* @param string $key // key can contain dot to indicate nested level
30-
* @param array $permissions
41+
* @param string $view
42+
* @param array $data
3143
*
32-
* @return $this
44+
* @return Application|Factory|\Illuminate\Contracts\View\View
3345
*/
34-
public function addManualPermission(string $key, array $permissions): Builder
46+
public function view(string $view, array $data = [])
3547
{
36-
$this->manualPermissions[$key] = $permissions;
37-
38-
return $this;
48+
return \view($view, $data)
49+
->with('permissionCards', $this->render())
50+
->with('permissionScripts', $this->scripts());
3951
}
4052

41-
public function view(): string
53+
protected function render(): string
4254
{
4355
return View::make(
4456
'permission-generator::permission',
4557
[
46-
'routes' => Permissions::make()->withManualPermissions($this->manualPermissions)->get(),
58+
'routes' => Permissions::make()->get(),
4759
'roleName' => $this->roleName,
4860
'rolePermissions' => $this->rolePermissions,
4961
]
5062
)->render();
5163
}
5264

53-
/**
54-
* @param null $url // role permissions save url
55-
*
56-
* @return string
57-
*/
58-
public function scripts($url = null): string
65+
protected function scripts(): string
5966
{
60-
return View::make('permission-generator::scripts', ['url' => $url])
61-
->render();
67+
return View::make('permission-generator::scripts', ['url' => $this->url])->render();
6268
}
6369
}

src/Permissions.php

Lines changed: 72 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@ class Permissions
1717

1818
private $globalExcludeControllers = [];
1919

20-
private $manualPermissions = [];
20+
protected $onlyPermissions = [];
21+
22+
protected $permissions = [];
23+
24+
public function __construct()
25+
{
26+
$this->generate();
27+
}
2128

2229
public static function make(): Permissions
2330
{
@@ -26,6 +33,24 @@ public static function make(): Permissions
2633

2734
public function get(): array
2835
{
36+
return $this->getCachedPermissions();
37+
}
38+
39+
public function getOnlyPermissions()
40+
{
41+
if (! $this->hasCachedPermissions()) {
42+
return $this->onlyPermissions;
43+
}
44+
45+
return Cache::get(Constant::CACHE_ONLY_PERMISSIONS);
46+
}
47+
48+
protected function generate(): Permissions
49+
{
50+
if ($this->hasCachedPermissions()) {
51+
return $this;
52+
}
53+
2954
$this->controllerNamespacePrefixes = config(
3055
'permission-generator.controller-namespace-prefixes'
3156
);
@@ -40,16 +65,6 @@ public function get(): array
4065

4166
$routes = Route::getRoutes();
4267

43-
$routesCount = count($routes);
44-
45-
$cacheRoutes = self::getCacheRoutes($routesCount);
46-
47-
if ($cacheRoutes !== null) {
48-
return $cacheRoutes;
49-
}
50-
51-
$permissions = [];
52-
5368
$tempRoutes = [];
5469

5570
foreach ($routes as $route) {
@@ -60,7 +75,6 @@ public function get(): array
6075
continue;
6176
}
6277

63-
6478
$actionName = $route->getActionName();
6579

6680
$actionExtract = explode('@', $actionName);
@@ -97,38 +111,54 @@ public function get(): array
97111
continue;
98112
}
99113

100-
$title = self::generatePermissionTitle($controllerInstance);
114+
$title = $this->generatePermissionTitle($controllerInstance);
101115

102116
$key = strtolower(Str::slug($title, "-"));
103117

104-
$permissions[$key][] = [
118+
$this->permissions[$key][] = [
105119
'slug' => $routeName,
106120
'name' => ucwords(str_replace($splitter, ' ', $routeName)),
107121
];
108122

109-
$tempRoutes = $permissions[$key];
110-
}
123+
$this->onlyPermissions[] = $routeName;
111124

112-
// add manual permissions to rendered permissions
113-
if ( ! empty($this->manualPermissions)) {
114-
foreach ($this->manualPermissions as $key => $permission) {
115-
$permissions[$key][] = $permission;
116-
}
125+
$tempRoutes = $this->permissions[$key];
117126
}
118127

119-
self::cachePermissions($routesCount, $permissions);
128+
// add custom permissions to rendered permissions
129+
$this->customPermissions();
130+
131+
$this->cachePermissions();
120132

121-
return $permissions;
133+
return $this;
122134
}
123135

124-
public function withManualPermissions(array $permissions): Permissions
136+
protected function customPermissions(): Permissions
125137
{
126-
$this->manualPermissions = $permissions;
138+
$customPermissions = config('permission-generator.custom-permissions');
139+
140+
if (is_array($customPermissions) && ! empty($customPermissions)) {
141+
foreach ($customPermissions as $key => $permission) {
142+
if (array_key_exists(0, $permission)
143+
&& is_array(
144+
$permission[0]
145+
)
146+
) {
147+
foreach ($permission as $item) {
148+
$this->permissions[$key][] = $item;
149+
}
150+
151+
continue;
152+
}
153+
154+
$this->permissions[$key][] = $permission;
155+
}
156+
}
127157

128158
return $this;
129159
}
130160

131-
protected static function generatePermissionTitle($controllerInstance)
161+
protected function generatePermissionTitle($controllerInstance)
132162
{
133163
// if the controller use the WithPermissible interface then get the title
134164
if ($controllerInstance instanceof WithPermissionGenerator) {
@@ -174,38 +204,33 @@ protected function isControllerValid($controller): bool
174204
return false;
175205
}
176206

177-
protected static function getCacheRoutes(int $routesCount)
207+
protected function getCachedPermissions()
178208
{
179-
if ( ! config('permission-generator.cache-permissions.cacheable')
180-
|| ! Cache::has(Constant::CACHE_ROUTES_COUNT_KEY)
181-
) {
182-
return null;
209+
if ( ! $this->hasCachedPermissions()) {
210+
return $this->permissions;
183211
}
184212

185-
if ($routesCount !== Cache::get(Constant::CACHE_ROUTES_COUNT_KEY)) {
186-
Cache::forget(Constant::CACHE_ROUTES_COUNT_KEY);
187-
Cache::forget(Constant::CACHE_PERMISSIONS_KEY);
188-
189-
return null;
190-
}
213+
return Cache::get(Constant::CACHE_PERMISSIONS);
214+
}
191215

192-
return Cache::get(Constant::CACHE_PERMISSIONS_KEY);
216+
protected function hasCachedPermissions(): bool
217+
{
218+
return config('permission-generator.cache-permissions.cacheable')
219+
&& Cache::has(Constant::CACHE_PERMISSIONS);
193220
}
194221

195-
protected static function cachePermissions(int $routesCount, $permissions)
222+
protected function cachePermissions()
196223
{
197-
if (config('permission-generator.cache-permissions.cacheable')
198-
&& ! Cache::has(Constant::CACHE_ROUTES_COUNT_KEY)
199-
&& ! empty($permissions)
200-
) {
224+
if (! $this->hasCachedPermissions() && ! empty($this->permissions)) {
201225
Cache::put(
202-
Constant::CACHE_ROUTES_COUNT_KEY,
203-
$routesCount,
226+
Constant::CACHE_PERMISSIONS,
227+
$this->permissions,
204228
now()->addDay()
205229
);
230+
206231
Cache::put(
207-
Constant::CACHE_PERMISSIONS_KEY,
208-
$permissions,
232+
Constant::CACHE_ONLY_PERMISSIONS,
233+
$this->onlyPermissions,
209234
now()->addDay()
210235
);
211236
}

0 commit comments

Comments
 (0)