]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/UserInviteController.php
Added translation string for tasklist WYSIWYG action
[bookstack] / app / Http / Controllers / Auth / UserInviteController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Auth;
4
5 use BookStack\Auth\Access\UserInviteService;
6 use BookStack\Auth\UserRepo;
7 use BookStack\Exceptions\UserTokenExpiredException;
8 use BookStack\Exceptions\UserTokenNotFoundException;
9 use BookStack\Http\Controllers\Controller;
10 use Exception;
11 use Illuminate\Http\RedirectResponse;
12 use Illuminate\Http\Request;
13 use Illuminate\Routing\Redirector;
14 use Illuminate\Validation\Rules\Password;
15
16 class UserInviteController extends Controller
17 {
18     protected $inviteService;
19     protected $userRepo;
20
21     /**
22      * Create a new controller instance.
23      */
24     public function __construct(UserInviteService $inviteService, UserRepo $userRepo)
25     {
26         $this->middleware('guest');
27         $this->middleware('guard:standard');
28
29         $this->inviteService = $inviteService;
30         $this->userRepo = $userRepo;
31     }
32
33     /**
34      * Show the page for the user to set the password for their account.
35      *
36      * @throws Exception
37      */
38     public function showSetPassword(string $token)
39     {
40         try {
41             $this->inviteService->checkTokenAndGetUserId($token);
42         } catch (Exception $exception) {
43             return $this->handleTokenException($exception);
44         }
45
46         return view('auth.invite-set-password', [
47             'token' => $token,
48         ]);
49     }
50
51     /**
52      * Sets the password for an invited user and then grants them access.
53      *
54      * @throws Exception
55      */
56     public function setPassword(Request $request, string $token)
57     {
58         $this->validate($request, [
59             'password' => ['required', Password::default()],
60         ]);
61
62         try {
63             $userId = $this->inviteService->checkTokenAndGetUserId($token);
64         } catch (Exception $exception) {
65             return $this->handleTokenException($exception);
66         }
67
68         $user = $this->userRepo->getById($userId);
69         $user->password = bcrypt($request->get('password'));
70         $user->email_confirmed = true;
71         $user->save();
72
73         $this->inviteService->deleteByUser($user);
74         $this->showSuccessNotification(trans('auth.user_invite_success_login', ['appName' => setting('app-name')]));
75
76         return redirect('/login');
77     }
78
79     /**
80      * Check and validate the exception thrown when checking an invite token.
81      *
82      * @throws Exception
83      *
84      * @return RedirectResponse|Redirector
85      */
86     protected function handleTokenException(Exception $exception)
87     {
88         if ($exception instanceof UserTokenNotFoundException) {
89             return redirect('/');
90         }
91
92         if ($exception instanceof UserTokenExpiredException) {
93             $this->showErrorNotification(trans('errors.invite_token_expired'));
94
95             return redirect('/password/email');
96         }
97
98         throw $exception;
99     }
100 }