]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/ConfirmEmailController.php
Update passwords.php
[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
68             if ($exception instanceof UserTokenNotFoundException) {
69                 session()->flash('error', trans('errors.email_confirmation_invalid'));
70                 return redirect('/register');
71             }
72
73             if ($exception instanceof UserTokenExpiredException) {
74                 $user = $this->userRepo->getById($exception->userId);
75                 $this->emailConfirmationService->sendConfirmation($user);
76                 session()->flash('error', trans('errors.email_confirmation_expired'));
77                 return redirect('/register/confirm');
78             }
79
80             throw $exception;
81         }
82
83         $user = $this->userRepo->getById($userId);
84         $user->email_confirmed = true;
85         $user->save();
86
87         auth()->login($user);
88         session()->flash('success', 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             session()->flash('error', trans('auth.email_confirm_send_error'));
111             return redirect('/register/confirm');
112         }
113
114         session()->flash('success', trans('auth.email_confirm_resent'));
115         return redirect('/register/confirm');
116     }
117
118 }