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