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