]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/ConfirmEmailController.php
Docker: Fix PHP tests
[bookstack] / app / Http / Controllers / Auth / ConfirmEmailController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Auth;
4
5 use BookStack\Auth\Access\EmailConfirmationService;
6 use BookStack\Auth\UserRepo;
7 use BookStack\Exceptions\ConfirmationEmailException;
8 use BookStack\Exceptions\UserTokenExpiredException;
9 use BookStack\Exceptions\UserTokenNotFoundException;
10 use BookStack\Http\Controllers\Controller;
11 use Exception;
12 use Illuminate\Http\RedirectResponse;
13 use Illuminate\Http\Request;
14 use Illuminate\Routing\Redirector;
15 use Illuminate\View\View;
16
17 class ConfirmEmailController extends Controller
18 {
19     protected $emailConfirmationService;
20     protected $userRepo;
21
22     /**
23      * Create a new controller instance.
24      */
25     public function __construct(EmailConfirmationService $emailConfirmationService, UserRepo $userRepo)
26     {
27         $this->emailConfirmationService = $emailConfirmationService;
28         $this->userRepo = $userRepo;
29     }
30
31
32     /**
33      * Show the page to tell the user to check their email
34      * and confirm their address.
35      */
36     public function show()
37     {
38         return view('auth.register-confirm');
39     }
40
41     /**
42      * Shows a notice that a user's email address has not been confirmed,
43      * Also has the option to re-send the confirmation email.
44      * @return View
45      */
46     public function showAwaiting()
47     {
48         return view('auth.user-unconfirmed');
49     }
50
51     /**
52      * Confirms an email via a token and logs the user into the system.
53      * @param $token
54      * @return RedirectResponse|Redirector
55      * @throws ConfirmationEmailException
56      * @throws Exception
57      */
58     public function confirm($token)
59     {
60         try {
61             $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
62         } catch (Exception $exception) {
63             if ($exception instanceof UserTokenNotFoundException) {
64                 $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
65                 return redirect('/register');
66             }
67
68             if ($exception instanceof UserTokenExpiredException) {
69                 $user = $this->userRepo->getById($exception->userId);
70                 $this->emailConfirmationService->sendConfirmation($user);
71                 $this->showErrorNotification(trans('errors.email_confirmation_expired'));
72                 return redirect('/register/confirm');
73             }
74
75             throw $exception;
76         }
77
78         $user = $this->userRepo->getById($userId);
79         $user->email_confirmed = true;
80         $user->save();
81
82         auth()->login($user);
83         $this->showSuccessNotification(trans('auth.email_confirm_success'));
84         $this->emailConfirmationService->deleteByUser($user);
85
86         return redirect('/');
87     }
88
89
90     /**
91      * Resend the confirmation email
92      * @param Request $request
93      * @return View
94      */
95     public function resend(Request $request)
96     {
97         $this->validate($request, [
98             'email' => 'required|email|exists:users,email'
99         ]);
100         $user = $this->userRepo->getByEmail($request->get('email'));
101
102         try {
103             $this->emailConfirmationService->sendConfirmation($user);
104         } catch (Exception $e) {
105             $this->showErrorNotification(trans('auth.email_confirm_send_error'));
106             return redirect('/register/confirm');
107         }
108
109         $this->showSuccessNotification(trans('auth.email_confirm_resent'));
110         return redirect('/register/confirm');
111     }
112 }