| 
									
										
										
										
											2019-12-30 23:46:12 +08:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace BookStack\Http\Middleware; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-02 00:33:47 +08:00
										 |  |  | use BookStack\Exceptions\UnauthorizedException; | 
					
						
							| 
									
										
										
										
											2019-12-30 23:46:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | trait ChecksForEmailConfirmation | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-02 00:33:47 +08:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Check if the current user has a confirmed email if the instance deems it as required. | 
					
						
							|  |  |  |      * Throws if confirmation is required by the user. | 
					
						
							| 
									
										
										
										
											2021-06-26 23:23:15 +08:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2020-01-02 00:33:47 +08:00
										 |  |  |      * @throws UnauthorizedException | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function ensureEmailConfirmedIfRequested() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($this->awaitingEmailConfirmation()) { | 
					
						
							|  |  |  |             throw new UnauthorizedException(trans('errors.email_confirmation_awaiting')); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-12-30 23:46:12 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Check if email confirmation is required and the current user is awaiting confirmation. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     protected function awaitingEmailConfirmation(): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (auth()->check()) { | 
					
						
							|  |  |  |             $requireConfirmation = (setting('registration-confirmation') || setting('registration-restrict')); | 
					
						
							|  |  |  |             if ($requireConfirmation && !auth()->user()->email_confirmed) { | 
					
						
							|  |  |  |                 return true; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-03-08 06:24:05 +08:00
										 |  |  | } |