1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Admin; |
4
|
|
|
|
5
|
|
|
use DB; |
6
|
|
|
use Mail; |
7
|
|
|
// use App\Role; |
8
|
|
|
use App\User; |
9
|
|
|
use Carbon\Carbon; |
10
|
|
|
use App\UserInitialize; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
use App\Mail\InitializeUser; |
13
|
|
|
use Illuminate\Http\Request; |
14
|
|
|
use Illuminate\Validation\Rule; |
15
|
|
|
use Illuminate\Support\Facades\Log; |
16
|
|
|
use Illuminate\Support\Facades\Auth; |
17
|
|
|
use App\Http\Controllers\Controller; |
18
|
|
|
use Illuminate\Support\Facades\Route; |
19
|
|
|
use Illuminate\Support\Facades\Notification; |
20
|
|
|
use App\Notifications\NewUserEmail; |
21
|
|
|
|
22
|
|
|
use App\UserRoleType; |
23
|
|
|
use App\UserLogins; |
24
|
|
|
use App\Http\Resources\UserCollection; |
25
|
|
|
use App\Http\Resources\User as UserResource; |
26
|
|
|
|
27
|
|
|
class UserController extends Controller |
28
|
|
|
{ |
29
|
|
|
private $user; |
30
|
|
|
// Constructor sets up middleware |
31
|
112 |
|
public function __construct() |
32
|
|
|
{ |
33
|
112 |
|
$this->middleware('auth')->except('initializeUser', 'submitInitializeUser'); |
34
|
|
|
$this->middleware(function ($request, $next) { |
35
|
96 |
|
$this->user = auth()->user(); // TODO - is this correct???? |
36
|
96 |
|
$this->authorize('hasAccess', 'Manage Users'); |
37
|
80 |
|
return $next($request); |
38
|
112 |
|
}); |
39
|
112 |
|
} |
40
|
|
|
|
41
|
|
|
// Show the list of current users to edit |
42
|
2 |
|
public function index() |
43
|
|
|
{ |
44
|
|
|
$userList = new UserCollection(User::where('active', 1)->with(['UserLogins' => function ($query) { |
45
|
2 |
|
$query->latest()->limit(1); |
46
|
2 |
|
}])->get() |
47
|
|
|
/** @scrutinizer ignore-call */ |
48
|
2 |
|
->makeVisible('user_id')); |
49
|
2 |
|
$route = 'admin.user.edit'; |
50
|
|
|
|
51
|
2 |
|
return view('admin.userIndex', [ |
52
|
2 |
|
'userList' => $userList, |
53
|
2 |
|
'route' => $route, |
54
|
|
|
// 'method' => 'edit', |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// Check if a username is in use |
59
|
12 |
|
public function checkUser($username, $type) |
60
|
|
|
{ |
61
|
12 |
|
$user = User::where($type, $username)->first(); |
62
|
|
|
|
63
|
12 |
|
if(!$user) |
64
|
|
|
{ |
65
|
4 |
|
return response()->json(['duplicate' => false]); |
66
|
|
|
} |
67
|
|
|
|
68
|
8 |
|
return response()->json([ |
69
|
8 |
|
'duplicate' => true, |
70
|
8 |
|
'user' => $user->full_name, |
71
|
8 |
|
'active' => $user->active, |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// Show the Add User form |
76
|
2 |
|
public function create() |
77
|
|
|
{ |
78
|
2 |
|
$roles = UserRoleType::all(); // Role::all(); |
79
|
|
|
|
80
|
2 |
|
$roleArr = []; |
81
|
2 |
|
foreach($roles as $role) |
82
|
|
|
{ |
83
|
2 |
|
if($role->role_id == 1 && Auth::user()->role_id != 1) |
84
|
|
|
{ |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
2 |
|
else if($role->role_id == 2 && Auth::user()->role_id > 1) |
88
|
|
|
{ |
89
|
|
|
continue; |
90
|
|
|
} |
91
|
|
|
else |
92
|
|
|
{ |
93
|
|
|
// $roleArr[$role->role_id] = $role->name; |
94
|
2 |
|
$roleArr[] = [ |
95
|
2 |
|
'value' => $role->role_id, |
96
|
2 |
|
'text' => $role->name, |
97
|
|
|
]; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
102
|
2 |
|
return view('admin.newUser', [ |
103
|
2 |
|
'roles' => $roleArr |
104
|
|
|
]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
// Submit the Add User form |
108
|
16 |
|
public function store(Request $request) |
109
|
|
|
{ |
110
|
|
|
// Validate the new user form |
111
|
16 |
|
$request->validate([ |
112
|
16 |
|
'role' => 'required|numeric', // TODO - add validation rule - is in user roles table |
113
|
|
|
'username' => 'required|unique:users|regex:/^[a-zA-Z0-9_]*$/', |
114
|
|
|
'first_name' => 'required', |
115
|
|
|
'last_name' => 'required', |
116
|
|
|
'email' => 'required|unique:users', |
117
|
|
|
]); |
118
|
|
|
|
119
|
|
|
// Create the user |
120
|
2 |
|
$newUser = User::create([ |
121
|
2 |
|
'role_id' => $request->role, |
122
|
2 |
|
'username' => $request->username, |
123
|
2 |
|
'first_name' => $request->first_name, |
124
|
2 |
|
'last_name' => $request->last_name, |
125
|
2 |
|
'email' => $request->email, |
126
|
2 |
|
'password' => bcrypt(strtolower(Str::random(15))), |
127
|
2 |
|
'active' => 1 |
128
|
|
|
]); |
129
|
|
|
|
130
|
2 |
|
$userID = $newUser->user_id; |
131
|
|
|
|
132
|
|
|
// Create the setup user link |
133
|
2 |
|
$hash = strtolower(Str::random(30)); |
134
|
2 |
|
UserInitialize::create([ |
135
|
2 |
|
'username' => $request->username, |
136
|
2 |
|
'token' => $hash |
137
|
|
|
]); |
138
|
|
|
|
139
|
|
|
// Email the new user |
140
|
|
|
// Mail::to($request->email)->send(new InitializeUser($hash, $request->username, $request->first_name.' '.$request->last_name)); |
141
|
2 |
|
Notification::send($newUser, new NewUserEmail($newUser, $hash)); |
142
|
|
|
|
143
|
2 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
144
|
2 |
|
Log::debug('User Data - ', $newUser->toArray()); |
145
|
2 |
|
Log::notice('New User ID-'.$userID.' Created by ID-'.Auth::user()->user_id); |
146
|
|
|
|
147
|
|
|
// return redirect()->back()->with('success', 'New User Created'); |
148
|
2 |
|
return response()->json(['success' => true]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// List all active or inactive users |
152
|
|
|
// public function show($type) |
153
|
|
|
// { |
154
|
|
|
// $userList = []; |
155
|
|
|
// $route = ''; |
156
|
|
|
|
157
|
|
|
// switch($type) |
158
|
|
|
// { |
159
|
|
|
// case 'active': |
160
|
|
|
// $userList = new UserCollection(User::where('active', 1)->with(['UserLogins' => function($query) |
161
|
|
|
// { |
162
|
|
|
// $query->latest()->limit(1); |
163
|
|
|
// }])->get() |
164
|
|
|
// /** @scrutinizer ignore-call */ |
165
|
|
|
// ->makeVisible('user_id')); |
166
|
|
|
// $route = 'admin.user.edit'; |
167
|
|
|
// break; |
168
|
|
|
// default: |
169
|
|
|
// abort(404); |
170
|
|
|
// } |
171
|
|
|
|
172
|
|
|
// // return $userList; |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
// return view('admin.userIndex', [ |
176
|
|
|
// 'userList' => $userList, |
177
|
|
|
// 'route' => $route, |
178
|
|
|
// // 'method' => 'edit', |
179
|
|
|
// ]); |
180
|
|
|
|
181
|
|
|
// } |
182
|
|
|
|
183
|
|
|
// Open the edit user form |
184
|
6 |
|
public function edit($id) |
185
|
|
|
{ |
186
|
6 |
|
$roles = UserRoleType::all(); // Role::all(); |
187
|
6 |
|
$user = new UserResource(User::findOrFail($id)); |
188
|
|
|
|
189
|
|
|
// Make sure that the user is not trying to deactivate someone with more permissions |
190
|
4 |
|
if ($user->role_id < Auth::user()->role_id) |
191
|
|
|
{ |
192
|
2 |
|
return abort(403); |
|
|
|
|
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
// Good to go - update user password |
196
|
2 |
|
$roleArr = []; |
197
|
2 |
|
foreach ($roles as $role) { |
198
|
2 |
|
if ($role->role_id == 1 && Auth::user()->role_id != 1) { |
199
|
|
|
continue; |
200
|
2 |
|
} else if ($role->role_id == 2 && Auth::user()->role_id > 1) { |
201
|
|
|
continue; |
202
|
|
|
} else { |
203
|
|
|
// $roleArr[$role->role_id] = $role->name; |
204
|
2 |
|
$roleArr[] = [ |
205
|
2 |
|
'value' => $role->role_id, |
206
|
2 |
|
'text' => $role->name, |
207
|
|
|
]; |
208
|
|
|
} |
209
|
|
|
} |
210
|
|
|
|
211
|
2 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by User ID-' . Auth::user()->user_id); |
212
|
2 |
|
return view('admin.userEdit', [ |
213
|
2 |
|
'roles' => $roleArr, |
214
|
2 |
|
'user' => $user->makeVisible(['user_id', 'username']), |
|
|
|
|
215
|
|
|
]); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
// Submit the update user form |
219
|
22 |
|
public function update(Request $request, $id) |
220
|
|
|
{ |
221
|
22 |
|
$request->validate([ |
222
|
|
|
'username' => [ |
223
|
22 |
|
'required', |
224
|
22 |
|
Rule::unique('users')->ignore($id, 'user_id') |
225
|
|
|
], |
226
|
22 |
|
'first_name' => 'required', |
227
|
22 |
|
'last_name' => 'required', |
228
|
|
|
'email' => [ |
229
|
22 |
|
'required', |
230
|
22 |
|
Rule::unique('users')->ignore($id, 'user_id') |
231
|
|
|
], |
232
|
22 |
|
'role' => 'required', |
233
|
|
|
]); |
234
|
|
|
|
235
|
|
|
// Update the user data |
236
|
8 |
|
$user = User::findOrFail($id); |
237
|
|
|
|
238
|
6 |
|
if ($user->role_id < Auth::user()->role_id) |
239
|
|
|
{ |
240
|
2 |
|
return abort(403); |
|
|
|
|
241
|
|
|
} |
242
|
|
|
|
243
|
4 |
|
$user->update( |
244
|
|
|
[ |
245
|
4 |
|
'username' => $request->username, |
246
|
4 |
|
'first_name' => $request->first_name, |
247
|
4 |
|
'last_name' => $request->last_name, |
248
|
4 |
|
'email' => $request->email, |
249
|
4 |
|
'role_id' => $request->role, |
250
|
|
|
]); |
251
|
|
|
|
252
|
|
|
// Update the user's role |
253
|
4 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
254
|
4 |
|
Log::debug('Edit user form submitted for User ID-'.$id.' Data - ', $request->toArray()); |
255
|
4 |
|
Log::notice('User ID-'.$id.' has updated their information.'); |
256
|
4 |
|
return response()->json(['success' => true]); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
// Submit the change password form |
260
|
12 |
|
public function submitPassword(Request $request) |
261
|
|
|
{ |
262
|
12 |
|
$request->validate([ |
263
|
12 |
|
'password' => 'required|string|min:6|confirmed', |
264
|
|
|
'user_id' => 'required', |
265
|
|
|
]); |
266
|
|
|
|
267
|
|
|
// $nextChange = isset($request->force_change) && $request->force_change == 'on' ? Carbon::now()->subDay() : null; |
268
|
|
|
|
269
|
8 |
|
if($request->force_change) |
270
|
|
|
{ |
271
|
6 |
|
$nextChange = Carbon::now()->subDay(); |
272
|
|
|
} |
273
|
|
|
else |
274
|
|
|
{ |
275
|
2 |
|
$nextChange = config('users.passExpires') != null ? Carbon::now()->addDays(config('users.passExpires')) : null; |
276
|
|
|
} |
277
|
|
|
|
278
|
8 |
|
$user = User::find($request->user_id); |
279
|
|
|
|
280
|
|
|
// Verify this is a valid user ID |
281
|
8 |
|
if (!$user) { |
282
|
2 |
|
$success = false; |
283
|
2 |
|
$reason = 'Cannot find user with this ID'; |
284
|
|
|
} |
285
|
|
|
// Make sure that the user is not trying to deactivate someone with more permissions |
286
|
6 |
|
else if ($user->role_id < Auth::user()->role_id) { |
287
|
2 |
|
$success = false; |
288
|
2 |
|
$reason = 'You cannot change password for a user with higher permissions that you. If this user has locked themselves out, have then use the reset link on the login page.'; |
289
|
|
|
} |
290
|
|
|
// Good to go - update user password |
291
|
|
|
else { |
292
|
|
|
// Update the user data |
293
|
4 |
|
$user->update( |
294
|
|
|
[ |
295
|
4 |
|
'password' => bcrypt($request->password), |
296
|
4 |
|
'password_expires' => $nextChange |
297
|
|
|
]); |
298
|
4 |
|
$success = true; |
299
|
4 |
|
$reason = 'Password for ' . $user->full_name . ' successfully reset.'; |
300
|
|
|
} |
301
|
|
|
|
302
|
8 |
|
Log::debug('Route ' . Route::currentRouteName() . ' visited by User ID-' . Auth::user()->user_id); |
303
|
8 |
|
Log::notice('User ID-' . $request->user_id . ' password chagned by ' . Auth::user()->user_id, [ |
304
|
8 |
|
'success' => $success, |
305
|
8 |
|
'reason' => $reason, |
306
|
|
|
]); |
307
|
|
|
|
308
|
8 |
|
return response()->json([ |
309
|
8 |
|
'success' => $success, |
310
|
8 |
|
'reason' => $reason, |
311
|
|
|
]); |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
// Disable the user |
315
|
8 |
|
public function destroy($id) |
316
|
|
|
{ |
317
|
8 |
|
$user = User::find($id); |
318
|
|
|
|
319
|
|
|
// Verify this is a valid user ID |
320
|
8 |
|
if(!$user) |
321
|
|
|
{ |
322
|
2 |
|
$success = false; |
323
|
2 |
|
$reason = 'Cannot find user with this ID'; |
324
|
|
|
} |
325
|
|
|
// Make suer that the user is not trying to deactivate themselves |
326
|
6 |
|
else if(Auth::user()->user_id == $id) |
327
|
|
|
{ |
328
|
2 |
|
$success = false; |
329
|
2 |
|
$reason = 'You cannot deactivate yourself'; |
330
|
|
|
} |
331
|
|
|
// Make sure that the user is not trying to deactivate someone with more permissions |
332
|
4 |
|
else if($user->role_id < Auth::user()->role_id) |
333
|
|
|
{ |
334
|
2 |
|
$success = false; |
335
|
2 |
|
$reason = 'You cannot deactivate a user with higher permissions that you.'; |
336
|
|
|
} |
337
|
|
|
// Good to go - deactivate user |
338
|
|
|
else |
339
|
|
|
{ |
340
|
2 |
|
$user->update(['active' => 0]); |
341
|
2 |
|
$success = true; |
342
|
2 |
|
$reason = 'User '.$user->full_name.' successfully deactivated.'; |
343
|
|
|
} |
344
|
|
|
|
345
|
8 |
|
Log::debug('Route '.Route::currentRouteName().' visited by User ID-'.Auth::user()->user_id); |
346
|
8 |
|
Log::notice('User ID-'.$id.' disabled by '.Auth::user()->user_id, [ |
347
|
8 |
|
'success' => $success, |
348
|
8 |
|
'reason' => $reason, |
349
|
|
|
]); |
350
|
|
|
|
351
|
8 |
|
return response()->json([ |
352
|
8 |
|
'success' => $success, |
353
|
8 |
|
'reason' => $reason, |
354
|
|
|
]); |
355
|
|
|
} |
356
|
|
|
} |
357
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.