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
|
|
|
|
56
|
|
|
public function setupCreateOperation() |
57
|
|
|
{ |
58
|
|
|
$this->addUserFields(); |
59
|
|
|
$this->crud->setValidation(StoreRequest::class); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setupUpdateOperation() |
63
|
|
|
{ |
64
|
|
|
$this->addUserFields(); |
65
|
|
|
$this->crud->setValidation(UpdateRequest::class); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Store a newly created resource in the database. |
70
|
|
|
* |
71
|
|
|
* @return \Illuminate\Http\RedirectResponse |
72
|
|
|
*/ |
73
|
|
View Code Duplication |
public function store() |
|
|
|
|
74
|
|
|
{ |
75
|
|
|
$this->crud->request = $this->crud->validateRequest(); |
76
|
|
|
$this->crud->request = $this->handlePasswordInput($this->crud->request); |
77
|
|
|
$this->crud->unsetValidation(); // validation has already been run |
78
|
|
|
|
79
|
|
|
return $this->traitStore(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Update the specified resource in the database. |
84
|
|
|
* |
85
|
|
|
* @return \Illuminate\Http\RedirectResponse |
|
|
|
|
86
|
|
|
*/ |
87
|
|
View Code Duplication |
public function update() |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$this->crud->request = $this->crud->validateRequest(); |
90
|
|
|
$this->crud->request = $this->handlePasswordInput($this->crud->request); |
91
|
|
|
$this->crud->unsetValidation(); // validation has already been run |
92
|
|
|
|
93
|
|
|
return $this->traitUpdate(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Handle password input fields. |
98
|
|
|
*/ |
99
|
|
|
protected function handlePasswordInput($request) |
|
|
|
|
100
|
|
|
{ |
101
|
|
|
// Remove fields not present on the user. |
102
|
|
|
$request->request->remove('password_confirmation'); |
103
|
|
|
$request->request->remove('roles_show'); |
104
|
|
|
$request->request->remove('permissions_show'); |
105
|
|
|
|
106
|
|
|
// Encrypt password if specified. |
107
|
|
|
if ($request->input('password')) { |
108
|
|
|
$request->request->set('password', Hash::make($request->input('password'))); |
109
|
|
|
} else { |
110
|
|
|
$request->request->remove('password'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $request; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function addUserFields() |
117
|
|
|
{ |
118
|
|
|
$this->crud->addFields([ |
119
|
|
|
[ |
120
|
|
|
'name' => 'name', |
121
|
|
|
'label' => trans('backpack::permissionmanager.name'), |
122
|
|
|
'type' => 'text', |
123
|
|
|
], |
124
|
|
|
[ |
125
|
|
|
'name' => 'email', |
126
|
|
|
'label' => trans('backpack::permissionmanager.email'), |
127
|
|
|
'type' => 'email', |
128
|
|
|
], |
129
|
|
|
[ |
130
|
|
|
'name' => 'password', |
131
|
|
|
'label' => trans('backpack::permissionmanager.password'), |
132
|
|
|
'type' => 'password', |
133
|
|
|
], |
134
|
|
|
[ |
135
|
|
|
'name' => 'password_confirmation', |
136
|
|
|
'label' => trans('backpack::permissionmanager.password_confirmation'), |
137
|
|
|
'type' => 'password', |
138
|
|
|
], |
139
|
|
|
[ |
140
|
|
|
// two interconnected entities |
141
|
|
|
'label' => trans('backpack::permissionmanager.user_role_permission'), |
142
|
|
|
'field_unique_name' => 'user_role_permission', |
143
|
|
|
'type' => 'checklist_dependency', |
144
|
|
|
'name' => 'roles_and_permissions', // the methods that defines the relationship in your Model |
145
|
|
|
'subfields' => [ |
146
|
|
|
'primary' => [ |
147
|
|
|
'label' => trans('backpack::permissionmanager.roles'), |
148
|
|
|
'name' => 'roles', // the method that defines the relationship in your Model |
149
|
|
|
'entity' => 'roles', // the method that defines the relationship in your Model |
150
|
|
|
'entity_secondary' => 'permissions', // the method that defines the relationship in your Model |
151
|
|
|
'attribute' => 'name', // foreign key attribute that is shown to user |
152
|
|
|
'model' => config('permission.models.role'), // foreign key model |
153
|
|
|
'pivot' => true, // on create&update, do you need to add/delete pivot table entries?] |
154
|
|
|
'number_columns' => 3, //can be 1,2,3,4,6 |
155
|
|
|
], |
156
|
|
|
'secondary' => [ |
157
|
|
|
'label' => ucfirst(trans('backpack::permissionmanager.permission_singular')), |
158
|
|
|
'name' => 'permissions', // the method that defines the relationship in your Model |
159
|
|
|
'entity' => 'permissions', // the method that defines the relationship in your Model |
160
|
|
|
'entity_primary' => 'roles', // the method that defines the relationship in your Model |
161
|
|
|
'attribute' => 'name', // foreign key attribute that is shown to user |
162
|
|
|
'model' => config('permission.models.permission'), // foreign key model |
163
|
|
|
'pivot' => true, // on create&update, do you need to add/delete pivot table entries?] |
164
|
|
|
'number_columns' => 3, //can be 1,2,3,4,6 |
165
|
|
|
], |
166
|
|
|
], |
167
|
|
|
], |
168
|
|
|
]); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
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.