| 
									
										
										
										
											2021-07-18 23:52:31 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Exceptions; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use BookStack\Auth\Access\LoginService; | 
					
						
							|  |  |  | use BookStack\Auth\User; | 
					
						
							|  |  |  | use Illuminate\Contracts\Support\Responsable; | 
					
						
							| 
									
										
										
										
											2021-08-08 04:18:59 +08:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							| 
									
										
										
										
											2021-07-18 23:52:31 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class StoppedAuthenticationException extends \Exception implements Responsable | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     protected $user; | 
					
						
							|  |  |  |     protected $loginService; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * StoppedAuthenticationException constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct(User $user, LoginService $loginService) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $this->user = $user; | 
					
						
							|  |  |  |         $this->loginService = $loginService; | 
					
						
							|  |  |  |         parent::__construct(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @inheritdoc | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function toResponse($request) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $redirect = '/login'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($this->loginService->awaitingEmailConfirmation($this->user)) { | 
					
						
							| 
									
										
										
										
											2021-08-08 04:18:59 +08:00
										 |  |  |             return $this->awaitingEmailConfirmationResponse($request); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if ($this->loginService->needsMfaVerification($this->user)) { | 
					
						
							| 
									
										
										
										
											2021-07-18 23:52:31 +08:00
										 |  |  |             $redirect = '/mfa/verify'; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return redirect($redirect); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-08-08 04:18:59 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Provide an error response for when the current user's email is not confirmed | 
					
						
							|  |  |  |      * in a system which requires it. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function awaitingEmailConfirmationResponse(Request $request) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($request->wantsJson()) { | 
					
						
							|  |  |  |             return response()->json([ | 
					
						
							|  |  |  |                 'error' => [ | 
					
						
							| 
									
										
										
										
											2021-08-21 22:49:40 +08:00
										 |  |  |                     'code'    => 401, | 
					
						
							| 
									
										
										
										
											2021-08-08 04:18:59 +08:00
										 |  |  |                     'message' => trans('errors.email_confirmation_awaiting'), | 
					
						
							|  |  |  |                 ], | 
					
						
							|  |  |  |             ], 401); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (session()->get('sent-email-confirmation') === true) { | 
					
						
							|  |  |  |             return redirect('/register/confirm'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return redirect('/register/confirm/awaiting'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-08-21 22:49:40 +08:00
										 |  |  | } |