3 namespace BookStack\Http\Controllers\Auth;
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;
11 use Illuminate\Http\RedirectResponse;
12 use Illuminate\Http\Request;
13 use Illuminate\Routing\Redirector;
14 use Illuminate\Validation\Rules\Password;
16 class UserInviteController extends Controller
18 protected $inviteService;
22 * Create a new controller instance.
24 public function __construct(UserInviteService $inviteService, UserRepo $userRepo)
26 $this->middleware('guest');
27 $this->middleware('guard:standard');
29 $this->inviteService = $inviteService;
30 $this->userRepo = $userRepo;
34 * Show the page for the user to set the password for their account.
38 public function showSetPassword(string $token)
41 $this->inviteService->checkTokenAndGetUserId($token);
42 } catch (Exception $exception) {
43 return $this->handleTokenException($exception);
46 return view('auth.invite-set-password', [
52 * Sets the password for an invited user and then grants them access.
56 public function setPassword(Request $request, string $token)
58 $this->validate($request, [
59 'password' => ['required', Password::default()],
63 $userId = $this->inviteService->checkTokenAndGetUserId($token);
64 } catch (Exception $exception) {
65 return $this->handleTokenException($exception);
68 $user = $this->userRepo->getById($userId);
69 $user->password = bcrypt($request->get('password'));
70 $user->email_confirmed = true;
73 $this->inviteService->deleteByUser($user);
74 $this->showSuccessNotification(trans('auth.user_invite_success_login', ['appName' => setting('app-name')]));
76 return redirect('/login');
80 * Check and validate the exception thrown when checking an invite token.
84 * @return RedirectResponse|Redirector
86 protected function handleTokenException(Exception $exception)
88 if ($exception instanceof UserTokenNotFoundException) {
92 if ($exception instanceof UserTokenExpiredException) {
93 $this->showErrorNotification(trans('errors.invite_token_expired'));
95 return redirect('/password/email');