]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/ConfirmEmailController.php
6e6a0e779d0871e49e9863ffd76a5f8373288395
[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     /**
36      * Show the page to tell the user to check their email
37      * and confirm their address.
38      */
39     public function show()
40     {
41         return view('auth.register-confirm');
42     }
43
44     /**
45      * Shows a notice that a user's email address has not been confirmed,
46      * Also has the option to re-send the confirmation email.
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      * @param $token
57      * @return RedirectResponse|Redirector
58      * @throws ConfirmationEmailException
59      * @throws Exception
60      */
61     public function confirm($token)
62     {
63         try {
64             $userId = $this->emailConfirmationService->checkTokenAndGetUserId($token);
65         } catch (Exception $exception) {
66             if ($exception instanceof UserTokenNotFoundException) {
67                 $this->showErrorNotification(trans('errors.email_confirmation_invalid'));
68                 return redirect('/register');
69             }
70
71             if ($exception instanceof UserTokenExpiredException) {
72                 $user = $this->userRepo->getById($exception->userId);
73                 $this->emailConfirmationService->sendConfirmation($user);
74                 $this->showErrorNotification(trans('errors.email_confirmation_expired'));
75                 return redirect('/register/confirm');
76             }
77
78             throw $exception;
79         }
80
81         $user = $this->userRepo->getById($userId);
82         $user->email_confirmed = true;
83         $user->save();
84
85         auth()->login($user);
86         Theme::dispatch(ThemeEvents::AUTH_LOGIN, auth()->getDefaultDriver(), $user);
87         $this->logActivity(ActivityType::AUTH_LOGIN, $user);
88         $this->showSuccessNotification(trans('auth.email_confirm_success'));
89         $this->emailConfirmationService->deleteByUser($user);
90
91         return redirect('/');
92     }
93
94
95     /**
96      * Resend the confirmation email
97      * @param Request $request
98      * @return View
99      */
100     public function resend(Request $request)
101     {
102         $this->validate($request, [
103             'email' => 'required|email|exists:users,email'
104         ]);
105         $user = $this->userRepo->getByEmail($request->get('email'));
106
107         try {
108             $this->emailConfirmationService->sendConfirmation($user);
109         } catch (Exception $e) {
110             $this->showErrorNotification(trans('auth.email_confirm_send_error'));
111             return redirect('/register/confirm');
112         }
113
114         $this->showSuccessNotification(trans('auth.email_confirm_resent'));
115         return redirect('/register/confirm');
116     }
117 }