| 
									
										
										
										
											2015-07-13 03:01:42 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-11 02:31:09 +08:00
										 |  |  | namespace BookStack\Http\Middleware; | 
					
						
							| 
									
										
										
										
											2015-07-13 03:01:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | use Closure; | 
					
						
							| 
									
										
										
										
											2019-12-30 23:46:12 +08:00
										 |  |  | use Illuminate\Http\Request; | 
					
						
							| 
									
										
										
										
											2015-07-13 03:01:42 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Authenticate | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2019-12-30 23:46:12 +08:00
										 |  |  |     use ChecksForEmailConfirmation; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-13 03:01:42 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Handle an incoming request. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2019-12-30 10:16:07 +08:00
										 |  |  |     public function handle(Request $request, Closure $next) | 
					
						
							| 
									
										
										
										
											2015-07-13 03:01:42 +08:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2019-12-30 23:46:12 +08:00
										 |  |  |         if ($this->awaitingEmailConfirmation()) { | 
					
						
							|  |  |  |             return $this->emailConfirmationErrorResponse($request); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-02-04 01:34:15 +08:00
										 |  |  |         if (!hasAppAccess()) { | 
					
						
							| 
									
										
										
										
											2015-07-13 03:01:42 +08:00
										 |  |  |             if ($request->ajax()) { | 
					
						
							|  |  |  |                 return response('Unauthorized.', 401); | 
					
						
							|  |  |  |             } else { | 
					
						
							| 
									
										
										
										
											2019-08-04 21:26:39 +08:00
										 |  |  |                 return redirect()->guest(url('/login')); | 
					
						
							| 
									
										
										
										
											2015-07-13 03:01:42 +08:00
										 |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $next($request); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-02 00:33:47 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Provide an error response for when the current user's email is not confirmed | 
					
						
							|  |  |  |      * in a system which requires it. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function emailConfirmationErrorResponse(Request $request) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($request->wantsJson()) { | 
					
						
							|  |  |  |             return response()->json([ | 
					
						
							|  |  |  |                 'error' => [ | 
					
						
							|  |  |  |                     'code' => 401, | 
					
						
							|  |  |  |                     'message' => trans('errors.email_confirmation_awaiting') | 
					
						
							|  |  |  |                 ] | 
					
						
							|  |  |  |             ], 401); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-06 00:26:48 +08:00
										 |  |  |         if (session()->get('sent-email-confirmation') === true) { | 
					
						
							|  |  |  |             return redirect('/register/confirm'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-02 00:33:47 +08:00
										 |  |  |         return redirect('/register/confirm/awaiting'); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-07-13 03:01:42 +08:00
										 |  |  | } |