1a00bcc63SGreg Roach<?php 2a00bcc63SGreg Roach/** 3a00bcc63SGreg Roach * webtrees: online genealogy 4a00bcc63SGreg Roach * Copyright (C) 2019 webtrees development team 5a00bcc63SGreg Roach * This program is free software: you can redistribute it and/or modify 6a00bcc63SGreg Roach * it under the terms of the GNU General Public License as published by 7a00bcc63SGreg Roach * the Free Software Foundation, either version 3 of the License, or 8a00bcc63SGreg Roach * (at your option) any later version. 9a00bcc63SGreg Roach * This program is distributed in the hope that it will be useful, 10a00bcc63SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 11a00bcc63SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12a00bcc63SGreg Roach * GNU General Public License for more details. 13a00bcc63SGreg Roach * You should have received a copy of the GNU General Public License 14a00bcc63SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 15a00bcc63SGreg Roach */ 16a00bcc63SGreg Roachdeclare(strict_types=1); 17a00bcc63SGreg Roach 18a00bcc63SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 19a00bcc63SGreg Roach 20a00bcc63SGreg Roachuse Fig\Http\Message\RequestMethodInterface; 21a00bcc63SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 22a00bcc63SGreg Roachuse Fisharebest\Webtrees\Carbon; 23a00bcc63SGreg Roachuse Fisharebest\Webtrees\FlashMessages; 24a00bcc63SGreg Roachuse Fisharebest\Webtrees\I18N; 25a00bcc63SGreg Roachuse Fisharebest\Webtrees\Log; 26a00bcc63SGreg Roachuse Fisharebest\Webtrees\Services\MailService; 27a00bcc63SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 28a00bcc63SGreg Roachuse Fisharebest\Webtrees\SiteUser; 29a00bcc63SGreg Roachuse Fisharebest\Webtrees\User; 30a00bcc63SGreg Roachuse Illuminate\Support\Str; 31a00bcc63SGreg Roachuse Psr\Http\Message\ResponseInterface; 32a00bcc63SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 33a00bcc63SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 34a00bcc63SGreg Roachuse function e; 35a00bcc63SGreg Roachuse function redirect; 36a00bcc63SGreg Roachuse function route; 37a00bcc63SGreg Roachuse function view; 38a00bcc63SGreg Roach 39a00bcc63SGreg Roach/** 40a00bcc63SGreg Roach * Request a new password. 41a00bcc63SGreg Roach */ 42a00bcc63SGreg Roachclass PasswordRequestAction implements RequestHandlerInterface, StatusCodeInterface, RequestMethodInterface 43a00bcc63SGreg Roach{ 44*57ab2231SGreg Roach private const TOKEN_LENGTH = 40; 45a00bcc63SGreg Roach 46a00bcc63SGreg Roach /** @var MailService */ 47a00bcc63SGreg Roach private $mail_service; 48a00bcc63SGreg Roach 49a00bcc63SGreg Roach /** @var UserService */ 50a00bcc63SGreg Roach private $user_service; 51a00bcc63SGreg Roach 52a00bcc63SGreg Roach /** 53a00bcc63SGreg Roach * PasswordRequestForm constructor. 54a00bcc63SGreg Roach * 55a00bcc63SGreg Roach * @param MailService $mail_service 56a00bcc63SGreg Roach * @param UserService $user_service 57a00bcc63SGreg Roach */ 58a00bcc63SGreg Roach public function __construct(MailService $mail_service, UserService $user_service) 59a00bcc63SGreg Roach { 60a00bcc63SGreg Roach $this->user_service = $user_service; 61a00bcc63SGreg Roach $this->mail_service = $mail_service; 62a00bcc63SGreg Roach } 63a00bcc63SGreg Roach 64a00bcc63SGreg Roach /** 65a00bcc63SGreg Roach * @param ServerRequestInterface $request 66a00bcc63SGreg Roach * 67a00bcc63SGreg Roach * @return ResponseInterface 68a00bcc63SGreg Roach */ 69a00bcc63SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 70a00bcc63SGreg Roach { 71a00bcc63SGreg Roach $email = $request->getParsedBody()['email'] ?? ''; 72a00bcc63SGreg Roach $user = $this->user_service->findByEmail($email); 73a00bcc63SGreg Roach 74a00bcc63SGreg Roach if ($user instanceof User) { 75a00bcc63SGreg Roach $token = Str::random(self::TOKEN_LENGTH); 76a00bcc63SGreg Roach $expire = (string) Carbon::now()->addHour()->timestamp; 77a00bcc63SGreg Roach $url = route('password-reset', ['token' => $token]); 78a00bcc63SGreg Roach 79a00bcc63SGreg Roach $user->setPreference('password-token', $token); 80a00bcc63SGreg Roach $user->setPreference('password-token-expire', $expire); 81a00bcc63SGreg Roach 82a00bcc63SGreg Roach $this->mail_service->send( 83a00bcc63SGreg Roach new SiteUser(), 84a00bcc63SGreg Roach $user, 85a00bcc63SGreg Roach new SiteUser(), 86a00bcc63SGreg Roach I18N::translate('Request a new password'), 87a00bcc63SGreg Roach view('emails/password-request-text', ['url' => $url, 'user' => $user]), 88a00bcc63SGreg Roach view('emails/password-request-html', ['url' => $url, 'user' => $user]) 89a00bcc63SGreg Roach ); 90a00bcc63SGreg Roach 91a00bcc63SGreg Roach Log::addAuthenticationLog('Password request for user: ' . $user->userName()); 92a00bcc63SGreg Roach 93a00bcc63SGreg Roach $message1 = I18N::translate('A password reset link has been sent to “%s”.', e($email)); 94a00bcc63SGreg Roach $message2 = I18N::translate('This link is valid for one hour.'); 95a00bcc63SGreg Roach FlashMessages::addMessage($message1 . '<br>' . $message2, 'success'); 96a00bcc63SGreg Roach } else { 97a00bcc63SGreg Roach $message = I18N::translate('There is no user account with the email “%s”.', e($email)); 98a00bcc63SGreg Roach FlashMessages::addMessage($message, 'danger'); 99a00bcc63SGreg Roach } 100a00bcc63SGreg Roach 101a00bcc63SGreg Roach return redirect(route('password-request')); 102a00bcc63SGreg Roach } 103a00bcc63SGreg Roach} 104