]> BookStack Code Mirror - bookstack/blob - app/Http/Middleware/ChecksForEmailConfirmation.php
df75c2f33dfb2bad5bf88f0afb8c98a432f8cc8e
[bookstack] / app / Http / Middleware / ChecksForEmailConfirmation.php
1 <?php
2
3 namespace BookStack\Http\Middleware;
4
5 use Illuminate\Http\Request;
6
7 trait ChecksForEmailConfirmation
8 {
9
10     /**
11      * Check if email confirmation is required and the current user is awaiting confirmation.
12      */
13     protected function awaitingEmailConfirmation(): bool
14     {
15         if (auth()->check()) {
16             $requireConfirmation = (setting('registration-confirmation') || setting('registration-restrict'));
17             if ($requireConfirmation && !auth()->user()->email_confirmed) {
18                 return true;
19             }
20         }
21
22         return false;
23     }
24
25     /**
26      * Provide an error response for when the current user's email is not confirmed
27      * in a system which requires it.
28      */
29     protected function emailConfirmationErrorResponse(Request $request, bool $forceJson = false)
30     {
31         if ($request->wantsJson() || $forceJson) {
32             return response()->json([
33                 'error' => [
34                     'code' => 401,
35                     'message' => trans('errors.email_confirmation_awaiting')
36                 ]
37             ], 401);
38         }
39
40         return redirect('/register/confirm/awaiting');
41     }
42 }