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