]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/ConfirmEmailController.php
Apply fixes from StyleCI
[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\UserRepo;
8 use BookStack\Exceptions\ConfirmationEmailException;
9 use BookStack\Exceptions\UserTokenExpiredException;
10 use BookStack\Exceptions\UserTokenNotFoundException;
11 use BookStack\Facades\Theme;
12 use BookStack\Http\Controllers\Controller;
13 use BookStack\Theming\ThemeEvents;
14 use Exception;
15 use Illuminate\Http\RedirectResponse;
16 use Illuminate\Http\Request;
17 use Illuminate\Routing\Redirector;
18 use Illuminate\View\View;
19
20 class ConfirmEmailController extends Controller
21 {
22     protected $emailConfirmationService;
23     protected $userRepo;
24
25     /**
26      * Create a new controller instance.
27      */
28     public function __construct(EmailConfirmationService $emailConfirmationService, UserRepo $userRepo)
29     {
30         $this->emailConfirmationService = $emailConfirmationService;
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      * @return View
48      */
49     public function showAwaiting()
50     {
51         return view('auth.user-unconfirmed');
52     }
53
54     /**
55      * Confirms an email via a token and logs the user into the system.
56      *
57      * @param $token
58      *
59      * @throws ConfirmationEmailException
60      * @throws Exception
61      *
62      * @return RedirectResponse|Redirector
63      */
64     public function confirm($token)
65     {
66         try {
67             $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
68         } catch (Exception $exception) {
69             if ($exception instanceof UserTokenNotFoundException) {
70                 $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
71
72                 return redirect('/register');
73             }
74
75             if ($exception instanceof UserTokenExpiredException) {
76                 $user = $this->userRepo->getById($exception->userId);
77                 $this->emailConfirmationService->sendConfirmation($user);
78                 $this->showErrorNotification(trans('errors.email_confirmation_expired'));
79
80                 return redirect('/register/confirm');
81             }
82
83             throw $exception;
84         }
85
86         $user = $this->userRepo->getById($userId);
87         $user->email_confirmed = true;
88         $user->save();
89
90         auth()->login($user);
91         Theme::dispatch(ThemeEvents::AUTH_LOGIN, auth()->getDefaultDriver(), $user);
92         $this->logActivity(ActivityType::AUTH_LOGIN, $user);
93         $this->showSuccessNotification(trans('auth.email_confirm_success'));
94         $this->emailConfirmationService->deleteByUser($user);
95
96         return redirect('/');
97     }
98
99     /**
100      * Resend the confirmation email.
101      *
102      * @param Request $request
103      *
104      * @return View
105      */
106     public function resend(Request $request)
107     {
108         $this->validate($request, [
109             'email' => 'required|email|exists:users,email',
110         ]);
111         $user = $this->userRepo->getByEmail($request->get('email'));
112
113         try {
114             $this->emailConfirmationService->sendConfirmation($user);
115         } catch (Exception $e) {
116             $this->showErrorNotification(trans('auth.email_confirm_send_error'));
117
118             return redirect('/register/confirm');
119         }
120
121         $this->showSuccessNotification(trans('auth.email_confirm_resent'));
122
123         return redirect('/register/confirm');
124     }
125 }