mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 02:36:28 +00:00 
			
		
		
		
	Authentication in separate commit.
This commit is contained in:
		| @@ -1,72 +1,32 @@ | ||||
| <?php | ||||
| /** | ||||
|  * ForgotPasswordController.php | ||||
|  * Copyright (C) 2016 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This software may be modified and distributed under the terms of the | ||||
|  * Creative Commons Attribution-ShareAlike 4.0 International License. | ||||
|  * | ||||
|  * See the LICENSE file for details. | ||||
|  */ | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace FireflyIII\Http\Controllers\Auth; | ||||
|  | ||||
| use FireflyIII\Http\Controllers\Controller; | ||||
| use FireflyIII\Repositories\User\UserRepositoryInterface; | ||||
| use FireflyIII\User; | ||||
| use Illuminate\Foundation\Auth\SendsPasswordResetEmails; | ||||
| use Illuminate\Http\Request; | ||||
| use Password; | ||||
|  | ||||
| /** | ||||
|  * Class ForgotPasswordController | ||||
|  * | ||||
|  * @package FireflyIII\Http\Controllers\Auth | ||||
|  */ | ||||
| class ForgotPasswordController extends Controller | ||||
| { | ||||
|     /* | ||||
|     |-------------------------------------------------------------------------- | ||||
|     | Password Reset Controller | ||||
|     |-------------------------------------------------------------------------- | ||||
|     | | ||||
|     | This controller is responsible for handling password reset emails and | ||||
|     | includes a trait which assists in sending these notifications from | ||||
|     | your application to your users. Feel free to explore this trait. | ||||
|     | | ||||
|     */ | ||||
|  | ||||
|     use SendsPasswordResetEmails; | ||||
|  | ||||
|     /** | ||||
|      * Create a new controller instance. | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function __construct() | ||||
|     { | ||||
|         parent::__construct(); | ||||
|         $this->middleware('guest'); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Send a reset link to the given user. | ||||
|      * | ||||
|      * @param  Request                $request | ||||
|      * | ||||
|      * @param UserRepositoryInterface $repository | ||||
|      * | ||||
|      * @return \Illuminate\Http\RedirectResponse | ||||
|      */ | ||||
|     public function sendResetLinkEmail(Request $request, UserRepositoryInterface $repository) | ||||
|     { | ||||
|         $this->validate($request, ['email' => 'required|email']); | ||||
|  | ||||
|         // verify if the user is not a demo user. If so, we give him back an error. | ||||
|         $user = User::where('email', $request->get('email'))->first(); | ||||
|  | ||||
|         if (!is_null($user) && $repository->hasRole($user, 'demo')) { | ||||
|             return back()->withErrors(['email' => trans('firefly.cannot_reset_demo_user')]); | ||||
|         } | ||||
|  | ||||
|         $response = $this->broker()->sendResetLink($request->only('email')); | ||||
|  | ||||
|         if ($response === Password::RESET_LINK_SENT) { | ||||
|             return back()->with('status', trans($response)); | ||||
|         } | ||||
|  | ||||
|         // If an error was returned by the password broker, we will get this message | ||||
|         // translated so we can notify a user of the problem. We'll redirect back | ||||
|         // to where the users came from so they can attempt this process again. | ||||
|         return back()->withErrors(['email' => trans($response)]); // @codeCoverageIgnore | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,191 +1,39 @@ | ||||
| <?php | ||||
| /** | ||||
|  * LoginController.php | ||||
|  * Copyright (C) 2016 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This software may be modified and distributed under the terms of the | ||||
|  * Creative Commons Attribution-ShareAlike 4.0 International License. | ||||
|  * | ||||
|  * See the LICENSE file for details. | ||||
|  */ | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace FireflyIII\Http\Controllers\Auth; | ||||
|  | ||||
| use Config; | ||||
| use FireflyConfig; | ||||
| use FireflyIII\Http\Controllers\Controller; | ||||
| use FireflyIII\User; | ||||
| use Illuminate\Cookie\CookieJar; | ||||
| use Illuminate\Foundation\Auth\AuthenticatesUsers; | ||||
| use Illuminate\Http\Request; | ||||
| use Lang; | ||||
|  | ||||
| /** | ||||
|  * @codeCoverageIgnore | ||||
|  * | ||||
|  * Class LoginController | ||||
|  * | ||||
|  * @package FireflyIII\Http\Controllers\Auth | ||||
|  */ | ||||
| class LoginController extends Controller | ||||
| { | ||||
|     /* | ||||
|     |-------------------------------------------------------------------------- | ||||
|     | Login Controller | ||||
|     |-------------------------------------------------------------------------- | ||||
|     | | ||||
|     | 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. | ||||
|     | | ||||
|     */ | ||||
|  | ||||
|     use AuthenticatesUsers; | ||||
|  | ||||
|     /** | ||||
|      * Where to redirect users after login. | ||||
|      * | ||||
|      * @var string | ||||
|      */ | ||||
|     protected $redirectTo = '/home'; | ||||
|  | ||||
|     /** | ||||
|      * Create a new controller instance. | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function __construct() | ||||
|     { | ||||
|         parent::__construct(); | ||||
|         $this->middleware('guest', ['except' => 'logout']); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Handle a login request to the application. | ||||
|      * | ||||
|      * @param Request $request | ||||
|      * | ||||
|      * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\Response | ||||
|      */ | ||||
|     public function login(Request $request) | ||||
|     { | ||||
|         $this->validateLogin($request); | ||||
|         $lockedOut = $this->hasTooManyLoginAttempts($request); | ||||
|         if ($lockedOut) { | ||||
|             $this->fireLockoutEvent($request); | ||||
|  | ||||
|             return $this->sendLockoutResponse($request); | ||||
|         } | ||||
|  | ||||
|         $credentials            = $this->credentials($request); | ||||
|         $credentials['blocked'] = 0; // must not be blocked. | ||||
|  | ||||
|         if ($this->guard()->attempt($credentials, $request->has('remember'))) { | ||||
|             return $this->sendLoginResponse($request); | ||||
|         } | ||||
|  | ||||
|         $errorMessage = $this->getBlockedError($credentials['email']); | ||||
|  | ||||
|         if (!$lockedOut) { | ||||
|             $this->incrementLoginAttempts($request); | ||||
|         } | ||||
|  | ||||
|         return $this->sendFailedLoginResponse($request, $errorMessage); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param Request   $request | ||||
|      * @param CookieJar $cookieJar | ||||
|      * | ||||
|      * @return $this | ||||
|      */ | ||||
|     public function logout(Request $request, CookieJar $cookieJar) | ||||
|     { | ||||
|         if (intval(getenv('SANDSTORM')) === 1) { | ||||
|             return view('error')->with('message', strval(trans('firefly.sandstorm_not_available'))); | ||||
|         } | ||||
|  | ||||
|         $cookie = $cookieJar->forever('twoFactorAuthenticated', 'false'); | ||||
|  | ||||
|         $this->guard()->logout(); | ||||
|  | ||||
|         $request->session()->flush(); | ||||
|  | ||||
|         $request->session()->regenerate(); | ||||
|  | ||||
|         return redirect('/')->withCookie($cookie); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @return string | ||||
|      */ | ||||
|     public function redirectTo(): string | ||||
|     { | ||||
|         return route('index'); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Show the application login form. | ||||
|      * | ||||
|      * @param Request   $request | ||||
|      * | ||||
|      * @param CookieJar $cookieJar | ||||
|      * | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function showLoginForm(Request $request, CookieJar $cookieJar) | ||||
|     { | ||||
|         // forget 2fa cookie: | ||||
|         $cookie = $cookieJar->forever('twoFactorAuthenticated', 'false'); | ||||
|         // is allowed to? | ||||
|         $singleUserMode    = FireflyConfig::get('single_user_mode', Config::get('firefly.configuration.single_user_mode'))->data; | ||||
|         $userCount         = User::count(); | ||||
|         $allowRegistration = true; | ||||
|         if ($singleUserMode === true && $userCount > 0) { | ||||
|             $allowRegistration = false; | ||||
|         } | ||||
|  | ||||
|         $email    = $request->old('email'); | ||||
|         $remember = $request->old('remember'); | ||||
|  | ||||
|         return view('auth.login', compact('allowRegistration', 'email', 'remember'))->withCookie($cookie); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get the failed login message. | ||||
|      * | ||||
|      * @param string $message | ||||
|      * | ||||
|      * @return string | ||||
|      */ | ||||
|     protected function getFailedLoginMessage(string $message) | ||||
|     { | ||||
|         if (strlen($message) > 0) { | ||||
|             return $message; | ||||
|         } | ||||
|  | ||||
|         return Lang::has('auth.failed') ? Lang::get('auth.failed') : 'These credentials do not match our records.'; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get the failed login response instance. | ||||
|      * | ||||
|      * @param \Illuminate\Http\Request $request | ||||
|      * @param string                   $message | ||||
|      * | ||||
|      * @return \Illuminate\Http\RedirectResponse | ||||
|      */ | ||||
|     protected function sendFailedLoginResponse(Request $request, string $message) | ||||
|     { | ||||
|         return redirect()->back() | ||||
|                          ->withInput($request->only($this->username(), 'remember')) | ||||
|                          ->withErrors( | ||||
|                              [ | ||||
|                                  $this->username() => $this->getFailedLoginMessage($message), | ||||
|                              ] | ||||
|                          ); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param string $email | ||||
|      * | ||||
|      * @return string | ||||
|      */ | ||||
|     private function getBlockedError(string $email): string | ||||
|     { | ||||
|         // check if user is blocked: | ||||
|         $errorMessage = ''; | ||||
|         /** @var User $foundUser */ | ||||
|         $foundUser = User::where('email', $email)->where('blocked', 1)->first(); | ||||
|         if (!is_null($foundUser)) { | ||||
|             // user exists, but is blocked: | ||||
|             $code         = strlen(strval($foundUser->blocked_code)) > 0 ? $foundUser->blocked_code : 'general_blocked'; | ||||
|             $errorMessage = strval(trans('firefly.' . $code . '_error', ['email' => $email])); | ||||
|         } | ||||
|  | ||||
|         return $errorMessage; | ||||
|         $this->middleware('guest')->except('logout'); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,88 +0,0 @@ | ||||
| <?php | ||||
| /** | ||||
|  * PasswordController.php | ||||
|  * Copyright (C) 2016 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This software may be modified and distributed under the terms of the | ||||
|  * Creative Commons Attribution-ShareAlike 4.0 International License. | ||||
|  * | ||||
|  * See the LICENSE file for details. | ||||
|  */ | ||||
|  | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace FireflyIII\Http\Controllers\Auth; | ||||
|  | ||||
| use FireflyIII\Http\Controllers\Controller; | ||||
| use FireflyIII\User; | ||||
| use Illuminate\Foundation\Auth\ResetsPasswords; | ||||
| use Illuminate\Http\Request; | ||||
| use Illuminate\Mail\Message; | ||||
| use Illuminate\Support\Facades\Password; | ||||
|  | ||||
|  | ||||
| /** | ||||
|  * @codeCoverageIgnore | ||||
|  * | ||||
|  * Class PasswordController | ||||
|  * | ||||
|  * @package FireflyIII\Http\Controllers\Auth | ||||
|  * @method getEmailSubject() | ||||
|  * @method getSendResetLinkEmailSuccessResponse(string $response) | ||||
|  * @method getSendResetLinkEmailFailureResponse(string $response) | ||||
|  */ | ||||
| class PasswordController extends Controller | ||||
| { | ||||
|  | ||||
|     use ResetsPasswords; | ||||
|  | ||||
|     /** | ||||
|      * Create a new password controller instance. | ||||
|      * | ||||
|      */ | ||||
|     public function __construct() | ||||
|     { | ||||
|         parent::__construct(); | ||||
|  | ||||
|         $this->middleware('guest'); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Send a reset link to the given user. | ||||
|      * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's 7 but ok | ||||
|      * | ||||
|      * @param  \Illuminate\Http\Request $request | ||||
|      * | ||||
|      * @return \Symfony\Component\HttpFoundation\Response | ||||
|      */ | ||||
|     public function sendResetLinkEmail(Request $request) | ||||
|     { | ||||
|         $this->validate($request, ['email' => 'required|email']); | ||||
|  | ||||
|         $user     = User::whereEmail($request->get('email'))->first(); | ||||
|         $response = 'passwords.blocked'; | ||||
|  | ||||
|         if (is_null($user)) { | ||||
|             $response = Password::INVALID_USER; | ||||
|         } | ||||
|  | ||||
|         if (!is_null($user) && intval($user->blocked) === 0) { | ||||
|             $response = Password::sendResetLink( | ||||
|                 $request->only('email'), function (Message $message) { | ||||
|                 $message->subject($this->getEmailSubject()); | ||||
|             } | ||||
|             ); | ||||
|         } | ||||
|  | ||||
|         switch ($response) { | ||||
|             case Password::RESET_LINK_SENT: | ||||
|                 return $this->getSendResetLinkEmailSuccessResponse($response); | ||||
|  | ||||
|             case Password::INVALID_USER: | ||||
|             case 'passwords.blocked': | ||||
|             default: | ||||
|                 return $this->getSendResetLinkEmailFailureResponse($response); | ||||
|         } | ||||
|     } | ||||
|  | ||||
| } | ||||
| @@ -1,43 +1,29 @@ | ||||
| <?php | ||||
| /** | ||||
|  * RegisterController.php | ||||
|  * Copyright (C) 2016 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This software may be modified and distributed under the terms of the | ||||
|  * Creative Commons Attribution-ShareAlike 4.0 International License. | ||||
|  * | ||||
|  * See the LICENSE file for details. | ||||
|  */ | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace FireflyIII\Http\Controllers\Auth; | ||||
|  | ||||
| use Auth; | ||||
| use Config; | ||||
| use FireflyConfig; | ||||
| use FireflyIII\Events\RegisteredUser; | ||||
| use FireflyIII\Http\Controllers\Controller; | ||||
| use FireflyIII\Http\Requests\UserRegistrationRequest; | ||||
| use FireflyIII\User; | ||||
| use FireflyIII\Http\Controllers\Controller; | ||||
| use Illuminate\Support\Facades\Validator; | ||||
| use Illuminate\Foundation\Auth\RegistersUsers; | ||||
| use Illuminate\Http\Request; | ||||
| use Session; | ||||
| use Validator; | ||||
|  | ||||
| /** | ||||
|  * @codeCoverageIgnore | ||||
|  * | ||||
|  * Class RegisterController | ||||
|  * | ||||
|  * @package FireflyIII\Http\Controllers\Auth | ||||
|  */ | ||||
| class RegisterController extends Controller | ||||
| { | ||||
|     /* | ||||
|     |-------------------------------------------------------------------------- | ||||
|     | Register Controller | ||||
|     |-------------------------------------------------------------------------- | ||||
|     | | ||||
|     | 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. | ||||
|     | | ||||
|     */ | ||||
|  | ||||
|     use RegistersUsers; | ||||
|  | ||||
|     /** | ||||
|      * Where to redirect users after login / registration. | ||||
|      * Where to redirect users after registration. | ||||
|      * | ||||
|      * @var string | ||||
|      */ | ||||
| @@ -45,111 +31,41 @@ class RegisterController extends Controller | ||||
|  | ||||
|     /** | ||||
|      * Create a new controller instance. | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function __construct() | ||||
|     { | ||||
|         parent::__construct(); | ||||
|         $this->middleware('guest'); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * @param UserRegistrationRequest|Request $request | ||||
|      * | ||||
|      * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View | ||||
|      */ | ||||
|     public function register(UserRegistrationRequest $request) | ||||
|     { | ||||
|         // is allowed to? | ||||
|         $singleUserMode = FireflyConfig::get('single_user_mode', Config::get('firefly.configuration.single_user_mode'))->data; | ||||
|         $userCount      = User::count(); | ||||
|         if ($singleUserMode === true && $userCount > 0) { | ||||
|             $message = 'Registration is currently not available.'; | ||||
|  | ||||
|             return view('error', compact('message')); | ||||
|         } | ||||
|  | ||||
|  | ||||
|         $validator = $this->validator($request->all()); | ||||
|  | ||||
|         if ($validator->fails()) { | ||||
|             $this->throwValidationException($request, $validator); | ||||
|         } | ||||
|  | ||||
|         $user = $this->create($request->all()); | ||||
|  | ||||
|         // trigger user registration event: | ||||
|         event(new RegisteredUser($user, $request->ip())); | ||||
|  | ||||
|         Auth::login($user); | ||||
|  | ||||
|         Session::flash('success', strval(trans('firefly.registered'))); | ||||
|         Session::flash('gaEventCategory', 'user'); | ||||
|         Session::flash('gaEventAction', 'new-registration'); | ||||
|  | ||||
|         return redirect($this->redirectPath()); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * OLD | ||||
|      * Show the application registration form. | ||||
|      * | ||||
|      * @param Request $request | ||||
|      * | ||||
|      * @return \Illuminate\Http\Response | ||||
|      */ | ||||
|     public function showRegistrationForm(Request $request) | ||||
|     { | ||||
|         // is demo site? | ||||
|         $isDemoSite = FireflyConfig::get('is_demo_site', Config::get('firefly.configuration.is_demo_site'))->data; | ||||
|  | ||||
|         // is allowed to? | ||||
|         $singleUserMode = FireflyConfig::get('single_user_mode', Config::get('firefly.configuration.single_user_mode'))->data; | ||||
|         $userCount      = User::count(); | ||||
|         if ($singleUserMode === true && $userCount > 0) { | ||||
|             $message = 'Registration is currently not available.'; | ||||
|  | ||||
|             return view('error', compact('message')); | ||||
|         } | ||||
|  | ||||
|         $email = $request->old('email'); | ||||
|  | ||||
|         return view('auth.register', compact('isDemoSite', 'email')); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Create a new user instance after a valid registration. | ||||
|      * | ||||
|      * @param  array $data | ||||
|      * | ||||
|      * @return User | ||||
|      */ | ||||
|     protected function create(array $data) | ||||
|     { | ||||
|         /** @var User $user */ | ||||
|         $user = User::create( | ||||
|             [ | ||||
|                 'email'    => $data['email'], | ||||
|                 'password' => bcrypt($data['password']), | ||||
|             ] | ||||
|         ); | ||||
|  | ||||
|         return $user; | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Get a validator for an incoming registration request. | ||||
|      * | ||||
|      * @param  array $data | ||||
|      * | ||||
|      * @param  array  $data | ||||
|      * @return \Illuminate\Contracts\Validation\Validator | ||||
|      */ | ||||
|     protected function validator(array $data) | ||||
|     { | ||||
|         return Validator::make( | ||||
|             $data, [ | ||||
|                      'email'    => 'required|email|max:255|unique:users', | ||||
|                      'password' => 'required|min:6|confirmed', | ||||
|                  ] | ||||
|         ); | ||||
|         return Validator::make($data, [ | ||||
|             'name' => 'required|string|max:255', | ||||
|             'email' => 'required|string|email|max:255|unique:users', | ||||
|             'password' => 'required|string|min:6|confirmed', | ||||
|         ]); | ||||
|     } | ||||
|  | ||||
|     /** | ||||
|      * Create a new user instance after a valid registration. | ||||
|      * | ||||
|      * @param  array  $data | ||||
|      * @return \FireflyIII\User | ||||
|      */ | ||||
|     protected function create(array $data) | ||||
|     { | ||||
|         return User::create([ | ||||
|             'name' => $data['name'], | ||||
|             'email' => $data['email'], | ||||
|             'password' => bcrypt($data['password']), | ||||
|         ]); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,40 +1,39 @@ | ||||
| <?php | ||||
| /** | ||||
|  * ResetPasswordController.php | ||||
|  * Copyright (C) 2016 thegrumpydictator@gmail.com | ||||
|  * | ||||
|  * This software may be modified and distributed under the terms of the | ||||
|  * Creative Commons Attribution-ShareAlike 4.0 International License. | ||||
|  * | ||||
|  * See the LICENSE file for details. | ||||
|  */ | ||||
| declare(strict_types=1); | ||||
|  | ||||
| namespace FireflyIII\Http\Controllers\Auth; | ||||
|  | ||||
| use FireflyIII\Http\Controllers\Controller; | ||||
| use Illuminate\Foundation\Auth\ResetsPasswords; | ||||
|  | ||||
| /** | ||||
|  * @codeCoverageIgnore | ||||
|  * | ||||
|  * Class ResetPasswordController | ||||
|  * | ||||
|  * @package FireflyIII\Http\Controllers\Auth | ||||
|  */ | ||||
| class ResetPasswordController extends Controller | ||||
| { | ||||
|     /* | ||||
|     |-------------------------------------------------------------------------- | ||||
|     | Password Reset Controller | ||||
|     |-------------------------------------------------------------------------- | ||||
|     | | ||||
|     | 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. | ||||
|     | | ||||
|     */ | ||||
|  | ||||
|     use ResetsPasswords; | ||||
|  | ||||
|     /** | ||||
|      * Where to redirect users after resetting their password. | ||||
|      * | ||||
|      * @var string | ||||
|      */ | ||||
|     protected $redirectTo = '/home'; | ||||
|  | ||||
|     /** | ||||
|      * Create a new controller instance. | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function __construct() | ||||
|     { | ||||
|         parent::__construct(); | ||||
|  | ||||
|         $this->middleware('guest'); | ||||
|     } | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user