]> BookStack Code Mirror - bookstack/blob - app/Exceptions/StoppedAuthenticationException.php
Cleaned some unused elements during testing
[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
13     protected $user;
14     protected $loginService;
15
16     /**
17      * StoppedAuthenticationException constructor.
18      */
19     public function __construct(User $user, LoginService $loginService)
20     {
21         $this->user = $user;
22         $this->loginService = $loginService;
23         parent::__construct();
24     }
25
26     /**
27      * @inheritdoc
28      */
29     public function toResponse($request)
30     {
31         $redirect = '/login';
32
33         if ($this->loginService->awaitingEmailConfirmation($this->user)) {
34             return $this->awaitingEmailConfirmationResponse($request);
35         }
36
37         if ($this->loginService->needsMfaVerification($this->user)) {
38             $redirect = '/mfa/verify';
39         }
40
41         return redirect($redirect);
42     }
43
44     /**
45      * Provide an error response for when the current user's email is not confirmed
46      * in a system which requires it.
47      */
48     protected function awaitingEmailConfirmationResponse(Request $request)
49     {
50         if ($request->wantsJson()) {
51             return response()->json([
52                 'error' => [
53                     'code' => 401,
54                     'message' => trans('errors.email_confirmation_awaiting'),
55                 ],
56             ], 401);
57         }
58
59         if (session()->get('sent-email-confirmation') === true) {
60             return redirect('/register/confirm');
61         }
62
63         return redirect('/register/confirm/awaiting');
64     }
65 }