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

123 lines
4.1 KiB
PHP
Raw Normal View History

2016-09-16 06:19:40 +02:00
<?php
2017-10-21 08:40:00 +02:00
/**
* ForgotPasswordController.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 14:41:58 +01:00
* along with Firefly III. If not, see <http://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;
use FireflyIII\Http\Controllers\Controller;
2018-01-05 07:54:10 +01:00
use FireflyIII\Repositories\User\UserRepositoryInterface;
use FireflyIII\User;
2016-09-16 06:19:40 +02:00
use Illuminate\Foundation\Auth\SendsPasswordResetEmails;
2018-01-05 07:54:10 +01:00
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Password;
use Log;
2016-09-16 06:19:40 +02:00
2017-12-17 14:30:53 +01:00
/**
* Class ForgotPasswordController
*/
2016-09-16 06:19:40 +02:00
class ForgotPasswordController extends Controller
{
use SendsPasswordResetEmails;
/**
2018-01-05 07:54:10 +01:00
* Create a new controller instance.
*/
public function __construct()
{
parent::__construct();
$this->middleware('guest');
}
/**
* Send a reset link to the given user.
*
* @param \Illuminate\Http\Request $request
*
* @param UserRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse
*/
public function sendResetLinkEmail(Request $request, UserRepositoryInterface $repository)
{
Log::info('Start of sendResetLinkEmail()');
$loginProvider = config('firefly.login_provider');
2018-12-18 07:07:51 +01:00
// @codeCoverageIgnoreStart
2018-10-13 15:06:56 +02:00
if ('eloquent' !== $loginProvider) {
$message = sprintf('Cannot reset password when authenticating over "%s".', $loginProvider);
Log::error($message);
2019-02-13 17:38:41 +01:00
2018-10-13 15:06:56 +02:00
return view('error', compact('message'));
}
2018-12-18 07:07:51 +01:00
// @codeCoverageIgnoreEnd
2018-10-13 15:06:56 +02:00
2018-01-05 07:54:10 +01:00
$this->validateEmail($request);
// 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();
2018-04-02 15:10:40 +02:00
if (null !== $user && $repository->hasRole($user, 'demo')) {
2018-07-15 09:38:49 +02:00
return back()->withErrors(['email' => (string)trans('firefly.cannot_reset_demo_user')]);
2018-01-05 07:54:10 +01:00
}
// We will send the password reset link to this user. Once we have attempted
// to send the link, we will examine the response then see the message we
// need to show to the user. Finally, we'll send out a proper response.
$response = $this->broker()->sendResetLink(
$request->only('email')
);
2018-07-09 19:24:08 +02:00
if ($response === Password::RESET_LINK_SENT) {
2018-01-05 07:54:10 +01:00
return back()->with('status', trans($response));
}
return back()->withErrors(['email' => trans($response)]); // @codeCoverageIgnore
}
/**
2018-07-21 08:06:24 +02:00
* Show form for email recovery.
*
2018-01-05 07:54:10 +01:00
* @codeCoverageIgnore
*
2018-07-08 12:28:42 +02:00
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function showLinkRequestForm()
{
$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 view('error', compact('message'));
}
// is allowed to?
2019-02-13 17:38:41 +01:00
$singleUserMode = app('fireflyconfig')->get('single_user_mode', config('firefly.configuration.single_user_mode'))->data;
$userCount = User::count();
$allowRegistration = true;
$pageTitle = (string)trans('firefly.forgot_pw_page_title');
if (true === $singleUserMode && $userCount > 0) {
$allowRegistration = false;
}
return view('auth.passwords.email')->with(compact('allowRegistration', 'pageTitle'));
}
2016-09-16 06:19:40 +02:00
}