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