1 | <?php |
||
2 | /** |
||
3 | * ResetPasswordController.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\User; |
||
29 | use Illuminate\Foundation\Auth\ResetsPasswords; |
||
30 | use Illuminate\Http\Request; |
||
31 | use Illuminate\Support\Facades\Password; |
||
32 | |||
33 | /** |
||
34 | * Class ResetPasswordController |
||
35 | * |
||
36 | * This controller is responsible for handling password reset requests |
||
37 | * and uses a simple trait to include this behavior. You're free to |
||
38 | * explore this trait and override any methods you wish to tweak. |
||
39 | * |
||
40 | * @codeCoverageIgnore |
||
41 | */ |
||
42 | class ResetPasswordController extends Controller |
||
43 | { |
||
44 | use ResetsPasswords; |
||
45 | |||
46 | /** |
||
47 | * Where to redirect users after resetting their password. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $redirectTo = '/home'; |
||
52 | |||
53 | /** |
||
54 | * Create a new controller instance. |
||
55 | */ |
||
56 | public function __construct() |
||
57 | { |
||
58 | parent::__construct(); |
||
59 | $this->middleware('guest'); |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * Reset the given user's password. |
||
64 | * |
||
65 | * @param \Illuminate\Http\Request $request |
||
66 | * |
||
67 | * @return \Illuminate\Http\RedirectResponse|\Illuminate\Http\JsonResponse |
||
68 | * @throws \Illuminate\Validation\ValidationException |
||
69 | */ |
||
70 | public function reset(Request $request) |
||
71 | { |
||
72 | $loginProvider = config('firefly.login_provider'); |
||
73 | if ('eloquent' !== $loginProvider) { |
||
74 | $message = sprintf('Cannot reset password when authenticating over "%s".', $loginProvider); |
||
75 | |||
76 | return view('error', compact('message')); |
||
77 | } |
||
78 | |||
79 | $rules = [ |
||
80 | 'token' => 'required', |
||
81 | 'email' => 'required|email', |
||
82 | 'password' => 'required|confirmed|min:6|secure_password', |
||
83 | ]; |
||
84 | |||
85 | $this->validate($request, $rules, $this->validationErrorMessages()); |
||
86 | |||
87 | // Here we will attempt to reset the user's password. If it is successful we |
||
88 | // will update the password on an actual user model and persist it to the |
||
89 | // database. Otherwise we will parse the error and return the response. |
||
90 | $response = $this->broker()->reset( |
||
91 | $this->credentials($request), function ($user, $password) { |
||
92 | $this->resetPassword($user, $password); |
||
93 | } |
||
94 | ); |
||
95 | |||
96 | // If the password was successfully reset, we will redirect the user back to |
||
97 | // the application's home authenticated view. If there is an error we can |
||
98 | // redirect them back to where they came from with their error message. |
||
99 | return $response === Password::PASSWORD_RESET |
||
100 | ? $this->sendResetResponse($request, $response) |
||
101 | : $this->sendResetFailedResponse($request, $response); |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * Display the password reset view for the given token. |
||
106 | * |
||
107 | * If no token is present, display the link request form. |
||
108 | * |
||
109 | * @param Request $request |
||
110 | * @param string|null $token |
||
111 | * |
||
112 | * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View |
||
113 | */ |
||
114 | public function showResetForm(Request $request, $token = null) |
||
115 | { |
||
116 | $loginProvider = config('firefly.login_provider'); |
||
117 | if ('eloquent' !== $loginProvider) { |
||
118 | $message = sprintf('Cannot reset password when authenticating over "%s".', $loginProvider); |
||
119 | |||
120 | return view('error', compact('message')); |
||
121 | } |
||
122 | |||
123 | // is allowed to register? |
||
124 | $singleUserMode = FireflyConfig::get('single_user_mode', config('firefly.configuration.single_user_mode'))->data; |
||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
125 | $userCount = User::count(); |
||
126 | $allowRegistration = true; |
||
127 | $pageTitle = (string)trans('firefly.reset_pw_page_title'); |
||
128 | if (true === $singleUserMode && $userCount > 0) { |
||
129 | $allowRegistration = false; |
||
130 | } |
||
131 | |||
132 | /** @noinspection PhpUndefinedFieldInspection */ |
||
133 | return view('auth.passwords.reset')->with( |
||
134 | ['token' => $token, 'email' => $request->email, 'allowRegistration' => $allowRegistration, 'pageTitle' => $pageTitle] |
||
135 | ); |
||
136 | } |
||
137 | } |
||
138 |