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\RoleStoreCrudRequest as StoreRequest; |
7
|
|
|
use Backpack\PermissionManager\app\Http\Requests\RoleUpdateCrudRequest as UpdateRequest; |
8
|
|
|
|
9
|
|
|
// VALIDATION |
10
|
|
|
|
11
|
|
|
class RoleCrudController 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($role_model); |
24
|
|
|
$this->crud->setEntityNameStrings(trans('backpack::permissionmanager.role'), trans('backpack::permissionmanager.roles')); |
25
|
|
|
$this->crud->setRoute(backpack_url('role')); |
|
|
|
|
26
|
|
|
|
27
|
|
|
// deny access according to configuration file |
28
|
|
|
if (config('backpack.permissionmanager.allow_role_create') == false) { |
29
|
|
|
$this->crud->denyAccess('create'); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
if (config('backpack.permissionmanager.allow_role_update') == false) { |
32
|
|
|
$this->crud->denyAccess('update'); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
if (config('backpack.permissionmanager.allow_role_delete') == false) { |
35
|
|
|
$this->crud->denyAccess('delete'); |
|
|
|
|
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function setupListOperation() |
40
|
|
|
{ |
41
|
|
|
/** |
42
|
|
|
* Show a column for the name of the role. |
43
|
|
|
*/ |
44
|
|
|
$this->crud->addColumn([ |
45
|
|
|
'name' => 'name', |
46
|
|
|
'label' => trans('backpack::permissionmanager.name'), |
47
|
|
|
'type' => 'text', |
48
|
|
|
]); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Show a column with the number of users that have that particular role. |
52
|
|
|
* |
53
|
|
|
* Note: To account for the fact that there can be thousands or millions |
54
|
|
|
* of users for a role, we did not use the `relationship_count` column, |
55
|
|
|
* but instead opted to append a fake `user_count` column to |
56
|
|
|
* the result, using Laravel's `withCount()` method. |
57
|
|
|
* That way, no users are loaded. |
58
|
|
|
*/ |
59
|
|
|
$this->crud->query->withCount('users'); |
60
|
|
|
$this->crud->addColumn([ |
61
|
|
|
'label' => trans('backpack::permissionmanager.users'), |
62
|
|
|
'type' => 'text', |
63
|
|
|
'name' => 'users_count', |
64
|
|
|
'wrapper' => [ |
65
|
|
|
'href' => function ($crud, $column, $entry, $related_key) { |
|
|
|
|
66
|
|
|
return backpack_url('user?role='.$entry->getKey()); |
67
|
|
|
}, |
68
|
|
|
], |
69
|
|
|
'suffix' => ' users', |
70
|
|
|
]); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* In case multiple guards are used, show a column for the guard. |
74
|
|
|
*/ |
75
|
|
|
if (config('backpack.permissionmanager.multiple_guards')) { |
76
|
|
|
$this->crud->addColumn([ |
77
|
|
|
'name' => 'guard_name', |
78
|
|
|
'label' => trans('backpack::permissionmanager.guard_type'), |
79
|
|
|
'type' => 'text', |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Show the exact permissions that role has. |
85
|
|
|
*/ |
86
|
|
|
$this->crud->addColumn([ |
87
|
|
|
// n-n relationship (with pivot table) |
88
|
|
|
'label' => ucfirst(trans('backpack::permissionmanager.permission_plural')), |
89
|
|
|
'type' => 'select_multiple', |
90
|
|
|
'name' => 'permissions', // the method that defines the relationship in your Model |
91
|
|
|
'entity' => 'permissions', // the method that defines the relationship in your Model |
92
|
|
|
'attribute' => 'name', // foreign key attribute that is shown to user |
93
|
|
|
'model' => $this->permission_model, // foreign key model |
94
|
|
|
'pivot' => true, // on create&update, do you need to add/delete pivot table entries? |
95
|
|
|
]); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function setupCreateOperation() |
99
|
|
|
{ |
100
|
|
|
$this->addFields(); |
101
|
|
|
$this->crud->setValidation(StoreRequest::class); |
102
|
|
|
|
103
|
|
|
//otherwise, changes won't have effect |
104
|
|
|
\Cache::forget('spatie.permission.cache'); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function setupUpdateOperation() |
108
|
|
|
{ |
109
|
|
|
$this->addFields(); |
110
|
|
|
$this->crud->setValidation(UpdateRequest::class); |
111
|
|
|
|
112
|
|
|
//otherwise, changes won't have effect |
113
|
|
|
\Cache::forget('spatie.permission.cache'); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function addFields() |
117
|
|
|
{ |
118
|
|
|
$this->crud->addField([ |
119
|
|
|
'name' => 'name', |
120
|
|
|
'label' => trans('backpack::permissionmanager.name'), |
121
|
|
|
'type' => 'text', |
122
|
|
|
]); |
123
|
|
|
|
124
|
|
|
if (config('backpack.permissionmanager.multiple_guards')) { |
125
|
|
|
$this->crud->addField([ |
126
|
|
|
'name' => 'guard_name', |
127
|
|
|
'label' => trans('backpack::permissionmanager.guard_type'), |
128
|
|
|
'type' => 'select_from_array', |
129
|
|
|
'options' => $this->getGuardTypes(), |
130
|
|
|
]); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$this->crud->addField([ |
134
|
|
|
'label' => ucfirst(trans('backpack::permissionmanager.permission_plural')), |
135
|
|
|
'type' => 'checklist', |
136
|
|
|
'name' => 'permissions', |
137
|
|
|
'entity' => 'permissions', |
138
|
|
|
'attribute' => 'name', |
139
|
|
|
'model' => $this->permission_model, |
140
|
|
|
'pivot' => true, |
141
|
|
|
]); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/* |
145
|
|
|
* Get an array list of all available guard types |
146
|
|
|
* that have been defined in app/config/auth.php |
147
|
|
|
* |
148
|
|
|
* @return array |
149
|
|
|
**/ |
150
|
|
View Code Duplication |
private function getGuardTypes() |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$guards = config('auth.guards'); |
153
|
|
|
|
154
|
|
|
$returnable = []; |
155
|
|
|
foreach ($guards as $key => $details) { |
156
|
|
|
$returnable[$key] = $key; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return $returnable; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
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.