1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests\ChangePasswordRequest; |
6
|
|
|
use App\Http\Requests\UserAddRequest; |
7
|
|
|
use App\Models\User; |
8
|
|
|
use App\Services\UserTableService; |
9
|
|
|
use Illuminate\Support\Facades\Auth; |
10
|
|
|
use Illuminate\Support\Facades\Hash; |
11
|
|
|
|
12
|
|
|
class UserController extends Controller |
13
|
|
|
{ |
14
|
|
|
protected $userTableService; |
15
|
|
|
|
16
|
12 |
|
public function __construct(UserTableService $userTableService) |
17
|
|
|
{ |
18
|
12 |
|
$this->userTableService = $userTableService; |
19
|
12 |
|
} |
20
|
|
|
|
21
|
4 |
|
public function index() |
22
|
|
|
{ |
23
|
4 |
|
$title = trans('general.users'); |
24
|
|
|
|
25
|
4 |
|
$dataset = User::select('id', 'name', 'email', 'created_at') |
26
|
4 |
|
->paginate($this->getItemsPerPage()); |
27
|
|
|
|
28
|
4 |
|
if ($dataset->isEmpty()) { |
29
|
1 |
|
$this->addFlashMessage(trans('general.no_users_in_database'), 'alert-danger'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$viewData = [ |
33
|
4 |
|
'columns' => $this->userTableService->getColumns(), |
34
|
4 |
|
'dataset' => $dataset, |
35
|
4 |
|
'routeName' => $this->userTableService->getRouteName(), |
36
|
4 |
|
'title' => $title, |
37
|
|
|
'disableEdit' => true, |
38
|
|
|
]; |
39
|
|
|
|
40
|
4 |
|
return view('list', $viewData); |
41
|
|
|
} |
42
|
|
|
|
43
|
6 |
|
public function showAddForm() |
44
|
|
|
{ |
45
|
6 |
|
$dataset = new User(); |
46
|
6 |
|
$title = trans('navigation.add_user'); |
47
|
|
|
|
48
|
|
|
$viewData = [ |
49
|
6 |
|
'dataset' => $dataset, |
50
|
6 |
|
'fields' => $this->getFields(), |
51
|
6 |
|
'title' => $title, |
52
|
6 |
|
'submitRoute' => route($this->userTableService->getRouteName().'.postadd'), |
53
|
6 |
|
'routeName' => $this->userTableService->getRouteName(), |
54
|
|
|
]; |
55
|
|
|
|
56
|
6 |
|
return view('addedit', $viewData); |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
public function postAdd(UserAddRequest $request) |
60
|
|
|
{ |
61
|
2 |
|
$object = new User(); |
62
|
|
|
|
63
|
2 |
|
$request->merge(['password' => Hash::make($request->password)]); |
64
|
2 |
|
$object->fill($request->all()); |
65
|
2 |
|
$object->save(); |
66
|
|
|
|
67
|
2 |
|
return redirect()->route($this->userTableService->getRouteName().'.index') |
68
|
2 |
|
->with([ |
69
|
2 |
|
'message' => trans('general.saved'), |
70
|
2 |
|
'alert-class' => 'alert-success', |
71
|
|
|
]); |
72
|
|
|
} |
73
|
|
|
|
74
|
3 |
|
public function delete($objectId) |
75
|
|
|
{ |
76
|
3 |
|
$object = User::find($objectId); |
77
|
3 |
|
$data = ['class' => 'alert-success', 'message' => trans('general.deleted')]; |
78
|
|
|
|
79
|
3 |
|
if (!$object || $object->id === 1 || $object->id = Auth::user()->id) { |
80
|
2 |
|
$data = ['class' => 'alert-danger', 'message' => trans('general.cannot_delete_object')]; |
81
|
|
|
} else { |
82
|
1 |
|
$object->delete(); |
83
|
|
|
} |
84
|
|
|
|
85
|
3 |
|
return response()->json($data); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
public function changePassword() |
89
|
|
|
{ |
90
|
1 |
|
return view('auth.passwords.change'); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
public function postChangePassword(ChangePasswordRequest $request) |
94
|
|
|
{ |
95
|
1 |
|
$user = Auth::user(); |
96
|
1 |
|
$user->password = Hash::make($request->new_password); |
97
|
1 |
|
$user->save(); |
98
|
|
|
|
99
|
1 |
|
return redirect()->route($this->userTableService->getRouteName().'.change_password') |
100
|
1 |
|
->with([ |
101
|
1 |
|
'message' => trans('general.saved'), |
102
|
1 |
|
'alert-class' => 'alert-success', |
103
|
|
|
]); |
104
|
|
|
} |
105
|
|
|
|
106
|
6 |
|
public function getFields() |
107
|
|
|
{ |
108
|
|
|
return [ |
109
|
|
|
[ |
110
|
6 |
|
'id' => 'name', |
111
|
6 |
|
'title' => trans('general.name'), |
112
|
6 |
|
'value' => function (User $data) { |
113
|
6 |
|
return $data->name; |
114
|
6 |
|
}, |
115
|
|
|
'optional' => [ |
116
|
|
|
'required' => 'required', |
117
|
|
|
], |
118
|
|
|
], |
119
|
|
|
[ |
120
|
6 |
|
'id' => 'email', |
121
|
6 |
|
'title' => trans('auth.email'), |
122
|
6 |
|
'value' => function (User $data) { |
123
|
6 |
|
return $data->email; |
124
|
6 |
|
}, |
125
|
6 |
|
'type' => 'email', |
126
|
|
|
'optional' => [ |
127
|
|
|
'required' => 'required', |
128
|
|
|
], |
129
|
|
|
], |
130
|
|
|
[ |
131
|
6 |
|
'id' => 'password', |
132
|
6 |
|
'title' => trans('auth.password'), |
133
|
6 |
|
'type' => 'password', |
134
|
|
|
'optional' => [ |
135
|
|
|
'required' => 'required', |
136
|
|
|
], |
137
|
|
|
], |
138
|
|
|
[ |
139
|
6 |
|
'id' => 'password_confirmation', |
140
|
6 |
|
'title' => trans('auth.password_confirmation'), |
141
|
6 |
|
'type' => 'password', |
142
|
|
|
'optional' => [ |
143
|
|
|
'required' => 'required', |
144
|
|
|
], |
145
|
|
|
], |
146
|
|
|
[ |
147
|
6 |
|
'id' => 'is_admin', |
148
|
6 |
|
'title' => trans('general.administrator'), |
149
|
6 |
|
'value' => function () { |
150
|
6 |
|
return true; |
151
|
6 |
|
}, |
152
|
6 |
|
'type' => 'checkbox', |
153
|
|
|
], |
154
|
|
|
]; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|