3 namespace BookStack\Http\Controllers\Auth;
6 use BookStack\Actions\ActivityType;
7 use BookStack\Auth\Access\SocialAuthService;
8 use BookStack\Exceptions\LoginAttemptEmailNeededException;
9 use BookStack\Exceptions\LoginAttemptException;
10 use BookStack\Facades\Theme;
11 use BookStack\Http\Controllers\Controller;
12 use BookStack\Theming\ThemeEvents;
13 use Illuminate\Foundation\Auth\AuthenticatesUsers;
14 use Illuminate\Http\Request;
15 use Illuminate\Validation\ValidationException;
17 class LoginController extends Controller
20 |--------------------------------------------------------------------------
22 |--------------------------------------------------------------------------
24 | This controller handles authenticating users for the application and
25 | redirecting them to your home screen. The controller uses a trait
26 | to conveniently provide its functionality to your applications.
30 use AuthenticatesUsers;
35 protected $redirectTo = '/';
36 protected $redirectPath = '/';
37 protected $redirectAfterLogout = '/login';
39 protected $socialAuthService;
42 * Create a new controller instance.
44 public function __construct(SocialAuthService $socialAuthService)
46 $this->middleware('guest', ['only' => ['getLogin', 'login']]);
47 $this->middleware('guard:standard,ldap', ['only' => ['login', 'logout']]);
49 $this->socialAuthService = $socialAuthService;
50 $this->redirectPath = url('/');
51 $this->redirectAfterLogout = url('/login');
54 public function username()
56 return config('auth.method') === 'standard' ? 'email' : 'username';
60 * Get the needed authorization credentials from the request.
62 protected function credentials(Request $request)
64 return $request->only('username', 'email', 'password');
68 * Show the application login form.
70 public function getLogin(Request $request)
72 $socialDrivers = $this->socialAuthService->getActiveDrivers();
73 $authMethod = config('auth.method');
75 if ($request->has('email')) {
76 session()->flashInput([
77 'email' => $request->get('email'),
78 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
82 // Store the previous location for redirect after login
83 $previous = url()->previous('');
84 if ($previous && $previous !== url('/login') && setting('app-public')) {
85 $isPreviousFromInstance = (strpos($previous, url('/')) === 0);
86 if ($isPreviousFromInstance) {
87 redirect()->setIntendedUrl($previous);
91 return view('auth.login', [
92 'socialDrivers' => $socialDrivers,
93 'authMethod' => $authMethod,
98 * Handle a login request to the application.
100 * @param \Illuminate\Http\Request $request
101 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
103 * @throws \Illuminate\Validation\ValidationException
105 public function login(Request $request)
107 $this->validateLogin($request);
108 $username = $request->get($this->username());
110 // If the class is using the ThrottlesLogins trait, we can automatically throttle
111 // the login attempts for this application. We'll key this by the username and
112 // the IP address of the client making these requests into this application.
113 if (method_exists($this, 'hasTooManyLoginAttempts') &&
114 $this->hasTooManyLoginAttempts($request)) {
115 $this->fireLockoutEvent($request);
117 Activity::logFailedLogin($username);
118 return $this->sendLockoutResponse($request);
122 if ($this->attemptLogin($request)) {
123 return $this->sendLoginResponse($request);
125 } catch (LoginAttemptException $exception) {
126 Activity::logFailedLogin($username);
127 return $this->sendLoginAttemptExceptionResponse($exception, $request);
130 // If the login attempt was unsuccessful we will increment the number of attempts
131 // to login and redirect the user back to the login form. Of course, when this
132 // user surpasses their maximum number of attempts they will get locked out.
133 $this->incrementLoginAttempts($request);
135 Activity::logFailedLogin($username);
136 return $this->sendFailedLoginResponse($request);
140 * The user has been authenticated.
142 * @param \Illuminate\Http\Request $request
146 protected function authenticated(Request $request, $user)
148 // Authenticate on all session guards if a likely admin
149 if ($user->can('users-manage') && $user->can('user-roles-manage')) {
150 $guards = ['standard', 'ldap', 'saml2'];
151 foreach ($guards as $guard) {
152 auth($guard)->login($user);
156 Theme::dispatch(ThemeEvents::AUTH_LOGIN, auth()->getDefaultDriver(), $user);
157 $this->logActivity(ActivityType::AUTH_LOGIN, $user);
158 return redirect()->intended($this->redirectPath());
162 * Validate the user login request.
164 * @param \Illuminate\Http\Request $request
167 * @throws \Illuminate\Validation\ValidationException
169 protected function validateLogin(Request $request)
171 $rules = ['password' => 'required|string'];
172 $authMethod = config('auth.method');
174 if ($authMethod === 'standard') {
175 $rules['email'] = 'required|email';
178 if ($authMethod === 'ldap') {
179 $rules['username'] = 'required|string';
180 $rules['email'] = 'email';
183 $request->validate($rules);
187 * Send a response when a login attempt exception occurs.
189 protected function sendLoginAttemptExceptionResponse(LoginAttemptException $exception, Request $request)
191 if ($exception instanceof LoginAttemptEmailNeededException) {
193 session()->flash('request-email', true);
196 if ($message = $exception->getMessage()) {
197 $this->showWarningNotification($message);
200 return redirect('/login');
204 * Get the failed login response instance.
206 * @param \Illuminate\Http\Request $request
207 * @return \Symfony\Component\HttpFoundation\Response
209 * @throws \Illuminate\Validation\ValidationException
211 protected function sendFailedLoginResponse(Request $request)
213 throw ValidationException::withMessages([
214 $this->username() => [trans('auth.failed')],
215 ])->redirectTo('/login');