mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-18 15:39:50 +00:00
Code cleanup.
This commit is contained in:
@@ -37,8 +37,6 @@ use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
/**
|
||||
* Class ForgotPasswordController
|
||||
*
|
||||
|
||||
*/
|
||||
class ForgotPasswordController extends Controller
|
||||
{
|
||||
@@ -60,9 +58,6 @@ class ForgotPasswordController extends Controller
|
||||
/**
|
||||
* Send a reset link to the given user.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param UserRepositoryInterface $repository
|
||||
*
|
||||
* @return Factory|RedirectResponse|View
|
||||
*/
|
||||
public function sendResetLinkEmail(Request $request, UserRepositoryInterface $repository)
|
||||
@@ -75,11 +70,10 @@ class ForgotPasswordController extends Controller
|
||||
return view('error', compact('message'));
|
||||
}
|
||||
|
||||
|
||||
$this->validateEmail($request);
|
||||
|
||||
// verify if the user is not a demo user. If so, we give him back an error.
|
||||
/** @var User|null $user */
|
||||
/** @var null|User $user */
|
||||
$user = User::where('email', $request->get('email'))->first();
|
||||
|
||||
if (null !== $user && $repository->hasRole($user, 'demo')) {
|
||||
@@ -103,8 +97,8 @@ class ForgotPasswordController extends Controller
|
||||
/**
|
||||
* Show form for email recovery.
|
||||
*
|
||||
*
|
||||
* @return Factory|View
|
||||
*
|
||||
* @throws FireflyException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
|
||||
@@ -24,7 +24,6 @@ declare(strict_types=1);
|
||||
namespace FireflyIII\Http\Controllers\Auth;
|
||||
|
||||
use Cookie;
|
||||
use DB;
|
||||
use FireflyIII\Events\ActuallyLoggedIn;
|
||||
use FireflyIII\Exceptions\FireflyException;
|
||||
use FireflyIII\Http\Controllers\Controller;
|
||||
@@ -50,8 +49,6 @@ use Psr\Container\NotFoundExceptionInterface;
|
||||
* 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.
|
||||
*
|
||||
|
||||
*/
|
||||
class LoginController extends Controller
|
||||
{
|
||||
@@ -60,8 +57,6 @@ class LoginController extends Controller
|
||||
|
||||
/**
|
||||
* Where to redirect users after login.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected string $redirectTo = RouteServiceProvider::HOME;
|
||||
|
||||
@@ -69,8 +64,6 @@ class LoginController extends Controller
|
||||
|
||||
/**
|
||||
* Create a new controller instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
@@ -82,10 +75,9 @@ class LoginController extends Controller
|
||||
/**
|
||||
* Handle a login request to the application.
|
||||
*
|
||||
* @return JsonResponse|RedirectResponse
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function login(Request $request): JsonResponse | RedirectResponse
|
||||
public function login(Request $request): JsonResponse|RedirectResponse
|
||||
{
|
||||
Log::channel('audit')->info(sprintf('User is trying to login using "%s"', $request->get($this->username())));
|
||||
app('log')->info('User is trying to login.');
|
||||
@@ -93,7 +85,7 @@ class LoginController extends Controller
|
||||
$this->validateLogin($request);
|
||||
app('log')->debug('Login data is present.');
|
||||
|
||||
/** Copied directly from AuthenticatesUsers, but with logging added: */
|
||||
// Copied directly from AuthenticatesUsers, but with logging added:
|
||||
// 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.
|
||||
@@ -104,7 +96,7 @@ class LoginController extends Controller
|
||||
|
||||
$this->sendLockoutResponse($request);
|
||||
}
|
||||
/** Copied directly from AuthenticatesUsers, but with logging added: */
|
||||
// Copied directly from AuthenticatesUsers, but with logging added:
|
||||
if ($this->attemptLogin($request)) {
|
||||
Log::channel('audit')->info(sprintf('User "%s" has been logged in.', $request->get($this->username())));
|
||||
app('log')->debug(sprintf('Redirect after login is %s.', $this->redirectPath()));
|
||||
@@ -119,7 +111,7 @@ class LoginController extends Controller
|
||||
}
|
||||
app('log')->warning('Login attempt failed.');
|
||||
|
||||
/** Copied directly from AuthenticatesUsers, but with logging added: */
|
||||
// Copied directly from AuthenticatesUsers, but with logging added:
|
||||
// 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.
|
||||
@@ -128,7 +120,7 @@ class LoginController extends Controller
|
||||
|
||||
$this->sendFailedLoginResponse($request);
|
||||
|
||||
/** @noinspection PhpUnreachableStatementInspection */
|
||||
// @noinspection PhpUnreachableStatementInspection
|
||||
return response()->json([]);
|
||||
}
|
||||
|
||||
@@ -142,33 +134,10 @@ class LoginController extends Controller
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the failed login response instance.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return void
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
* @throws ValidationException
|
||||
*/
|
||||
protected function sendFailedLoginResponse(Request $request)
|
||||
{
|
||||
$exception = ValidationException::withMessages(
|
||||
[
|
||||
$this->username() => [trans('auth.failed')],
|
||||
]
|
||||
);
|
||||
$exception->redirectTo = route('login');
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
|
||||
/**
|
||||
* Log the user out of the application.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return RedirectResponse|Redirector|Response
|
||||
* @return Redirector|RedirectResponse|Response
|
||||
*/
|
||||
public function logout(Request $request)
|
||||
{
|
||||
@@ -183,7 +152,7 @@ class LoginController extends Controller
|
||||
|
||||
// also logout current 2FA tokens.
|
||||
$cookieName = config('google2fa.cookie_name', 'google2fa_token');
|
||||
Cookie::forget($cookieName);
|
||||
\Cookie::forget($cookieName);
|
||||
|
||||
$this->guard()->logout();
|
||||
|
||||
@@ -201,9 +170,8 @@ class LoginController extends Controller
|
||||
/**
|
||||
* Show the application's login form.
|
||||
*
|
||||
* @param Request $request
|
||||
* @return Application|Factory|Redirector|RedirectResponse|View
|
||||
*
|
||||
* @return Factory|Application|View|Redirector|RedirectResponse
|
||||
* @throws FireflyException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
@@ -212,7 +180,7 @@ class LoginController extends Controller
|
||||
{
|
||||
Log::channel('audit')->info('Show login form (1.1).');
|
||||
|
||||
$count = DB::table('users')->count();
|
||||
$count = \DB::table('users')->count();
|
||||
$guard = config('auth.defaults.guard');
|
||||
$title = (string)trans('firefly.login_page_title');
|
||||
|
||||
@@ -246,4 +214,23 @@ class LoginController extends Controller
|
||||
|
||||
return view('auth.login', compact('allowRegistration', 'email', 'remember', 'allowReset', 'title', 'usernameField'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the failed login response instance.
|
||||
*
|
||||
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
|
||||
*
|
||||
* @throws ValidationException
|
||||
*/
|
||||
protected function sendFailedLoginResponse(Request $request)
|
||||
{
|
||||
$exception = ValidationException::withMessages(
|
||||
[
|
||||
$this->username() => [trans('auth.failed')],
|
||||
]
|
||||
);
|
||||
$exception->redirectTo = route('login');
|
||||
|
||||
throw $exception;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,8 +46,6 @@ use Psr\Container\NotFoundExceptionInterface;
|
||||
* This controller handles the registration of new users as well as their
|
||||
* validation and creation. By default this controller uses a trait to
|
||||
* provide this functionality without requiring any additional code.
|
||||
*
|
||||
|
||||
*/
|
||||
class RegisterController extends Controller
|
||||
{
|
||||
@@ -77,9 +75,8 @@ class RegisterController extends Controller
|
||||
/**
|
||||
* Handle a registration request for the application.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Application|Redirector|RedirectResponse
|
||||
*
|
||||
* @throws FireflyException
|
||||
* @throws ValidationException
|
||||
*/
|
||||
@@ -94,7 +91,6 @@ class RegisterController extends Controller
|
||||
throw new FireflyException('Registration is currently not available :(');
|
||||
}
|
||||
|
||||
|
||||
$this->validator($request->all())->validate();
|
||||
$user = $this->createUser($request->all());
|
||||
app('log')->info(sprintf('Registered new user %s', $user->email));
|
||||
@@ -113,35 +109,11 @@ class RegisterController extends Controller
|
||||
return redirect($this->redirectPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function allowedToRegister(): bool
|
||||
{
|
||||
// is allowed to register?
|
||||
$allowRegistration = true;
|
||||
try {
|
||||
$singleUserMode = app('fireflyconfig')->get('single_user_mode', config('firefly.configuration.single_user_mode'))->data;
|
||||
} catch (ContainerExceptionInterface | NotFoundExceptionInterface $e) {
|
||||
$singleUserMode = true;
|
||||
}
|
||||
$userCount = User::count();
|
||||
$guard = config('auth.defaults.guard');
|
||||
if (true === $singleUserMode && $userCount > 0 && 'web' === $guard) {
|
||||
$allowRegistration = false;
|
||||
}
|
||||
if ('web' !== $guard) {
|
||||
$allowRegistration = false;
|
||||
}
|
||||
return $allowRegistration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the application registration form if the invitation code is valid.
|
||||
*
|
||||
*
|
||||
* @return Factory|View
|
||||
*
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws FireflyException
|
||||
* @throws NotFoundExceptionInterface
|
||||
@@ -174,9 +146,8 @@ class RegisterController extends Controller
|
||||
/**
|
||||
* Show the application registration form.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Factory|View
|
||||
*
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws FireflyException
|
||||
* @throws NotFoundExceptionInterface
|
||||
@@ -197,4 +168,29 @@ class RegisterController extends Controller
|
||||
|
||||
return view('auth.register', compact('isDemoSite', 'email', 'pageTitle'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws FireflyException
|
||||
*/
|
||||
protected function allowedToRegister(): bool
|
||||
{
|
||||
// is allowed to register?
|
||||
$allowRegistration = true;
|
||||
|
||||
try {
|
||||
$singleUserMode = app('fireflyconfig')->get('single_user_mode', config('firefly.configuration.single_user_mode'))->data;
|
||||
} catch (ContainerExceptionInterface|NotFoundExceptionInterface $e) {
|
||||
$singleUserMode = true;
|
||||
}
|
||||
$userCount = User::count();
|
||||
$guard = config('auth.defaults.guard');
|
||||
if (true === $singleUserMode && $userCount > 0 && 'web' === $guard) {
|
||||
$allowRegistration = false;
|
||||
}
|
||||
if ('web' !== $guard) {
|
||||
$allowRegistration = false;
|
||||
}
|
||||
|
||||
return $allowRegistration;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,8 +43,6 @@ use Psr\Container\NotFoundExceptionInterface;
|
||||
* This controller is responsible for handling password reset requests
|
||||
* and uses a simple trait to include this behavior. You're free to
|
||||
* explore this trait and override any methods you wish to tweak.
|
||||
*
|
||||
|
||||
*/
|
||||
class ResetPasswordController extends Controller
|
||||
{
|
||||
@@ -73,11 +71,9 @@ class ResetPasswordController extends Controller
|
||||
/**
|
||||
* Reset the given user's password.
|
||||
*
|
||||
* @param Request $request
|
||||
*
|
||||
* @return Factory|JsonResponse|RedirectResponse|View
|
||||
* @throws ValidationException
|
||||
*
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function reset(Request $request)
|
||||
{
|
||||
@@ -107,7 +103,7 @@ class ResetPasswordController extends Controller
|
||||
// If the password was successfully reset, we will redirect the user back to
|
||||
// the application's home authenticated view. If there is an error we can
|
||||
// redirect them back to where they came from with their error message.
|
||||
return $response === Password::PASSWORD_RESET
|
||||
return Password::PASSWORD_RESET === $response
|
||||
? $this->sendResetResponse($request, $response)
|
||||
: $this->sendResetFailedResponse($request, $response);
|
||||
}
|
||||
@@ -117,10 +113,10 @@ class ResetPasswordController extends Controller
|
||||
*
|
||||
* If no token is present, display the link request form.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param null $token
|
||||
* @param null $token
|
||||
*
|
||||
* @return Factory|View
|
||||
*
|
||||
* @throws FireflyException
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
|
||||
@@ -54,9 +54,7 @@ class TwoFactorController extends Controller
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
*
|
||||
* @return RedirectResponse|Redirector
|
||||
* @return Redirector|RedirectResponse
|
||||
*/
|
||||
public function submitMFA(Request $request)
|
||||
{
|
||||
@@ -101,11 +99,6 @@ class TwoFactorController extends Controller
|
||||
/**
|
||||
* Each MFA history has a timestamp and a code, saving the MFA entries for 5 minutes. So if the
|
||||
* submitted MFA code has been submitted in the last 5 minutes, it won't work despite being valid.
|
||||
*
|
||||
* @param string $mfaCode
|
||||
* @param array $mfaHistory
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function inMFAHistory(string $mfaCode, array $mfaHistory): bool
|
||||
{
|
||||
@@ -143,9 +136,6 @@ class TwoFactorController extends Controller
|
||||
app('preferences')->set('mfa_history', $newHistory);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $mfaCode
|
||||
*/
|
||||
private function addToMFAHistory(string $mfaCode): void
|
||||
{
|
||||
/** @var array $mfaHistory */
|
||||
@@ -162,10 +152,6 @@ class TwoFactorController extends Controller
|
||||
|
||||
/**
|
||||
* Checks if code is in users backup codes.
|
||||
*
|
||||
* @param string $mfaCode
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isBackupCode(string $mfaCode): bool
|
||||
{
|
||||
@@ -182,8 +168,6 @@ class TwoFactorController extends Controller
|
||||
|
||||
/**
|
||||
* Remove the used code from the list of backup codes.
|
||||
*
|
||||
* @param string $mfaCode
|
||||
*/
|
||||
private function removeFromBackupCodes(string $mfaCode): void
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user