firefly-iii/app/Http/Controllers/Auth/LoginController.php

151 lines
4.9 KiB
PHP
Raw Normal View History

2016-09-15 23:19:40 -05:00
<?php
2017-10-21 01:40:00 -05:00
/**
* LoginController.php
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
* This file is part of Firefly III.
*
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:41:58 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
2017-10-21 01:40:00 -05:00
*/
2018-07-08 00:59:58 -05:00
/** @noinspection PhpDynamicAsStaticMethodCallInspection */
2017-09-14 10:40:02 -05:00
declare(strict_types=1);
2016-09-15 23:19:40 -05:00
namespace FireflyIII\Http\Controllers\Auth;
2018-03-08 13:44:56 -06:00
use DB;
2017-09-14 09:13:25 -05:00
use FireflyConfig;
2016-09-15 23:19:40 -05:00
use FireflyIII\Http\Controllers\Controller;
2017-09-14 09:13:25 -05:00
use FireflyIII\User;
use Illuminate\Cookie\CookieJar;
2016-09-15 23:19:40 -05:00
use Illuminate\Foundation\Auth\AuthenticatesUsers;
2017-09-14 09:13:25 -05:00
use Illuminate\Http\Request;
/**
2017-12-17 11:23:10 -06:00
* @codeCoverageIgnore
* Class LoginController
2017-12-17 11:23:10 -06:00
*
* This controller handles authenticating users for the application and
* redirecting them to your home screen. The controller uses a trait
* to conveniently provide its functionality to your applications.
*/
2016-09-15 23:19:40 -05:00
class LoginController extends Controller
{
use AuthenticatesUsers;
/**
2017-09-09 15:03:27 -05:00
* Where to redirect users after login.
2016-09-15 23:19:40 -05:00
*
2017-09-09 15:03:27 -05:00
* @var string
2016-09-15 23:19:40 -05:00
*/
2017-09-09 15:03:27 -05:00
protected $redirectTo = '/home';
2016-09-16 00:22:57 -05:00
/**
2017-09-09 15:03:27 -05:00
* Create a new controller instance.
2016-12-28 11:49:30 -06:00
*/
2017-09-09 15:03:27 -05:00
public function __construct()
2016-12-28 11:49:30 -06:00
{
2017-09-12 11:24:42 -05:00
parent::__construct();
2017-09-09 15:03:27 -05:00
$this->middleware('guest')->except('logout');
2016-12-28 11:49:30 -06:00
}
2017-09-14 09:13:25 -05:00
2017-11-22 13:20:57 -06:00
/**
2017-11-25 01:54:52 -06:00
* @param Request $request
2017-11-22 13:20:57 -06:00
*
2018-07-09 12:24:08 -05:00
* @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response|void
* @throws \Illuminate\Validation\ValidationException
2017-11-22 13:20:57 -06:00
*/
public function login(Request $request)
{
$this->validateLogin($request);
// If the class is using the ThrottlesLogins trait, we can automatically throttle
// the login attempts for this application. We'll key this by the username and
// the IP address of the client making these requests into this application.
if ($this->hasTooManyLoginAttempts($request)) {
$this->fireLockoutEvent($request);
2018-07-09 12:24:08 -05:00
/** @noinspection PhpInconsistentReturnPointsInspection */
/** @noinspection PhpVoidFunctionResultUsedInspection */
2017-11-22 13:20:57 -06:00
return $this->sendLockoutResponse($request);
}
if ($this->attemptLogin($request)) {
// user is logged in. Save in session if the user requested session to be remembered:
$request->session()->put('remember_login', $request->filled('remember'));
2018-07-09 12:24:08 -05:00
/** @noinspection PhpInconsistentReturnPointsInspection */
/** @noinspection PhpVoidFunctionResultUsedInspection */
2017-11-22 13:20:57 -06:00
return $this->sendLoginResponse($request);
}
// If the login attempt was unsuccessful we will increment the number of attempts
// to login and redirect the user back to the login form. Of course, when this
// user surpasses their maximum number of attempts they will get locked out.
$this->incrementLoginAttempts($request);
2018-07-09 12:24:08 -05:00
/** @noinspection PhpInconsistentReturnPointsInspection */
/** @noinspection PhpVoidFunctionResultUsedInspection */
2017-11-22 13:20:57 -06:00
return $this->sendFailedLoginResponse($request);
}
/**
* Log the user out of the application.
*
2017-11-25 01:54:52 -06:00
* @param Request $request
* @param CookieJar $cookieJar
2017-11-22 14:12:27 -06:00
*
2017-12-29 02:05:35 -06:00
* @return $this|\Illuminate\Http\RedirectResponse
2017-11-22 13:20:57 -06:00
*/
public function logout(Request $request, CookieJar $cookieJar)
{
$this->guard()->logout();
$request->session()->invalidate();
$cookie = $cookieJar->forget('twoFactorAuthenticated');
return redirect('/')->withCookie($cookie);
}
2017-09-14 09:13:25 -05:00
/**
* Show the application's login form.
*
2017-12-16 12:48:31 -06:00
* @param Request $request
2017-11-17 22:46:19 -06:00
*
2017-11-25 01:54:52 -06:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
2017-09-14 09:13:25 -05:00
*/
2017-11-25 01:54:52 -06:00
public function showLoginForm(Request $request)
2017-09-14 09:13:25 -05:00
{
2018-03-08 13:44:56 -06:00
$count = DB::table('users')->count();
2018-07-08 00:59:58 -05:00
if (0 === $count) {
2018-03-08 13:44:56 -06:00
return redirect(route('register')); // @codeCoverageIgnore
}
2017-11-22 13:20:57 -06:00
// forget 2fa session thing.
2017-11-18 09:30:45 -06:00
$request->session()->forget('twoFactorAuthenticated');
2017-09-14 09:13:25 -05:00
// is allowed to?
$singleUserMode = FireflyConfig::get('single_user_mode', config('firefly.configuration.single_user_mode'))->data;
$userCount = User::count();
$allowRegistration = true;
2017-11-15 05:25:49 -06:00
if (true === $singleUserMode && $userCount > 0) {
2017-09-14 09:13:25 -05:00
$allowRegistration = false;
}
$email = $request->old('email');
$remember = $request->old('remember');
2017-11-25 01:54:52 -06:00
return view('auth.login', compact('allowRegistration', 'email', 'remember'));
2017-09-14 09:13:25 -05:00
}
2016-09-15 23:19:40 -05:00
}