1a00bcc63SGreg Roach<?php 23976b470SGreg Roach 3a00bcc63SGreg Roach/** 4a00bcc63SGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6a00bcc63SGreg Roach * This program is free software: you can redistribute it and/or modify 7a00bcc63SGreg Roach * it under the terms of the GNU General Public License as published by 8a00bcc63SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a00bcc63SGreg Roach * (at your option) any later version. 10a00bcc63SGreg Roach * This program is distributed in the hope that it will be useful, 11a00bcc63SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a00bcc63SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a00bcc63SGreg Roach * GNU General Public License for more details. 14a00bcc63SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 16a00bcc63SGreg Roach */ 17fcfa147eSGreg Roach 18a00bcc63SGreg Roachdeclare(strict_types=1); 19a00bcc63SGreg Roach 20a00bcc63SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21a00bcc63SGreg Roach 22a00bcc63SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 23a00bcc63SGreg Roachuse Fisharebest\Webtrees\Auth; 24a00bcc63SGreg Roachuse Fisharebest\Webtrees\FlashMessages; 25a00bcc63SGreg Roachuse Fisharebest\Webtrees\I18N; 26a00bcc63SGreg Roachuse Fisharebest\Webtrees\Log; 27a00bcc63SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 28f917a287SGreg Roachuse Fisharebest\Webtrees\Tree; 29a00bcc63SGreg Roachuse Fisharebest\Webtrees\User; 30b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 31a00bcc63SGreg Roachuse Psr\Http\Message\ResponseInterface; 32a00bcc63SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 33a00bcc63SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 34a00bcc63SGreg Roach 35a00bcc63SGreg Roach/** 36a00bcc63SGreg Roach * Set a new password. 37a00bcc63SGreg Roach */ 38a00bcc63SGreg Roachclass PasswordResetAction implements RequestHandlerInterface, StatusCodeInterface 39a00bcc63SGreg Roach{ 40c4943cffSGreg Roach private UserService $user_service; 41a00bcc63SGreg Roach 42a00bcc63SGreg Roach /** 43a00bcc63SGreg Roach * @param UserService $user_service 44a00bcc63SGreg Roach */ 45a00bcc63SGreg Roach public function __construct(UserService $user_service) 46a00bcc63SGreg Roach { 47a00bcc63SGreg Roach $this->user_service = $user_service; 48a00bcc63SGreg Roach } 49a00bcc63SGreg Roach 50a00bcc63SGreg Roach /** 51a00bcc63SGreg Roach * @param ServerRequestInterface $request 52a00bcc63SGreg Roach * 53a00bcc63SGreg Roach * @return ResponseInterface 54a00bcc63SGreg Roach */ 55a00bcc63SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 56a00bcc63SGreg Roach { 57b55cbc6bSGreg Roach $tree = Validator::attributes($request)->treeOptional(); 58f917a287SGreg Roach $token = $request->getAttribute('token'); 59a00bcc63SGreg Roach $user = $this->user_service->findByToken($token); 60a00bcc63SGreg Roach 61a00bcc63SGreg Roach if ($user instanceof User) { 62748dbe15SGreg Roach $password = Validator::parsedBody($request)->string('password'); 63a00bcc63SGreg Roach 645c98992aSGreg Roach $user->setPreference('password-token', ''); 655c98992aSGreg Roach $user->setPreference('password-token-expire', ''); 665c98992aSGreg Roach $user->setPassword($password); 67a00bcc63SGreg Roach 68a00bcc63SGreg Roach Auth::login($user); 69a00bcc63SGreg Roach 70a00bcc63SGreg Roach Log::addAuthenticationLog('Password reset for user: ' . $user->userName()); 71a00bcc63SGreg Roach 72a00bcc63SGreg Roach $message = I18N::translate('Your password has been updated.'); 73a00bcc63SGreg Roach 74a00bcc63SGreg Roach FlashMessages::addMessage($message, 'success'); 75a00bcc63SGreg Roach 76f917a287SGreg Roach return redirect(route(HomePage::class)); 77a00bcc63SGreg Roach } 78a00bcc63SGreg Roach 79a00bcc63SGreg Roach $message1 = I18N::translate('The password reset link has expired.'); 80a00bcc63SGreg Roach $message2 = I18N::translate('Please try again.'); 81a00bcc63SGreg Roach $message = $message1 . '<br>' . $message2; 82a00bcc63SGreg Roach 83a00bcc63SGreg Roach FlashMessages::addMessage($message, 'danger'); 84a00bcc63SGreg Roach 8581bf3221SGreg Roach return redirect(route(PasswordRequestPage::class, ['tree' => $tree?->name()])); 86a00bcc63SGreg Roach } 87a00bcc63SGreg Roach} 88