]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/LoginController.php
Allow book, shelf, settings & profile form input validation to skip image
[bookstack] / app / Http / Controllers / Auth / LoginController.php
1 <?php
2
3 namespace BookStack\Http\Controllers\Auth;
4
5 use BookStack\Auth\Access\SocialAuthService;
6 use BookStack\Exceptions\LoginAttemptEmailNeededException;
7 use BookStack\Exceptions\LoginAttemptException;
8 use BookStack\Exceptions\UserRegistrationException;
9 use BookStack\Http\Controllers\Controller;
10 use Illuminate\Foundation\Auth\AuthenticatesUsers;
11 use Illuminate\Http\Request;
12
13 class LoginController extends Controller
14 {
15     /*
16     |--------------------------------------------------------------------------
17     | Login Controller
18     |--------------------------------------------------------------------------
19     |
20     | This controller handles authenticating users for the application and
21     | redirecting them to your home screen. The controller uses a trait
22     | to conveniently provide its functionality to your applications.
23     |
24     */
25
26     use AuthenticatesUsers;
27
28     /**
29      * Redirection paths
30      */
31     protected $redirectTo = '/';
32     protected $redirectPath = '/';
33     protected $redirectAfterLogout = '/login';
34
35     protected $socialAuthService;
36
37     /**
38      * Create a new controller instance.
39      */
40     public function __construct(SocialAuthService $socialAuthService)
41     {
42         $this->middleware('guest', ['only' => ['getLogin', 'login']]);
43         $this->middleware('guard:standard,ldap', ['only' => ['login', 'logout']]);
44
45         $this->socialAuthService = $socialAuthService;
46         $this->redirectPath = url('/');
47         $this->redirectAfterLogout = url('/login');
48         parent::__construct();
49     }
50
51     public function username()
52     {
53         return config('auth.method') === 'standard' ? 'email' : 'username';
54     }
55
56     /**
57      * Get the needed authorization credentials from the request.
58      */
59     protected function credentials(Request $request)
60     {
61         return $request->only('username', 'email', 'password');
62     }
63
64     /**
65      * Show the application login form.
66      */
67     public function getLogin(Request $request)
68     {
69         $socialDrivers = $this->socialAuthService->getActiveDrivers();
70         $authMethod = config('auth.method');
71
72         if ($request->has('email')) {
73             session()->flashInput([
74                 'email' => $request->get('email'),
75                 'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
76             ]);
77         }
78
79         return view('auth.login', [
80           'socialDrivers' => $socialDrivers,
81           'authMethod' => $authMethod,
82         ]);
83     }
84
85     /**
86      * Handle a login request to the application.
87      *
88      * @param  \Illuminate\Http\Request  $request
89      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
90      *
91      * @throws \Illuminate\Validation\ValidationException
92      */
93     public function login(Request $request)
94     {
95         $this->validateLogin($request);
96
97         // If the class is using the ThrottlesLogins trait, we can automatically throttle
98         // the login attempts for this application. We'll key this by the username and
99         // the IP address of the client making these requests into this application.
100         if (method_exists($this, 'hasTooManyLoginAttempts') &&
101             $this->hasTooManyLoginAttempts($request)) {
102             $this->fireLockoutEvent($request);
103
104             return $this->sendLockoutResponse($request);
105         }
106
107         try {
108             if ($this->attemptLogin($request)) {
109                 return $this->sendLoginResponse($request);
110             }
111         } catch (LoginAttemptException $exception) {
112             return $this->sendLoginAttemptExceptionResponse($exception, $request);
113         }
114
115         // If the login attempt was unsuccessful we will increment the number of attempts
116         // to login and redirect the user back to the login form. Of course, when this
117         // user surpasses their maximum number of attempts they will get locked out.
118         $this->incrementLoginAttempts($request);
119
120         return $this->sendFailedLoginResponse($request);
121     }
122
123     /**
124      * Validate the user login request.
125      *
126      * @param  \Illuminate\Http\Request  $request
127      * @return void
128      *
129      * @throws \Illuminate\Validation\ValidationException
130      */
131     protected function validateLogin(Request $request)
132     {
133         $rules = ['password' => 'required|string'];
134         $authMethod = config('auth.method');
135
136         if ($authMethod === 'standard') {
137             $rules['email'] = 'required|email';
138         }
139
140         if ($authMethod === 'ldap') {
141             $rules['username'] = 'required|string';
142             $rules['email'] = 'email';
143         }
144
145         $request->validate($rules);
146     }
147
148     /**
149      * Send a response when a login attempt exception occurs.
150      */
151     protected function sendLoginAttemptExceptionResponse(LoginAttemptException $exception, Request $request)
152     {
153         if ($exception instanceof LoginAttemptEmailNeededException) {
154             $request->flash();
155             session()->flash('request-email', true);
156         }
157
158         if ($message = $exception->getMessage()) {
159             $this->showWarningNotification($message);
160         }
161
162         return redirect('/login');
163     }
164
165 }