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;
16 class LoginController extends Controller
19 |--------------------------------------------------------------------------
21 |--------------------------------------------------------------------------
23 | This controller handles authenticating users for the application and
24 | redirecting them to your home screen. The controller uses a trait
25 | to conveniently provide its functionality to your applications.
29 use AuthenticatesUsers;
34 protected $redirectTo = '/';
35 protected $redirectPath = '/';
36 protected $redirectAfterLogout = '/login';
38 protected $socialAuthService;
41 * Create a new controller instance.
43 public function __construct(SocialAuthService $socialAuthService)
45 $this->middleware('guest', ['only' => ['getLogin', 'login']]);
46 $this->middleware('guard:standard,ldap', ['only' => ['login', 'logout']]);
48 $this->socialAuthService = $socialAuthService;
49 $this->redirectPath = url('/');
50 $this->redirectAfterLogout = url('/login');
53 public function username()
55 return config('auth.method') === 'standard' ? 'email' : 'username';
59 * Get the needed authorization credentials from the request.
61 protected function credentials(Request $request)
63 return $request->only('username', 'email', 'password');
67 * Show the application login form.
69 public function getLogin(Request $request)
71 $socialDrivers = $this->socialAuthService->getActiveDrivers();
72 $authMethod = config('auth.method');
74 if ($request->has('email')) {
75 session()->flashInput([
76 'email' => $request->get('email'),
77 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
81 // Store the previous location for redirect after login
82 $previous = url()->previous('');
83 if ($previous && $previous !== url('/login') && setting('app-public')) {
84 $isPreviousFromInstance = (strpos($previous, url('/')) === 0);
85 if ($isPreviousFromInstance) {
86 redirect()->setIntendedUrl($previous);
90 return view('auth.login', [
91 'socialDrivers' => $socialDrivers,
92 'authMethod' => $authMethod,
97 * Handle a login request to the application.
99 * @param \Illuminate\Http\Request $request
100 * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
102 * @throws \Illuminate\Validation\ValidationException
104 public function login(Request $request)
106 $this->validateLogin($request);
107 $username = $request->get($this->username());
109 // If the class is using the ThrottlesLogins trait, we can automatically throttle
110 // the login attempts for this application. We'll key this by the username and
111 // the IP address of the client making these requests into this application.
112 if (method_exists($this, 'hasTooManyLoginAttempts') &&
113 $this->hasTooManyLoginAttempts($request)) {
114 $this->fireLockoutEvent($request);
116 Activity::logFailedLogin($username);
117 return $this->sendLockoutResponse($request);
121 if ($this->attemptLogin($request)) {
122 return $this->sendLoginResponse($request);
124 } catch (LoginAttemptException $exception) {
125 Activity::logFailedLogin($username);
126 return $this->sendLoginAttemptExceptionResponse($exception, $request);
129 // If the login attempt was unsuccessful we will increment the number of attempts
130 // to login and redirect the user back to the login form. Of course, when this
131 // user surpasses their maximum number of attempts they will get locked out.
132 $this->incrementLoginAttempts($request);
134 Activity::logFailedLogin($username);
135 return $this->sendFailedLoginResponse($request);
139 * The user has been authenticated.
141 * @param \Illuminate\Http\Request $request
145 protected function authenticated(Request $request, $user)
147 // Authenticate on all session guards if a likely admin
148 if ($user->can('users-manage') && $user->can('user-roles-manage')) {
149 $guards = ['standard', 'ldap', 'saml2'];
150 foreach ($guards as $guard) {
151 auth($guard)->login($user);
155 Theme::dispatch(ThemeEvents::AUTH_LOGIN, auth()->getDefaultDriver(), $user);
156 $this->logActivity(ActivityType::AUTH_LOGIN, $user);
157 return redirect()->intended($this->redirectPath());
161 * Validate the user login request.
163 * @param \Illuminate\Http\Request $request
166 * @throws \Illuminate\Validation\ValidationException
168 protected function validateLogin(Request $request)
170 $rules = ['password' => 'required|string'];
171 $authMethod = config('auth.method');
173 if ($authMethod === 'standard') {
174 $rules['email'] = 'required|email';
177 if ($authMethod === 'ldap') {
178 $rules['username'] = 'required|string';
179 $rules['email'] = 'email';
182 $request->validate($rules);
186 * Send a response when a login attempt exception occurs.
188 protected function sendLoginAttemptExceptionResponse(LoginAttemptException $exception, Request $request)
190 if ($exception instanceof LoginAttemptEmailNeededException) {
192 session()->flash('request-email', true);
195 if ($message = $exception->getMessage()) {
196 $this->showWarningNotification($message);
199 return redirect('/login');