1 | <?php |
||
2 | /** |
||
3 | * ForgotPasswordController.php |
||
4 | * Copyright (c) 2017 [email protected] |
||
5 | * |
||
6 | * This file is part of Firefly III. |
||
7 | * |
||
8 | * Firefly III is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU General Public License as published by |
||
10 | * the Free Software Foundation, either version 3 of the License, or |
||
11 | * (at your option) any later version. |
||
12 | * |
||
13 | * Firefly III is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU General Public License |
||
19 | * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
||
20 | */ |
||
21 | /** @noinspection PhpDynamicAsStaticMethodCallInspection */ |
||
22 | declare(strict_types=1); |
||
23 | |||
24 | namespace FireflyIII\Http\Controllers\Auth; |
||
25 | |||
26 | use FireflyConfig; |
||
27 | use FireflyIII\Http\Controllers\Controller; |
||
28 | use FireflyIII\Repositories\User\UserRepositoryInterface; |
||
29 | use FireflyIII\User; |
||
30 | use Illuminate\Foundation\Auth\SendsPasswordResetEmails; |
||
31 | use Illuminate\Http\Request; |
||
32 | use Illuminate\Support\Facades\Password; |
||
33 | use Log; |
||
34 | |||
35 | /** |
||
36 | * Class ForgotPasswordController |
||
37 | */ |
||
38 | class ForgotPasswordController extends Controller |
||
39 | { |
||
40 | use SendsPasswordResetEmails; |
||
41 | |||
42 | /** |
||
43 | * Create a new controller instance. |
||
44 | */ |
||
45 | public function __construct() |
||
46 | { |
||
47 | parent::__construct(); |
||
48 | $this->middleware('guest'); |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Send a reset link to the given user. |
||
53 | * |
||
54 | * @param \Illuminate\Http\Request $request |
||
55 | * |
||
56 | * @param UserRepositoryInterface $repository |
||
57 | * |
||
58 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse |
||
59 | */ |
||
60 | public function sendResetLinkEmail(Request $request, UserRepositoryInterface $repository) |
||
61 | { |
||
62 | Log::info('Start of sendResetLinkEmail()'); |
||
63 | $loginProvider = config('firefly.login_provider'); |
||
64 | // @codeCoverageIgnoreStart |
||
65 | if ('eloquent' !== $loginProvider) { |
||
66 | $message = sprintf('Cannot reset password when authenticating over "%s".', $loginProvider); |
||
67 | Log::error($message); |
||
68 | return view('error', compact('message')); |
||
69 | } |
||
70 | // @codeCoverageIgnoreEnd |
||
71 | |||
72 | $this->validateEmail($request); |
||
73 | |||
74 | // verify if the user is not a demo user. If so, we give him back an error. |
||
75 | $user = User::where('email', $request->get('email'))->first(); |
||
76 | |||
77 | if (null !== $user && $repository->hasRole($user, 'demo')) { |
||
78 | return back()->withErrors(['email' => (string)trans('firefly.cannot_reset_demo_user')]); |
||
79 | } |
||
80 | |||
81 | // We will send the password reset link to this user. Once we have attempted |
||
82 | // to send the link, we will examine the response then see the message we |
||
83 | // need to show to the user. Finally, we'll send out a proper response. |
||
84 | $response = $this->broker()->sendResetLink( |
||
85 | $request->only('email') |
||
86 | ); |
||
87 | |||
88 | if ($response === Password::RESET_LINK_SENT) { |
||
89 | return back()->with('status', trans($response)); |
||
90 | } |
||
91 | |||
92 | return back()->withErrors(['email' => trans($response)]); // @codeCoverageIgnore |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Show form for email recovery. |
||
97 | * |
||
98 | * @codeCoverageIgnore |
||
99 | * |
||
100 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
101 | */ |
||
102 | public function showLinkRequestForm() |
||
103 | { |
||
104 | $loginProvider = config('firefly.login_provider'); |
||
105 | if ('eloquent' !== $loginProvider) { |
||
106 | $message = sprintf('Cannot reset password when authenticating over "%s".', $loginProvider); |
||
107 | |||
108 | return view('error', compact('message')); |
||
109 | } |
||
110 | |||
111 | // is allowed to? |
||
112 | $singleUserMode = FireflyConfig::get('single_user_mode', config('firefly.configuration.single_user_mode'))->data; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
113 | $userCount = User::count(); |
||
114 | $allowRegistration = true; |
||
115 | $pageTitle = (string)trans('firefly.forgot_pw_page_title'); |
||
116 | if (true === $singleUserMode && $userCount > 0) { |
||
117 | $allowRegistration = false; |
||
118 | } |
||
119 | |||
120 | return view('auth.passwords.email')->with(compact('allowRegistration', 'pageTitle')); |
||
121 | } |
||
122 | } |
||
123 |