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