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;
15 class UserInviteController extends Controller
17 protected $inviteService;
21 * Create a new controller instance.
23 public function __construct(UserInviteService $inviteService, UserRepo $userRepo)
25 $this->middleware('guest');
26 $this->middleware('guard:standard');
28 $this->inviteService = $inviteService;
29 $this->userRepo = $userRepo;
31 parent::__construct();
35 * 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.
55 public function setPassword(Request $request, string $token)
57 $this->validate($request, [
58 'password' => 'required|min:8'
62 $userId = $this->inviteService->checkTokenAndGetUserId($token);
63 } catch (Exception $exception) {
64 return $this->handleTokenException($exception);
67 $user = $this->userRepo->getById($userId);
68 $user->password = bcrypt($request->get('password'));
69 $user->email_confirmed = true;
73 $this->showSuccessNotification(trans('auth.user_invite_success', ['appName' => setting('app-name')]));
74 $this->inviteService->deleteByUser($user);
80 * Check and validate the exception thrown when checking an invite token.
81 * @return RedirectResponse|Redirector
84 protected function handleTokenException(Exception $exception)
86 if ($exception instanceof UserTokenNotFoundException) {
90 if ($exception instanceof UserTokenExpiredException) {
91 $this->showErrorNotification(trans('errors.invite_token_expired'));
92 return redirect('/password/email');