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