-
Notifications
You must be signed in to change notification settings - Fork 9
/
UserCrudController.php
133 lines (117 loc) · 4.18 KB
/
UserCrudController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
namespace EduardoArandaH\UserManager\app\Http\Controllers;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Http\Requests\CrudRequest;
use EduardoArandaH\UserManager\app\Http\Requests\UserStoreCrudRequest as StoreRequest;
use EduardoArandaH\UserManager\app\Http\Requests\UserUpdateCrudRequest as UpdateRequest;
class UserCrudController extends CrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation { store as traitStore; }
use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation { update as traitUpdate; }
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
public function setup()
{
$this->crud->setModel(config('eduardoarandah.usermanager.user_model'));
$this->crud->setEntityNameStrings(trans('eduardoarandah::usermanager.user'), trans('eduardoarandah::usermanager.users'));
$this->crud->setRoute(config('backpack.base.route_prefix').'/user');
}
protected function setupListOperation()
{
$this->crud->setColumns([
[
'name' => 'name',
'label' => trans('eduardoarandah::usermanager.name'),
'type' => 'text',
],
[
'name' => 'email',
'label' => trans('eduardoarandah::usermanager.email'),
'type' => 'email',
]
]);
}
protected function setupCreateOperation()
{
$this->crud->setValidation(StoreRequest::class);
$this->addFields();
}
protected function setupUpdateOperation()
{
$this->crud->setValidation(UpdateRequest::class);
$this->addFields();
}
/**
* Store a newly created resource in the database.
*
* @param StoreRequest $request - type injection used for validation using Requests
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store(StoreRequest $request)
{
$this->handlePasswordInput($request);
return $this->traitStore($request);
}
/**
* Update the specified resource in the database.
*
* @param UpdateRequest $request - type injection used for validation using Requests
*
* @return \Illuminate\Http\RedirectResponse
*/
public function update(UpdateRequest $request)
{
$this->handlePasswordInput($request);
return $this->traitUpdate($request);
}
/**
* Add the fields needed in the Create and Update operations.
*/
protected function addFields()
{
$this->crud->addFields([
[
'name' => 'name',
'label' => trans('eduardoarandah::usermanager.name'),
'type' => 'text',
],
[
'name' => 'email',
'label' => trans('eduardoarandah::usermanager.email'),
'type' => 'email',
],
[
'name' => 'password',
'label' => trans('eduardoarandah::usermanager.password'),
'type' => 'password',
],
[
'name' => 'password_confirmation',
'label' => trans('eduardoarandah::usermanager.password_confirmation'),
'type' => 'password',
]
]);
}
/**
* Handle password input fields.
*
* @param CrudRequest $request
*/
protected function handlePasswordInput(CrudRequest $request)
{
$crud_request = $this->crud->getRequest();
// If a password was specified
if ($request->input('password')) {
// encrypt it before storing it
$hashed_password = bcrypt($request->input('password'));
$crud_request->request->set('password', $hashed_password);
$crud_request->request->set('password_confirmation', $hashed_password);
} else {
// ignore the password inputs entirely
$crud_request->request->remove('password');
$crud_request->request->remove('password_confirmation');
}
$this->crud->setRequest($crud_request);
}
}