Files
firefly-iii/app/Http/Controllers/Auth/ResetPasswordController.php

151 lines
5.2 KiB
PHP
Raw Normal View History

2016-09-16 06:19:40 +02:00
<?php
2017-10-21 08:40:00 +02:00
/**
* ResetPasswordController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2017-10-21 08:40:00 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2017-10-21 08:40:00 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 08:40:00 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2017-10-21 08:40:00 +02:00
*/
2018-07-08 07:59:58 +02:00
/** @noinspection PhpDynamicAsStaticMethodCallInspection */
2017-09-14 17:40:02 +02:00
declare(strict_types=1);
2016-09-16 06:19:40 +02:00
namespace FireflyIII\Http\Controllers\Auth;
2020-08-14 09:59:56 +02:00
use FireflyIII\Exceptions\FireflyException;
2016-09-16 06:19:40 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\User;
use Illuminate\Contracts\View\Factory;
2016-09-16 06:19:40 +02:00
use Illuminate\Foundation\Auth\ResetsPasswords;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
2018-10-13 15:06:56 +02:00
use Illuminate\Support\Facades\Password;
use Illuminate\View\View;
2016-09-16 06:19:40 +02:00
2017-12-17 14:30:53 +01:00
/**
* Class ResetPasswordController
2017-12-17 18:23:10 +01:00
*
* 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.
2018-07-22 08:10:16 +02:00
*
* @codeCoverageIgnore
2017-12-17 14:30:53 +01:00
*/
2016-09-16 06:19:40 +02:00
class ResetPasswordController extends Controller
{
use ResetsPasswords;
2017-09-09 22:03:27 +02:00
/**
* Where to redirect users after resetting their password.
*
* @var string
*/
protected $redirectTo = '/home';
2016-09-16 06:19:40 +02:00
/**
* Create a new controller instance.
*/
public function __construct()
{
2017-09-16 07:17:58 +02:00
parent::__construct();
2016-09-16 06:19:40 +02:00
$this->middleware('guest');
2020-08-14 09:59:56 +02:00
$loginProvider = config('firefly.login_provider');
$authGuard = config('firefly.authentication_guard');
if ('eloquent' !== $loginProvider || 'web' !== $authGuard) {
throw new FireflyException('Using external identity provider. Cannot continue.');
}
2016-09-16 06:19:40 +02:00
}
2018-10-13 15:06:56 +02:00
/**
* Reset the given user's password.
*
* @param Request $request
2018-10-13 15:06:56 +02:00
*
2020-08-14 09:59:56 +02:00
* @return Factory|JsonResponse|RedirectResponse|View
2018-10-13 15:06:56 +02:00
* @throws \Illuminate\Validation\ValidationException
*
2018-10-13 15:06:56 +02:00
*/
public function reset(Request $request)
{
$loginProvider = config('firefly.login_provider');
2018-10-13 15:06:56 +02:00
if ('eloquent' !== $loginProvider) {
$message = sprintf('Cannot reset password when authenticating over "%s".', $loginProvider);
return prefixView('error', compact('message'));
2018-10-13 15:06:56 +02:00
}
2018-12-31 07:58:13 +01:00
$rules = [
'token' => 'required',
'email' => 'required|email',
'password' => 'required|confirmed|min:16|secure_password',
2018-12-31 07:58:13 +01:00
];
$this->validate($request, $rules, $this->validationErrorMessages());
2018-10-13 15:06:56 +02:00
// Here we will attempt to reset the user's password. If it is successful we
// will update the password on an actual user model and persist it to the
// database. Otherwise we will parse the error and return the response.
$response = $this->broker()->reset(
$this->credentials($request),
function ($user, $password) {
$this->resetPassword($user, $password);
}
2018-10-13 15:06:56 +02:00
);
// 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
? $this->sendResetResponse($request, $response)
: $this->sendResetFailedResponse($request, $response);
}
/**
2018-12-31 07:58:13 +01:00
* Display the password reset view for the given token.
*
* If no token is present, display the link request form.
*
* @param Request $request
* @param string|null $token
*
* @return Factory|View
*/
2018-12-31 07:58:13 +01:00
public function showResetForm(Request $request, $token = null)
{
2018-12-31 07:58:13 +01:00
$loginProvider = config('firefly.login_provider');
if ('eloquent' !== $loginProvider) {
$message = sprintf('Cannot reset password when authenticating over "%s".', $loginProvider);
return prefixView('error', compact('message'));
2018-12-31 07:58:13 +01:00
}
// is allowed to register?
2019-02-13 17:38:41 +01:00
$singleUserMode = app('fireflyconfig')->get('single_user_mode', config('firefly.configuration.single_user_mode'))->data;
2018-12-31 07:58:13 +01:00
$userCount = User::count();
$allowRegistration = true;
$pageTitle = (string) trans('firefly.reset_pw_page_title');
2018-12-31 07:58:13 +01:00
if (true === $singleUserMode && $userCount > 0) {
$allowRegistration = false;
}
/** @noinspection PhpUndefinedFieldInspection */
return prefixView('auth.passwords.reset')->with(
2018-12-31 07:58:13 +01:00
['token' => $token, 'email' => $request->email, 'allowRegistration' => $allowRegistration, 'pageTitle' => $pageTitle]
);
}
2016-09-16 06:19:40 +02:00
}