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;
33 * Show the page for the user to set the password for their account.
36 public function showSetPassword(string $token)
39 $this->inviteService->checkTokenAndGetUserId($token);
40 } catch (Exception $exception) {
41 return $this->handleTokenException($exception);
44 return view('auth.invite-set-password', [
50 * Sets the password for an invited user and then grants them access.
53 public function setPassword(Request $request, string $token)
55 $this->validate($request, [
56 'password' => 'required|min:8'
60 $userId = $this->inviteService->checkTokenAndGetUserId($token);
61 } catch (Exception $exception) {
62 return $this->handleTokenException($exception);
65 $user = $this->userRepo->getById($userId);
66 $user->password = bcrypt($request->get('password'));
67 $user->email_confirmed = true;
71 $this->showSuccessNotification(trans('auth.user_invite_success', ['appName' => setting('app-name')]));
72 $this->inviteService->deleteByUser($user);
78 * Check and validate the exception thrown when checking an invite token.
79 * @return RedirectResponse|Redirector
82 protected function handleTokenException(Exception $exception)
84 if ($exception instanceof UserTokenNotFoundException) {
88 if ($exception instanceof UserTokenExpiredException) {
89 $this->showErrorNotification(trans('errors.invite_token_expired'));
90 return redirect('/password/email');