3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Access\LoginService;
7 use BookStack\Auth\Access\UserInviteService;
8 use BookStack\Auth\UserRepo;
9 use BookStack\Exceptions\UserTokenExpiredException;
10 use BookStack\Exceptions\UserTokenNotFoundException;
11 use BookStack\Facades\Theme;
12 use BookStack\Http\Controllers\Controller;
13 use BookStack\Theming\ThemeEvents;
15 use Illuminate\Http\RedirectResponse;
16 use Illuminate\Http\Request;
17 use Illuminate\Routing\Redirector;
19 class UserInviteController extends Controller
21 protected $inviteService;
22 protected $loginService;
26 * Create a new controller instance.
28 public function __construct(UserInviteService $inviteService, LoginService $loginService, UserRepo $userRepo)
30 $this->middleware('guest');
31 $this->middleware('guard:standard');
33 $this->inviteService = $inviteService;
34 $this->loginService = $loginService;
35 $this->userRepo = $userRepo;
39 * Show the page for the user to set the password for their account.
43 public function showSetPassword(string $token)
46 $this->inviteService->checkTokenAndGetUserId($token);
47 } catch (Exception $exception) {
48 return $this->handleTokenException($exception);
51 return view('auth.invite-set-password', [
57 * Sets the password for an invited user and then grants them access.
61 public function setPassword(Request $request, string $token)
63 $this->validate($request, [
64 'password' => 'required|min:8',
68 $userId = $this->inviteService->checkTokenAndGetUserId($token);
69 } catch (Exception $exception) {
70 return $this->handleTokenException($exception);
73 $user = $this->userRepo->getById($userId);
74 $user->password = bcrypt($request->get('password'));
75 $user->email_confirmed = true;
78 $this->loginService->login($user, auth()->getDefaultDriver());
79 $this->showSuccessNotification(trans('auth.user_invite_success', ['appName' => setting('app-name')]));
80 $this->inviteService->deleteByUser($user);
86 * Check and validate the exception thrown when checking an invite token.
90 * @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'));
101 return redirect('/password/email');