Skip to content

Commit 3748fda

Browse files
author
Nur Alam
committed
bug fixed, read me updated
1 parent 3b77e98 commit 3748fda

File tree

6 files changed

+85
-65
lines changed

6 files changed

+85
-65
lines changed

README.md

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
[![Latest Version on Packagist](https://img.shields.io/packagist/v/radiatecode/laravel-permission-name-generator.svg?style=flat-square)](https://packagist.org/packages/radiatecode/laravel-permission-name-generator)
33
[![Total Downloads](https://img.shields.io/packagist/dt/radiatecode/laravel-permission-name-generator.svg?style=flat-square)](https://packagist.org/packages/radiatecode/laravel-permission-name-generator)
44

5-
This package will generate permission names from routes. In many application we create static permission names (ex: create-post, edit-post, delete-post) to check user's accessability, using the package can helps you to generate permission names dynamically.
5+
This package will generate permission names from route names. In many application we create static permission names (ex: create-post, edit-post, delete-post) to check user's accessability, using the package can helps you to generate permission names dynamically.
66
# Requirements
77
- [PHP >= 7.1](https://www.php.net/)
88
- [Laravel 5.7|6.x|7.x|8.x](https://github.com/laravel/framework)
@@ -48,15 +48,17 @@ class OfficeController extends Controller implements WithPermissionGenerator
4848
}
4949
```
5050

51-
> **PermissionGenerator** trait is optional. Because if no permissible title defined, then this package dynamically generate a title based on controller name, And routes can be excluded in the config file.
51+
> **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.
5252
5353
## Get permissions names
5454

5555
RadiateCode\PermissionNameGenerator\Permissions::make()->get();
5656

5757
**Output**
5858

59-
![Stats](img/permissible-routes-output.png)
59+
![Stats](img/permissions.png)
60+
61+
> Notice the key `department-permissions` it generated from `DepartmentController` and permissions are generated from DepartmentController's routes.
6062
6163
## Permission View Builder Facade
6264
The package comes with predefined a view with permission names
@@ -76,9 +78,6 @@ The package comes with predefined a view with permission names
7678
> ```
7779
7880
## Example
79-
### Permissions view
80-
![Stats](img/permission-view.png)
81-
8281
**In controller:**
8382
8483
```php
@@ -99,27 +98,6 @@ class RoleController extends Controller
9998
}
10099
}
101100
```
102-
**Create the permission savings route**
103-
104-
```php
105-
Route::post('/role/{id}/permissions/create',[RoleController::class,'permissionStore'])->name('create-role-permission');
106-
```
107-
```php
108-
use \Illuminate\Http\Request;
109-
class RoleController extends Controller
110-
{
111-
public function permissionStore(Request $request,$id)
112-
{
113-
$role = Role::find($id);
114-
115-
$role->role_permissions = json_encode($request->get('permissions')); // get the submitted permissions
116-
$role->save();
117-
118-
return response()->json('success',201);
119-
}
120-
}
121-
```
122-
123101
**In app/role/permissions.blade.php file:**
124102
```html
125103
@extends('layouts.app')
@@ -147,12 +125,35 @@ class RoleController extends Controller
147125
```
148126

149127
> The layout is only for demo purpose, you should only notice the `$permissionCards` and `$permissionScripts` variables, and put those acording to your view layout.
128+
### Permissions view
129+
![Stats](img/permission-view.png)
130+
131+
**Create the permission savings route**
132+
133+
```php
134+
Route::post('/role/{id}/permissions/create',[RoleController::class,'permissionStore'])->name('create-role-permission');
135+
```
136+
```php
137+
use \Illuminate\Http\Request;
138+
class RoleController extends Controller
139+
{
140+
public function permissionStore(Request $request,$id)
141+
{
142+
$role = Role::find($id);
143+
144+
$role->role_permissions = json_encode($request->get('permissions')); // get the submitted permissions
145+
$role->save();
146+
147+
return response()->json('success',201);
148+
}
149+
}
150+
```
150151

151152
## Configuration
152153

153154
Config the **config/permission-generator.php** file.
154155

155-
1. If route name contains any special char then split the the name by that char. It will use to generate route title. For example if route name is **create.post** then it's title would be **Create Post**
156+
1. If route name contains any special char then split the name by that char. It will use to generate permission title. For example if route name is **create.post** then permission title would be **Create Post**
156157
```php
157158
/**
158159
* Split route name by defined needle
@@ -172,18 +173,26 @@ Config the **config/permission-generator.php** file.
172173
> Example
173174
> ```php
174175
> 'custom-permissions' = [
175-
> 'post-permission' => 'approve-permission',
176-
> 'user-permission' => ['active-user','inactive-user']
176+
> 'user-permission' => ['active-user','inactive-user'],
177+
> 'bonus-permission' => [
178+
> 'name' => 'approve-own-department',
179+
> 'title' => 'Approve Own Department'
180+
> ]
177181
> ]
182+
>
178183
>```
184+
> Note: notice the `user-permission` key which contains only permission name, the package dynamically make a title for the permission name.
185+
>
179186
180-
3. Each route associate with controllers, so you have to define the controller namespace so that the generator can generate permission names from those route associate controllers. By default all the controllers associate routes will be generated as permission names.
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.
181188
182189
```php
183190
/**
184191
* Define controller namespace
185192
*
186-
* [NT: permissions will be generated from those controller which contains the defined prefix]
193+
* By Default permissions will be generated from all controller's routes
194+
*
195+
* [Note: permissions will be generated from those controller which contains the defined prefix]
187196
*/
188197
'controller-namespace-prefixes' => [
189198
'App\Http\Controllers',
@@ -193,14 +202,14 @@ Config the **config/permission-generator.php** file.
193202

194203
```php
195204
/**
196-
* Exclude routes by controller or controller namespace-prefix
205+
* Exclude routes by controller whole namespace or sub/prefix of controller namespace
197206
*
198-
* [NT: We can exclude routes by defining controller name or namespace-prefix. All the routes associated with controller will be excluded]
207+
* By default all auth controller's routes will be excluded from being generated as permission names
208+
*
209+
* [Note: We can exclude routes by defining controller name or namespace-prefix. All the routes associated with controller will be excluded]
199210
*/
200211
'exclude-controllers' => [
201-
/*
202-
* exclude every route which associate with the prefix namespace
203-
*/
212+
// exclude every route which associate with the namespace-prefix of controller
204213
'App\Http\Controllers\Auth',
205214
],
206215
```

config/permission-generator.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
/**
1818
* Define controller namespace
1919
*
20-
* [NT: permissions will be generated from those controller which contains the defined prefix]
20+
* 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]
2123
*/
2224
'controller-namespace-prefixes' => [
2325
'App\Http\Controllers',
@@ -31,14 +33,14 @@
3133
],
3234

3335
/**
34-
* Exclude routes by controller or controller namespace-prefix
36+
* Exclude routes by controller whole namespace or sub/prefix of controller namespace
3537
*
36-
* [NT: We can exclude routes by defining controller name or namespace-prefix. All the routes associated with controller will be excluded]
38+
* By default all auth controller's routes will be excluded from being generated as permission names
39+
*
40+
* [Note: We can exclude routes by defining controller name or namespace-prefix. All the routes associated with controller will be excluded]
3741
*/
3842
'exclude-controllers' => [
39-
/*
40-
* exclude every route which associate with the prefix namespace
41-
*/
43+
// exclude every route which associate with the prefix of controller namespace
4244
'App\Http\Controllers\Auth',
4345
],
4446

@@ -56,4 +58,4 @@
5658
* [NT: Predefined permission cards works on bootstrap]
5759
*/
5860
'card-size-class' => 'col-md-3 col-lg-3 col-sm-12',
59-
];
61+
];

img/permissions.png

175 KB
Loading

resources/views/permission.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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['slug'] }}" id="{{ $route['slug'] }}" {{ in_array($route['slug'],$rolePermissions) ? 'checked' : '' }}>
33-
<label class="form-check-label" for="{{ $route['slug'] }}">{{ $route['name'] }}</label>
32+
<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>
3434
</li>
3535
@endforeach
3636
</ul>

src/Html/Builder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public function view(string $view, array $data = [])
5252

5353
protected function render(): string
5454
{
55+
dd(Permissions::make()->get());
56+
5557
return View::make(
5658
'permission-generator::permission',
5759
[

src/Permissions.php

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ class Permissions
2121

2222
protected $permissions = [];
2323

24+
protected $splitter;
25+
2426
public function __construct()
2527
{
28+
$this->splitter = config('permission-generator.route-name-splitter-needle');
29+
2630
$this->generate();
2731
}
2832

@@ -38,7 +42,7 @@ public function get(): array
3842

3943
public function getOnlyPermissions()
4044
{
41-
if (! $this->hasCachedPermissions()) {
45+
if (!$this->hasCachedPermissions()) {
4246
return $this->onlyPermissions;
4347
}
4448

@@ -61,8 +65,6 @@ protected function generate(): Permissions
6165

6266
$globalExcludedRoutes = config('permission-generator.exclude-routes');
6367

64-
$splitter = config('permission-generator.route-name-splitter-needle');
65-
6668
$routes = Route::getRoutes();
6769

6870
$tempRoutes = [];
@@ -83,15 +85,16 @@ protected function generate(): Permissions
8385

8486
// $routeMiddlewares = $route->gatherMiddleware();
8587

86-
if ($controller == 'Closure'
87-
|| ! $this->isControllerValid($controller)
88+
if (
89+
$controller == 'Closure'
90+
|| !$this->isControllerValid($controller)
8891
|| $this->isExcludedController($controller)
8992
) {
9093
continue;
9194
}
9295

9396

94-
$controllerInstance = app('\\'.$controller);
97+
$controllerInstance = app('\\' . $controller);
9598

9699
// if the controller use the WithPermissible interface then find the excluded routes
97100
if ($controllerInstance instanceof WithPermissionGenerator) {
@@ -111,13 +114,14 @@ protected function generate(): Permissions
111114
continue;
112115
}
113116

117+
// permission group title
114118
$title = $this->generatePermissionTitle($controllerInstance);
115119

116120
$key = strtolower(Str::slug($title, "-"));
117121

118122
$this->permissions[$key][] = [
119-
'slug' => $routeName,
120-
'name' => ucwords(str_replace($splitter, ' ', $routeName)),
123+
'name' => $routeName, // permission name
124+
'title' => ucwords(str_replace($this->splitter, ' ', $routeName)), // permission title
121125
];
122126

123127
$this->onlyPermissions[] = $routeName;
@@ -137,18 +141,21 @@ protected function customPermissions(): Permissions
137141
{
138142
$customPermissions = config('permission-generator.custom-permissions');
139143

140-
if (is_array($customPermissions) && ! empty($customPermissions)) {
144+
if (is_array($customPermissions) && !empty($customPermissions)) {
141145
foreach ($customPermissions as $key => $permission) {
142-
if (array_key_exists(0, $permission)
143-
&& is_array($permission[0])
144-
) {
146+
// when the permission only contains permission name
147+
if (array_key_exists(0, $permission) && is_array($permission)) {
145148
foreach ($permission as $item) {
146-
$this->permissions[$key][] = $item;
149+
$this->permissions[$key][] = [
150+
'name' => $item,
151+
'title' => ucwords(str_replace($this->splitter, ' ', $item)),
152+
];
147153
}
148154

149155
continue;
150156
}
151157

158+
// when permission has valid permission structure (ex: slug, name key available)
152159
$this->permissions[$key][] = $permission;
153160
}
154161
}
@@ -162,7 +169,7 @@ protected function generatePermissionTitle($controllerInstance)
162169
if ($controllerInstance instanceof WithPermissionGenerator) {
163170
$title = $controllerInstance->getPermissionTitle();
164171

165-
if ( ! empty($title)) {
172+
if (!empty($title)) {
166173
return $title;
167174
}
168175
}
@@ -174,10 +181,10 @@ protected function generatePermissionTitle($controllerInstance)
174181
$name = preg_replace('/([a-z])([A-Z])/s', '$1 $2', $controllerName);
175182

176183
if (Str::contains($controllerName, 'Controller')) {
177-
return str_replace('Controller', 'Permission', $name);
184+
return str_replace('Controller', 'Permissions', $name);
178185
}
179186

180-
return $name.' Permission';
187+
return $name . ' Permissions';
181188
}
182189

183190
protected function isExcludedController($controller): bool
@@ -204,7 +211,7 @@ protected function isControllerValid($controller): bool
204211

205212
protected function getCachedPermissions()
206213
{
207-
if ( ! $this->hasCachedPermissions()) {
214+
if (!$this->hasCachedPermissions()) {
208215
return $this->permissions;
209216
}
210217

@@ -219,7 +226,7 @@ protected function hasCachedPermissions(): bool
219226

220227
protected function cachePermissions()
221228
{
222-
if (! $this->hasCachedPermissions() && ! empty($this->permissions)) {
229+
if (!$this->hasCachedPermissions() && !empty($this->permissions)) {
223230
Cache::put(
224231
Constant::CACHE_PERMISSIONS,
225232
$this->permissions,
@@ -233,4 +240,4 @@ protected function cachePermissions()
233240
);
234241
}
235242
}
236-
}
243+
}

0 commit comments

Comments
 (0)