]> BookStack Code Mirror - bookstack/blob - app/Exceptions/StoppedAuthenticationException.php
Lexical: Fixed code in lists, removed extra old alignment code
[bookstack] / app / Exceptions / StoppedAuthenticationException.php
1 <?php
2
3 namespace BookStack\Exceptions;
4
5 use BookStack\Access\LoginService;
6 use BookStack\Users\Models\User;
7 use Illuminate\Contracts\Support\Responsable;
8 use Illuminate\Http\Request;
9
10 class StoppedAuthenticationException extends \Exception implements Responsable
11 {
12     public function __construct(
13         protected User $user,
14         protected LoginService $loginService
15     ) {
16         parent::__construct();
17     }
18
19     /**
20      * {@inheritdoc}
21      */
22     public function toResponse($request)
23     {
24         $redirect = '/login';
25
26         if ($this->loginService->awaitingEmailConfirmation($this->user)) {
27             return $this->awaitingEmailConfirmationResponse($request);
28         }
29
30         if ($this->loginService->needsMfaVerification($this->user)) {
31             $redirect = '/mfa/verify';
32         }
33
34         return redirect($redirect);
35     }
36
37     /**
38      * Provide an error response for when the current user's email is not confirmed
39      * in a system which requires it.
40      */
41     protected function awaitingEmailConfirmationResponse(Request $request)
42     {
43         if ($request->wantsJson()) {
44             return response()->json([
45                 'error' => [
46                     'code'    => 401,
47                     'message' => trans('errors.email_confirmation_awaiting'),
48                 ],
49             ], 401);
50         }
51
52         if (session()->pull('sent-email-confirmation') === true) {
53             return redirect('/register/confirm');
54         }
55
56         return redirect('/register/confirm/awaiting');
57     }
58 }