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