| @@ 16-53 (lines=38) @@ | ||
| 13 | * |
|
| 14 | * @package Acacha\Users\Http\Controllers |
|
| 15 | */ |
|
| 16 | class APILoggedUserEmailController extends Controller { |
|
| 17 | ||
| 18 | /** |
|
| 19 | * Update logged user password. |
|
| 20 | * |
|
| 21 | * @param LoggedUserEmailUpdate $request |
|
| 22 | * @return JsonResponse|\App\User|null |
|
| 23 | */ |
|
| 24 | public function update(LoggedUserEmailUpdate $request) |
|
| 25 | { |
|
| 26 | $user = Auth::user(); |
|
| 27 | if (! $this->checkCurrentPassword($request, $user)) { |
|
| 28 | return new JsonResponse( |
|
| 29 | [ |
|
| 30 | 'message'=> 'The given data was invalid.', |
|
| 31 | 'errors'=> [ |
|
| 32 | 'current_password' => [ 'The password is not valid'] |
|
| 33 | ] |
|
| 34 | ], |
|
| 35 | 422 |
|
| 36 | ); |
|
| 37 | } |
|
| 38 | $user->update(['email' => $request->email]); |
|
| 39 | ||
| 40 | return $user; |
|
| 41 | } |
|
| 42 | ||
| 43 | /** |
|
| 44 | * Check current password |
|
| 45 | * |
|
| 46 | * @param $user |
|
| 47 | * @return bool |
|
| 48 | */ |
|
| 49 | protected function checkCurrentPassword(LoggedUserEmailUpdate $request, $user) |
|
| 50 | { |
|
| 51 | return Hash::check($request->current_password, $user->password); |
|
| 52 | } |
|
| 53 | } |
|
| @@ 15-52 (lines=38) @@ | ||
| 12 | * |
|
| 13 | * @package Acacha\Users\Http\Controllers |
|
| 14 | */ |
|
| 15 | class APILoggedUserPasswordController extends Controller { |
|
| 16 | ||
| 17 | /** |
|
| 18 | * Update logged user password. |
|
| 19 | * |
|
| 20 | * @param LoggedUserPasswordUpdate $request |
|
| 21 | * @return JsonResponse|\App\User|null |
|
| 22 | */ |
|
| 23 | public function update(LoggedUserPasswordUpdate $request) |
|
| 24 | { |
|
| 25 | $user = Auth::user(); |
|
| 26 | if (! $this->checkCurrentPassword($request, $user)) { |
|
| 27 | return new JsonResponse( |
|
| 28 | [ |
|
| 29 | 'message'=> 'The given data was invalid.', |
|
| 30 | 'errors'=> [ |
|
| 31 | 'current_password' => [ 'The password is not valid'] |
|
| 32 | ] |
|
| 33 | ], |
|
| 34 | 422 |
|
| 35 | ); |
|
| 36 | } |
|
| 37 | $user->update(['password' => bcrypt($request->password)]); |
|
| 38 | ||
| 39 | return $user; |
|
| 40 | } |
|
| 41 | ||
| 42 | /** |
|
| 43 | * Check current password |
|
| 44 | * |
|
| 45 | * @param $user |
|
| 46 | * @return bool |
|
| 47 | */ |
|
| 48 | protected function checkCurrentPassword(LoggedUserPasswordUpdate $request, $user) |
|
| 49 | { |
|
| 50 | return Hash::check($request->current_password, $user->password); |
|
| 51 | } |
|
| 52 | } |
|