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\UserStoreCrudRequest as StoreRequest; |
7
|
|
|
use Backpack\PermissionManager\app\Http\Requests\UserUpdateCrudRequest as UpdateRequest; |
8
|
|
|
use Illuminate\Support\Facades\Hash; |
9
|
|
|
|
10
|
|
|
class UserCrudController extends CrudController |
11
|
|
|
{ |
12
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation; |
13
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { store as traitStore; } |
14
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { update as traitUpdate; } |
15
|
|
|
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation; |
16
|
|
|
|
17
|
|
|
public function setup() |
18
|
|
|
{ |
19
|
|
|
$this->crud->setModel(config('backpack.permissionmanager.models.user')); |
20
|
|
|
$this->crud->setEntityNameStrings(trans('backpack::permissionmanager.user'), trans('backpack::permissionmanager.users')); |
21
|
|
|
$this->crud->setRoute(backpack_url('user')); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function setupListOperation() |
25
|
|
|
{ |
26
|
|
|
$this->crud->setColumns([ |
27
|
|
|
[ |
28
|
|
|
'name' => 'name', |
29
|
|
|
'label' => trans('backpack::permissionmanager.name'), |
30
|
|
|
'type' => 'text', |
31
|
|
|
], |
32
|
|
|
[ |
33
|
|
|
'name' => 'email', |
34
|
|
|
'label' => trans('backpack::permissionmanager.email'), |
35
|
|
|
'type' => 'email', |
36
|
|
|
], |
37
|
|
|
[ // n-n relationship (with pivot table) |
38
|
|
|
'label' => trans('backpack::permissionmanager.roles'), // Table column heading |
39
|
|
|
'type' => 'select_multiple', |
40
|
|
|
'name' => 'roles', // the method that defines the relationship in your Model |
41
|
|
|
'entity' => 'roles', // the method that defines the relationship in your Model |
42
|
|
|
'attribute' => 'name', // foreign key attribute that is shown to user |
43
|
|
|
'model' => config('permission.models.role'), // foreign key model |
44
|
|
|
], |
45
|
|
|
[ // n-n relationship (with pivot table) |
46
|
|
|
'label' => trans('backpack::permissionmanager.extra_permissions'), // Table column heading |
47
|
|
|
'type' => 'select_multiple', |
48
|
|
|
'name' => 'permissions', // the method that defines the relationship in your Model |
49
|
|
|
'entity' => 'permissions', // the method that defines the relationship in your Model |
50
|
|
|
'attribute' => 'name', // foreign key attribute that is shown to user |
51
|
|
|
'model' => config('permission.models.permission'), // foreign key model |
52
|
|
|
], |
53
|
|
|
]); |
54
|
|
|
|
55
|
|
View Code Duplication |
if (config('backpack.base.authentication_column') !== 'email') { |
|
|
|
|
56
|
|
|
$this->crud->addColumn([ |
57
|
|
|
'name' => config('backpack.base.authentication_column'), |
58
|
|
|
'type' => 'text', |
59
|
|
|
'label' => config('backpack.base.authentication_column_name'), |
60
|
|
|
])->afterColumn('name'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Role Filter |
64
|
|
|
$this->crud->addFilter( |
65
|
|
|
[ |
66
|
|
|
'name' => 'role', |
67
|
|
|
'type' => 'dropdown', |
68
|
|
|
'label' => trans('backpack::permissionmanager.role'), |
69
|
|
|
], |
70
|
|
|
config('permission.models.role')::all()->pluck('name', 'id')->toArray(), |
71
|
|
|
function ($value) { // if the filter is active |
72
|
|
|
$this->crud->addClause('whereHas', 'roles', function ($query) use ($value) { |
73
|
|
|
$query->where('role_id', '=', $value); |
74
|
|
|
}); |
75
|
|
|
} |
76
|
|
|
); |
77
|
|
|
|
78
|
|
|
// Extra Permission Filter |
79
|
|
|
$this->crud->addFilter( |
80
|
|
|
[ |
81
|
|
|
'name' => 'permissions', |
82
|
|
|
'type' => 'select2', |
83
|
|
|
'label' => trans('backpack::permissionmanager.extra_permissions'), |
84
|
|
|
], |
85
|
|
|
config('permission.models.permission')::all()->pluck('name', 'id')->toArray(), |
86
|
|
|
function ($value) { // if the filter is active |
87
|
|
|
$this->crud->addClause('whereHas', 'permissions', function ($query) use ($value) { |
88
|
|
|
$query->where('permission_id', '=', $value); |
89
|
|
|
}); |
90
|
|
|
} |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function setupCreateOperation() |
95
|
|
|
{ |
96
|
|
|
$this->addUserFields(); |
97
|
|
|
$this->crud->setValidation(StoreRequest::class); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function setupUpdateOperation() |
101
|
|
|
{ |
102
|
|
|
$this->addUserFields(); |
103
|
|
|
$this->crud->setValidation(UpdateRequest::class); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Store a newly created resource in the database. |
108
|
|
|
* |
109
|
|
|
* @return \Illuminate\Http\RedirectResponse |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
public function store() |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$this->crud->setRequest($this->crud->validateRequest()); |
114
|
|
|
$this->crud->setRequest($this->handlePasswordInput($this->crud->getRequest())); |
115
|
|
|
$this->crud->unsetValidation(); // validation has already been run |
116
|
|
|
|
117
|
|
|
return $this->traitStore(); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Update the specified resource in the database. |
122
|
|
|
* |
123
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
|
|
|
124
|
|
|
*/ |
125
|
|
View Code Duplication |
public function update() |
|
|
|
|
126
|
|
|
{ |
127
|
|
|
$this->crud->setRequest($this->crud->validateRequest()); |
128
|
|
|
$this->crud->setRequest($this->handlePasswordInput($this->crud->getRequest())); |
129
|
|
|
$this->crud->unsetValidation(); // validation has already been run |
130
|
|
|
|
131
|
|
|
return $this->traitUpdate(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Handle password input fields. |
136
|
|
|
*/ |
137
|
|
|
protected function handlePasswordInput($request) |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
// Remove fields not present on the user. |
140
|
|
|
$request->request->remove('password_confirmation'); |
141
|
|
|
$request->request->remove('roles_show'); |
142
|
|
|
$request->request->remove('permissions_show'); |
143
|
|
|
|
144
|
|
|
// Encrypt password if specified. |
145
|
|
|
if ($request->input('password')) { |
146
|
|
|
$request->request->set('password', Hash::make($request->input('password'))); |
147
|
|
|
} else { |
148
|
|
|
$request->request->remove('password'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $request; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected function addUserFields() |
155
|
|
|
{ |
156
|
|
|
$this->crud->addFields([ |
157
|
|
|
[ |
158
|
|
|
'name' => 'name', |
159
|
|
|
'label' => trans('backpack::permissionmanager.name'), |
160
|
|
|
'type' => 'text', |
161
|
|
|
], |
162
|
|
|
[ |
163
|
|
|
'name' => 'email', |
164
|
|
|
'label' => trans('backpack::permissionmanager.email'), |
165
|
|
|
'type' => 'email', |
166
|
|
|
], |
167
|
|
|
[ |
168
|
|
|
'name' => 'password', |
169
|
|
|
'label' => trans('backpack::permissionmanager.password'), |
170
|
|
|
'type' => 'password', |
171
|
|
|
], |
172
|
|
|
[ |
173
|
|
|
'name' => 'password_confirmation', |
174
|
|
|
'label' => trans('backpack::permissionmanager.password_confirmation'), |
175
|
|
|
'type' => 'password', |
176
|
|
|
], |
177
|
|
|
[ |
178
|
|
|
// two interconnected entities |
179
|
|
|
'label' => trans('backpack::permissionmanager.user_role_permission'), |
180
|
|
|
'field_unique_name' => 'user_role_permission', |
181
|
|
|
'type' => 'checklist_dependency', |
182
|
|
|
'name' => ['roles', 'permissions'], |
183
|
|
|
'subfields' => [ |
184
|
|
|
'primary' => [ |
185
|
|
|
'label' => trans('backpack::permissionmanager.roles'), |
186
|
|
|
'name' => 'roles', // the method that defines the relationship in your Model |
187
|
|
|
'entity' => 'roles', // the method that defines the relationship in your Model |
188
|
|
|
'entity_secondary' => 'permissions', // the method that defines the relationship in your Model |
189
|
|
|
'attribute' => 'name', // foreign key attribute that is shown to user |
190
|
|
|
'model' => config('permission.models.role'), // foreign key model |
191
|
|
|
'pivot' => true, // on create&update, do you need to add/delete pivot table entries?] |
192
|
|
|
'number_columns' => 3, //can be 1,2,3,4,6 |
193
|
|
|
], |
194
|
|
|
'secondary' => [ |
195
|
|
|
'label' => ucfirst(trans('backpack::permissionmanager.permission_singular')), |
196
|
|
|
'name' => 'permissions', // the method that defines the relationship in your Model |
197
|
|
|
'entity' => 'permissions', // the method that defines the relationship in your Model |
198
|
|
|
'entity_primary' => 'roles', // the method that defines the relationship in your Model |
199
|
|
|
'attribute' => 'name', // foreign key attribute that is shown to user |
200
|
|
|
'model' => config('permission.models.permission'), // foreign key model |
201
|
|
|
'pivot' => true, // on create&update, do you need to add/delete pivot table entries?] |
202
|
|
|
'number_columns' => 3, //can be 1,2,3,4,6 |
203
|
|
|
], |
204
|
|
|
], |
205
|
|
|
], |
206
|
|
|
]); |
207
|
|
|
|
208
|
|
View Code Duplication |
if (config('backpack.base.authentication_column') !== 'email') { |
|
|
|
|
209
|
|
|
$this->crud->addField([ |
210
|
|
|
'name' => config('backpack.base.authentication_column'), |
211
|
|
|
'type' => 'text', |
212
|
|
|
'label' => config('backpack.base.authentication_column_name'), |
213
|
|
|
])->afterField('name'); |
214
|
|
|
} |
215
|
|
|
} |
216
|
|
|
} |
217
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.