3 namespace BookStack\Http\Controllers\Auth;
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\Access\UserInviteService;
7 use BookStack\Auth\UserRepo;
8 use BookStack\Exceptions\UserTokenExpiredException;
9 use BookStack\Exceptions\UserTokenNotFoundException;
10 use BookStack\Facades\Theme;
11 use BookStack\Http\Controllers\Controller;
12 use BookStack\Theming\ThemeEvents;
14 use Illuminate\Http\RedirectResponse;
15 use Illuminate\Http\Request;
16 use Illuminate\Routing\Redirector;
18 class UserInviteController extends Controller
20 protected $inviteService;
24 * Create a new controller instance.
26 public function __construct(UserInviteService $inviteService, UserRepo $userRepo)
28 $this->middleware('guest');
29 $this->middleware('guard:standard');
31 $this->inviteService = $inviteService;
32 $this->userRepo = $userRepo;
36 * Show the page for the user to set the password for their account.
40 public function showSetPassword(string $token)
43 $this->inviteService->checkTokenAndGetUserId($token);
44 } catch (Exception $exception) {
45 return $this->handleTokenException($exception);
48 return view('auth.invite-set-password', [
54 * Sets the password for an invited user and then grants them access.
58 public function setPassword(Request $request, string $token)
60 $this->validate($request, [
61 'password' => 'required|min:8',
65 $userId = $this->inviteService->checkTokenAndGetUserId($token);
66 } catch (Exception $exception) {
67 return $this->handleTokenException($exception);
70 $user = $this->userRepo->getById($userId);
71 $user->password = bcrypt($request->get('password'));
72 $user->email_confirmed = true;
76 Theme::dispatch(ThemeEvents::AUTH_LOGIN, auth()->getDefaultDriver(), $user);
77 $this->logActivity(ActivityType::AUTH_LOGIN, $user);
78 $this->showSuccessNotification(trans('auth.user_invite_success', ['appName' => setting('app-name')]));
79 $this->inviteService->deleteByUser($user);
85 * Check and validate the exception thrown when checking an invite token.
89 * @return RedirectResponse|Redirector
91 protected function handleTokenException(Exception $exception)
93 if ($exception instanceof UserTokenNotFoundException) {
97 if ($exception instanceof UserTokenExpiredException) {
98 $this->showErrorNotification(trans('errors.invite_token_expired'));
100 return redirect('/password/email');