PasswordChangeController.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. namespace App\Http\Controllers\Auth;
  3. use App\Http\Controllers\Controller;
  4. use App\Services\CallApiService;
  5. use Illuminate\Support\Facades\Validator;
  6. class PasswordChangeController extends Controller
  7. {
  8. private $callApiService;
  9. public function __construct(CallApiService $callApiService)
  10. {
  11. $this->callApiService = $callApiService;
  12. }
  13. public function index()
  14. {
  15. $response = $this->callApiService->callApi([
  16. 'url' => 'user-passwd-reset',
  17. 'data' => [
  18. 'ResetCode' => request('code')
  19. ]
  20. ]);
  21. if (empty(request('code')) || $this->callApiService->verifyApiError($response)) {
  22. return redirect()->route('password-reset-code-failed');
  23. }
  24. $passwdPolicy = setupPasswdPolicy('users');
  25. return view('auth.user-password-change', [
  26. 'policyDesc' => $passwdPolicy['PolicyDesc']
  27. ]);
  28. }
  29. public function store()
  30. {
  31. $passwdPolicy = setupPasswdPolicy('users');
  32. if ($this->callApiService->verifyApiError($passwdPolicy)) {
  33. notify()->error($passwdPolicy['body'], 'Error', 'bottomRight');
  34. return redirect()->back();
  35. }
  36. $passwdRules = makePasswdRules($passwdPolicy);
  37. $validator = validator::make(request()->all(), [
  38. 'password' => $passwdRules
  39. ]);
  40. if ($validator->fails()) {
  41. return redirect()->back()->withErrors($validator)->withInput();
  42. }
  43. $response = $this->callApiService->callApi([
  44. 'url' => 'user-passwd-reset',
  45. 'data' => [
  46. 'ResetCode' => request('code'),
  47. 'Passwd' => request('password')
  48. ]
  49. ]);
  50. if ($this->callApiService->verifyApiError($response)) {
  51. notify()->error($response['body'], 'Error', 'bottomRight');
  52. return redirect()->back();
  53. }
  54. notify()->success(_e('Action completed'), 'Success', 'bottomRight');
  55. return redirect()->route('user-login');
  56. }
  57. }