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