]> BookStack Code Mirror - bookstack/blob - app/Http/Controllers/Auth/LoginController.php
Merge branch 'feature/public-login-redirect' of git://github.com/Xiphoseer/BookStack...
[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         if ($request->has('intended')) {
80             redirect()->setIntendedUrl($request->get('intended'));
81         }
82
83         return view('auth.login', [
84           'socialDrivers' => $socialDrivers,
85           'authMethod' => $authMethod,
86         ]);
87     }
88
89     /**
90      * Handle a login request to the application.
91      *
92      * @param  \Illuminate\Http\Request  $request
93      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response|\Illuminate\Http\JsonResponse
94      *
95      * @throws \Illuminate\Validation\ValidationException
96      */
97     public function login(Request $request)
98     {
99         $this->validateLogin($request);
100
101         // If the class is using the ThrottlesLogins trait, we can automatically throttle
102         // the login attempts for this application. We'll key this by the username and
103         // the IP address of the client making these requests into this application.
104         if (method_exists($this, 'hasTooManyLoginAttempts') &&
105             $this->hasTooManyLoginAttempts($request)) {
106             $this->fireLockoutEvent($request);
107
108             return $this->sendLockoutResponse($request);
109         }
110
111         try {
112             if ($this->attemptLogin($request)) {
113                 return $this->sendLoginResponse($request);
114             }
115         } catch (LoginAttemptException $exception) {
116             return $this->sendLoginAttemptExceptionResponse($exception, $request);
117         }
118
119         // If the login attempt was unsuccessful we will increment the number of attempts
120         // to login and redirect the user back to the login form. Of course, when this
121         // user surpasses their maximum number of attempts they will get locked out.
122         $this->incrementLoginAttempts($request);
123
124         return $this->sendFailedLoginResponse($request);
125     }
126
127     /**
128      * Validate the user login request.
129      *
130      * @param  \Illuminate\Http\Request  $request
131      * @return void
132      *
133      * @throws \Illuminate\Validation\ValidationException
134      */
135     protected function validateLogin(Request $request)
136     {
137         $rules = ['password' => 'required|string'];
138         $authMethod = config('auth.method');
139
140         if ($authMethod === 'standard') {
141             $rules['email'] = 'required|email';
142         }
143
144         if ($authMethod === 'ldap') {
145             $rules['username'] = 'required|string';
146             $rules['email'] = 'email';
147         }
148
149         $request->validate($rules);
150     }
151
152     /**
153      * Send a response when a login attempt exception occurs.
154      */
155     protected function sendLoginAttemptExceptionResponse(LoginAttemptException $exception, Request $request)
156     {
157         if ($exception instanceof LoginAttemptEmailNeededException) {
158             $request->flash();
159             session()->flash('request-email', true);
160         }
161
162         if ($message = $exception->getMessage()) {
163             $this->showWarningNotification($message);
164         }
165
166         return redirect('/login');
167     }
168
169 }