1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\PermissionManager\app\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use Backpack\CRUD\app\Http\Controllers\CrudController; |
6
|
|
|
use Backpack\PermissionManager\app\Http\Requests\PermissionStoreCrudRequest as StoreRequest; |
7
|
|
|
use Backpack\PermissionManager\app\Http\Requests\PermissionUpdateCrudRequest as UpdateRequest; |
8
|
|
|
|
9
|
|
|
// VALIDATION |
10
|
|
|
|
11
|
|
|
class PermissionCrudController extends CrudController |
12
|
|
|
{ |
13
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation; |
14
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation; |
15
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation; |
16
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation; |
17
|
|
|
|
18
|
|
View Code Duplication |
public function setup() |
|
|
|
|
19
|
|
|
{ |
20
|
|
|
$this->role_model = $role_model = config('backpack.permissionmanager.models.role'); |
|
|
|
|
21
|
|
|
$this->permission_model = $permission_model = config('backpack.permissionmanager.models.permission'); |
|
|
|
|
22
|
|
|
|
23
|
|
|
$this->crud->setModel($permission_model); |
24
|
|
|
$this->crud->setEntityNameStrings(trans('backpack::permissionmanager.permission_singular'), trans('backpack::permissionmanager.permission_plural')); |
25
|
|
|
$this->crud->setRoute(backpack_url('permission')); |
26
|
|
|
|
27
|
|
|
// deny access according to configuration file |
28
|
|
|
if (config('backpack.permissionmanager.allow_permission_create') == false) { |
29
|
|
|
$this->crud->denyAccess('create'); |
30
|
|
|
} |
31
|
|
|
if (config('backpack.permissionmanager.allow_permission_update') == false) { |
32
|
|
|
$this->crud->denyAccess('update'); |
33
|
|
|
} |
34
|
|
|
if (config('backpack.permissionmanager.allow_permission_delete') == false) { |
35
|
|
|
$this->crud->denyAccess('delete'); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
View Code Duplication |
public function setupListOperation() |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$this->crud->addColumn([ |
42
|
|
|
'name' => 'name', |
43
|
|
|
'label' => trans('backpack::permissionmanager.name'), |
44
|
|
|
'type' => 'text', |
45
|
|
|
]); |
46
|
|
|
|
47
|
|
|
if (config('backpack.permissionmanager.multiple_guards')) { |
48
|
|
|
$this->crud->addColumn([ |
49
|
|
|
'name' => 'guard_name', |
50
|
|
|
'label' => trans('backpack::permissionmanager.guard_type'), |
51
|
|
|
'type' => 'text', |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function setupCreateOperation() |
57
|
|
|
{ |
58
|
|
|
$this->addFields(); |
59
|
|
|
$this->crud->setValidation(StoreRequest::class); |
60
|
|
|
|
61
|
|
|
//otherwise, changes won't have effect |
62
|
|
|
\Cache::forget('spatie.permission.cache'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function setupUpdateOperation() |
66
|
|
|
{ |
67
|
|
|
$this->addFields(); |
68
|
|
|
$this->crud->setValidation(UpdateRequest::class); |
69
|
|
|
|
70
|
|
|
//otherwise, changes won't have effect |
71
|
|
|
\Cache::forget('spatie.permission.cache'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
private function addFields() |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$this->crud->addField([ |
77
|
|
|
'name' => 'name', |
78
|
|
|
'label' => trans('backpack::permissionmanager.name'), |
79
|
|
|
'type' => 'text', |
80
|
|
|
]); |
81
|
|
|
|
82
|
|
|
if (config('backpack.permissionmanager.multiple_guards')) { |
83
|
|
|
$this->crud->addField([ |
84
|
|
|
'name' => 'guard_name', |
85
|
|
|
'label' => trans('backpack::permissionmanager.guard_type'), |
86
|
|
|
'type' => 'select_from_array', |
87
|
|
|
'options' => $this->getGuardTypes(), |
88
|
|
|
]); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/* |
93
|
|
|
* Get an array list of all available guard types |
94
|
|
|
* that have been defined in app/config/auth.php |
95
|
|
|
* |
96
|
|
|
* @return array |
97
|
|
|
**/ |
98
|
|
View Code Duplication |
private function getGuardTypes() |
|
|
|
|
99
|
|
|
{ |
100
|
|
|
$guards = config('auth.guards'); |
101
|
|
|
|
102
|
|
|
$returnable = []; |
103
|
|
|
foreach ($guards as $key => $details) { |
104
|
|
|
$returnable[$key] = $key; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $returnable; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.