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