3 namespace BookStack\Access\Controllers;
5 use BookStack\Access\UserInviteService;
6 use BookStack\Exceptions\UserTokenExpiredException;
7 use BookStack\Exceptions\UserTokenNotFoundException;
8 use BookStack\Http\Controllers\Controller;
9 use BookStack\Users\UserRepo;
11 use Illuminate\Http\RedirectResponse;
12 use Illuminate\Http\Request;
13 use Illuminate\Routing\Redirector;
14 use Illuminate\Support\Facades\Hash;
15 use Illuminate\Validation\Rules\Password;
17 class UserInviteController extends Controller
19 protected UserInviteService $inviteService;
20 protected UserRepo $userRepo;
23 * Create a new controller instance.
25 public function __construct(UserInviteService $inviteService, UserRepo $userRepo)
27 $this->middleware('guest');
28 $this->middleware('guard:standard');
30 $this->inviteService = $inviteService;
31 $this->userRepo = $userRepo;
35 * Show the page for the user to set the password for their account.
39 public function showSetPassword(string $token)
42 $this->inviteService->checkTokenAndGetUserId($token);
43 } catch (Exception $exception) {
44 return $this->handleTokenException($exception);
47 return view('auth.invite-set-password', [
53 * Sets the password for an invited user and then grants them access.
57 public function setPassword(Request $request, string $token)
59 $this->validate($request, [
60 'password' => ['required', Password::default()],
64 $userId = $this->inviteService->checkTokenAndGetUserId($token);
65 } catch (Exception $exception) {
66 return $this->handleTokenException($exception);
69 $user = $this->userRepo->getById($userId);
70 $user->password = Hash::make($request->get('password'));
71 $user->email_confirmed = true;
74 $this->inviteService->deleteByUser($user);
75 $this->showSuccessNotification(trans('auth.user_invite_success_login', ['appName' => setting('app-name')]));
77 return redirect('/login');
81 * Check and validate the exception thrown when checking an invite token.
85 * @return RedirectResponse|Redirector
87 protected function handleTokenException(Exception $exception)
89 if ($exception instanceof UserTokenNotFoundException) {
93 if ($exception instanceof UserTokenExpiredException) {
94 $this->showErrorNotification(trans('errors.invite_token_expired'));
96 return redirect('/password/email');