]> BookStack Code Mirror - bookstack/blob - app/Http/Middleware/ChecksForEmailConfirmation.php
Move logFailedAccess into Activity
[bookstack] / app / Http / Middleware / ChecksForEmailConfirmation.php
1 <?php
2
3 namespace BookStack\Http\Middleware;
4
5 use BookStack\Exceptions\UnauthorizedException;
6 use Illuminate\Http\Request;
7
8 trait ChecksForEmailConfirmation
9 {
10     /**
11      * Check if the current user has a confirmed email if the instance deems it as required.
12      * Throws if confirmation is required by the user.
13      * @throws UnauthorizedException
14      */
15     protected function ensureEmailConfirmedIfRequested()
16     {
17         if ($this->awaitingEmailConfirmation()) {
18             throw new UnauthorizedException(trans('errors.email_confirmation_awaiting'));
19         }
20     }
21
22     /**
23      * Check if email confirmation is required and the current user is awaiting confirmation.
24      */
25     protected function awaitingEmailConfirmation(): bool
26     {
27         if (auth()->check()) {
28             $requireConfirmation = (setting('registration-confirmation') || setting('registration-restrict'));
29             if ($requireConfirmation && !auth()->user()->email_confirmed) {
30                 return true;
31             }
32         }
33
34         return false;
35     }
36 }