1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Devpri\Tinre\Http\Controllers\Web; |
4
|
|
|
|
5
|
|
|
use Devpri\Tinre\Http\Controllers\Controller; |
6
|
|
|
use Devpri\Tinre\Http\Resources\Web\User as UserResource; |
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Illuminate\Support\Facades\Hash; |
9
|
|
|
use Illuminate\Validation\ValidationException; |
10
|
|
|
|
11
|
|
|
class LoggedUserController extends Controller |
12
|
|
|
{ |
13
|
1 |
|
public function show(Request $request) |
14
|
|
|
{ |
15
|
1 |
|
$user = $request->user(); |
16
|
|
|
|
17
|
1 |
|
return new UserResource($user); |
18
|
|
|
} |
19
|
|
|
|
20
|
2 |
|
public function update(Request $request) |
21
|
|
|
{ |
22
|
2 |
|
$user = $request->user(); |
23
|
|
|
|
24
|
2 |
|
if ($user->cant('updateOwn', $user)) { |
25
|
|
|
abort(401); |
26
|
|
|
} |
27
|
|
|
|
28
|
2 |
|
$validatedData = $request->validate([ |
29
|
2 |
|
'name' => ['required', 'string', 'max:255'], |
30
|
|
|
'current_password' => ['nullable', 'required_with:new_password'], |
31
|
|
|
'new_password' => ['nullable', 'required_with:current_password', 'min:6'], |
32
|
|
|
]); |
33
|
|
|
|
34
|
2 |
|
if ($validatedData['new_password'] && ! Hash::check($validatedData['current_password'], $user->password)) { |
35
|
1 |
|
throw ValidationException::withMessages([ |
36
|
1 |
|
'current_password' => [trans('The current password is invalid.')], |
37
|
|
|
]); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
if ($validatedData['new_password']) { |
41
|
1 |
|
$user->password = bcrypt($validatedData['new_password']); |
42
|
|
|
} |
43
|
|
|
|
44
|
1 |
|
$user->name = $validatedData['name']; |
45
|
|
|
|
46
|
1 |
|
$user->save(); |
47
|
|
|
|
48
|
1 |
|
return (new UserResource($user))->additional(['message' => 'The user has been updated.']); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|