Skip to content

Commit 3bf9538

Browse files
committed
refactored
1 parent cd4af29 commit 3bf9538

File tree

11 files changed

+221
-63
lines changed

11 files changed

+221
-63
lines changed

composer.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "radiatecode/laravel-permission-name-generator",
33
"description": "Laravel package to generate permission names from route names",
44
"keywords": [
5-
"laravel-permission-name",
5+
"laravel-permission-names",
66
"laravel-permissions-view",
77
"permission-name-generator"
88
],
@@ -24,7 +24,10 @@
2424
"autoload": {
2525
"psr-4": {
2626
"RadiateCode\\PermissionNameGenerator\\": "src/"
27-
}
27+
},
28+
"files": [
29+
"src/helpers.php"
30+
]
2831
},
2932
"autoload-dev": {
3033
"psr-4": {
@@ -41,7 +44,7 @@
4144
"extra": {
4245
"laravel": {
4346
"providers" : [
44-
"PermissionNameServiceProvider"
47+
"RadiateCode\\PermissionNameGenerator\\PermissionNameServiceProvider"
4548
]
4649
}
4750
},

config/permission-generator.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@
33

44
return [
55
/**
6-
* Permission middlewares
6+
* Define controller namespace
77
*
8-
* [NT: Which middleware routes need to be count as permission]
8+
* [NT: permissions will be generated from those controller which contains the prefix]
99
*/
10-
'permission-middlewares' => [
11-
// permission middleware
10+
'controller-namespace-prefixes' => [
11+
'App\Http\Controllers',
1212
],
1313

1414
/**
15-
* Split route name by defined character
15+
* Split route name by defined needle
1616
*/
17-
'route-name-splitter' => '.',
17+
'route-name-splitter-needle' => '.',
1818

1919
/**
2020
* Exclude routes by route name
@@ -24,21 +24,21 @@
2424
],
2525

2626
/**
27-
* Exclude routes by controller
27+
* Exclude routes by controller or controller namespace-prefix
2828
*
29-
* [NT: We can exclude routes by controllers. All the routes associated with controller will be excluded]
29+
* [NT: We can exclude routes by defining controller name or namespace-prefix. All the routes associated with controller will be excluded]
3030
*/
3131
'exclude-controllers' => [
3232
/*
33-
* exclude every route which associate with WelcomeController
33+
* exclude every route which associate with the prefix namespace
3434
*/
35-
// WelcomeController::class
35+
'App\Http\Controllers\Auth',
3636
],
3737

3838
/**
3939
* Cache the permissible routes
4040
*/
41-
'cache-routes' => [
41+
'cache-permissions' => [
4242
'cacheable' => true,
4343
'cache-driver' => env('CACHE_DRIVER', 'file')
4444
],

resources/views/permission.blade.php

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

1313
@foreach($routes as $key => $values)
14-
<div class="{{ config('permissions-generator.card-size-class') }}">
14+
<div class="{{ config('permission-generator.card-size-class') }}">
1515
<div class="card permission-card">
1616
<div class="card-header permission-header">
1717
<div class="card-title">
@@ -29,8 +29,8 @@
2929
<ul style="list-style-type: none">
3030
@foreach($values as $route)
3131
<li>
32-
<input type="checkbox" name="permissions[]" value="{{ $route['route'] }}" id="{{ $route['route'] }}" {{ in_array($route['route'],$rolePermissions) ? 'checked' : '' }}>
33-
<label class="form-check-label" for="{{ $route['route'] }}">{{ $route['title'] }}</label>
32+
<input type="checkbox" name="permissions[]" value="{{ $route['slug'] }}" id="{{ $route['slug'] }}" {{ in_array($route['slug'],$rolePermissions) ? 'checked' : '' }}>
33+
<label class="form-check-label" for="{{ $route['slug'] }}">{{ $route['name'] }}</label>
3434
</li>
3535
@endforeach
3636
</ul>
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace RadiateCode\PermissionNameGenerator\Console;
4+
5+
use Illuminate\Console\Command;
6+
use Illuminate\Support\Facades\Cache;
7+
use RadiateCode\PermissionNameGenerator\Enums\Constant;
8+
9+
class PermissionCacheClearCommand extends Command
10+
{
11+
/**
12+
* The console command name.
13+
*
14+
* @var string
15+
*/
16+
protected $name = 'permissions:cache-clear';
17+
18+
/**
19+
* The console command description.
20+
*
21+
* @var string
22+
*/
23+
protected $description = 'Clear laravel generated permissions cache';
24+
25+
26+
/**
27+
* Execute the console command.
28+
*
29+
*/
30+
public function handle()
31+
{
32+
Cache::forget(Constant::CACHE_PERMISSIONS_KEY);
33+
34+
Cache::forget(Constant::CACHE_ROUTES_COUNT_KEY);
35+
36+
$this->info('laravel-permission-name-generator caches are cleared successfully');
37+
}
38+
}

src/Enums/Constant.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
4+
namespace RadiateCode\PermissionNameGenerator\Enums;
5+
6+
7+
class Constant
8+
{
9+
public const CACHE_ROUTES_COUNT_KEY = 'routes-count';
10+
11+
public const CACHE_PERMISSIONS_KEY = 'permissions';
12+
}

src/Facades/PermissionsView.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
/**
1111
* @method static Builder withRolePermissions(string $roleName, array $rolePermissions)
12-
* @method static string permissionView()
13-
* @method static string permissionScripts($url = null)
12+
* @method static Builder addManualPermission(string $key, array $permissions)
13+
* @method static string view()
14+
* @method static string scripts($url = null)
1415
*
1516
* @see Builder
1617
*/

src/Html/Builder.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,52 @@ class Builder
1212

1313
protected $roleName = '';
1414

15-
public function rolePermissions(string $roleName, array $rolePermissions): Builder
16-
{
15+
protected $manualPermissions = [];
16+
17+
public function withRolePermissions(
18+
string $roleName,
19+
array $rolePermissions
20+
): Builder {
1721
$this->rolePermissions = $rolePermissions;
1822

1923
$this->roleName = $roleName;
2024

2125
return $this;
2226
}
2327

28+
/**
29+
* @param string $key // key can contain dot to indicate nested level
30+
* @param array $permissions
31+
*
32+
* @return $this
33+
*/
34+
public function addManualPermission(string $key, array $permissions): Builder
35+
{
36+
$this->manualPermissions[$key] = $permissions;
37+
38+
return $this;
39+
}
40+
2441
public function view(): string
2542
{
26-
return View::make('permissions-generator::permission',
43+
return View::make(
44+
'permission-generator::permission',
2745
[
28-
'routes' => Permissions::get(),
29-
'roleName' => $this->roleName,
30-
'rolePermissions' => $this->rolePermissions
46+
'routes' => Permissions::make()->withManualPermissions($this->manualPermissions)->get(),
47+
'roleName' => $this->roleName,
48+
'rolePermissions' => $this->rolePermissions,
3149
]
3250
)->render();
3351
}
3452

3553
/**
36-
* @param null $url // role permissions save url
54+
* @param null $url // role permissions save url
3755
*
3856
* @return string
3957
*/
4058
public function scripts($url = null): string
4159
{
42-
return View::make('permissions-generator::scripts', ['url' => $url])->render();
60+
return View::make('permission-generator::scripts', ['url' => $url])
61+
->render();
4362
}
4463
}

src/PermissionNameServiceProvider.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
use Illuminate\Support\ServiceProvider;
66
use RadiateCode\PermissionNameGenerator\Html\Builder;
7+
use RadiateCode\PermissionNameGenerator\Console\PermissionCacheClearCommand;
78

89
class PermissionNameServiceProvider extends ServiceProvider
910
{
1011
public function register()
1112
{
1213
$this->mergeConfigFrom(
1314
__DIR__.'/../config/permission-generator.php',
14-
'route-permission'
15+
'permission-generator'
1516
);
1617

1718
$this->app->singleton(
@@ -24,15 +25,23 @@ function ($app) {
2425

2526
public function boot()
2627
{
28+
if ($this->app->runningInConsole()) {
29+
$this->commands(
30+
[
31+
PermissionCacheClearCommand::class
32+
]
33+
);
34+
}
35+
2736
$this->loadViewsFrom(
2837
__DIR__.'/../resources/views',
29-
'permissions-generator'
38+
'permission-generator'
3039
);
3140

3241
$this->publishes(
3342
[
3443
__DIR__.'/../resources/views' => resource_path(
35-
'views/vendor/permissions-generator'
44+
'views/vendor/permission-generator'
3645
),
3746
]
3847
);
@@ -41,10 +50,10 @@ public function boot()
4150
[
4251
__DIR__
4352
.'/../config/permission-generator.php' => config_path(
44-
'permissions-generator.php'
53+
'permission-generator.php'
4554
),
4655
],
47-
'permissions-generator-config'
56+
'permission-generator-config'
4857
);
4958
}
5059
}

0 commit comments

Comments
 (0)