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\Contracts\View\Factory;
12 use Illuminate\Http\RedirectResponse;
13 use Illuminate\Http\Request;
14 use Illuminate\Routing\Redirector;
15 use Illuminate\View\View;
17 class UserInviteController extends Controller
19 protected $inviteService;
23 * Create a new controller instance.
25 * @param UserInviteService $inviteService
26 * @param UserRepo $userRepo
28 public function __construct(UserInviteService $inviteService, UserRepo $userRepo)
30 $this->inviteService = $inviteService;
31 $this->userRepo = $userRepo;
32 $this->middleware('guest');
33 parent::__construct();
37 * Show the page for the user to set the password for their account.
38 * @param string $token
39 * @return Factory|View|RedirectResponse
42 public function showSetPassword(string $token)
45 $this->inviteService->checkTokenAndGetUserId($token);
46 } catch (Exception $exception) {
47 return $this->handleTokenException($exception);
50 return view('auth.invite-set-password', [
56 * Sets the password for an invited user and then grants them access.
57 * @param Request $request
58 * @param string $token
59 * @return RedirectResponse|Redirector
62 public function setPassword(Request $request, string $token)
64 $this->validate($request, [
65 'password' => 'required|min:8'
69 $userId = $this->inviteService->checkTokenAndGetUserId($token);
70 } catch (Exception $exception) {
71 return $this->handleTokenException($exception);
74 $user = $this->userRepo->getById($userId);
75 $user->password = bcrypt($request->get('password'));
76 $user->email_confirmed = true;
80 $this->showSuccessNotification(trans('auth.user_invite_success', ['appName' => setting('app-name')]));
81 $this->inviteService->deleteByUser($user);
87 * Check and validate the exception thrown when checking an invite token.
88 * @param Exception $exception
89 * @return RedirectResponse|Redirector
92 protected function handleTokenException(Exception $exception)
94 if ($exception instanceof UserTokenNotFoundException) {
98 if ($exception instanceof UserTokenExpiredException) {
99 $this->showErrorNotification(trans('errors.invite_token_expired'));
100 return redirect('/password/email');