xref: /webtrees/app/Http/RequestHandlers/PasswordRequestAction.php (revision 1a218474113038005e50986fff24ebcbd58554ff)
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\StatusCodeInterface;
22use Fisharebest\Webtrees\Carbon;
23use Fisharebest\Webtrees\FlashMessages;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Log;
26use Fisharebest\Webtrees\Services\MailService;
27use Fisharebest\Webtrees\Services\UserService;
28use Fisharebest\Webtrees\SiteUser;
29use Fisharebest\Webtrees\User;
30use Illuminate\Support\Str;
31use Psr\Http\Message\ResponseInterface;
32use Psr\Http\Message\ServerRequestInterface;
33use Psr\Http\Server\RequestHandlerInterface;
34
35use function e;
36use function redirect;
37use function route;
38use function view;
39
40/**
41 * Request a new password.
42 */
43class PasswordRequestAction implements RequestHandlerInterface, StatusCodeInterface
44{
45    private const TOKEN_LENGTH = 40;
46
47    /** @var MailService */
48    private $mail_service;
49
50    /** @var UserService */
51    private $user_service;
52
53    /**
54     * PasswordRequestForm constructor.
55     *
56     * @param MailService $mail_service
57     * @param UserService $user_service
58     */
59    public function __construct(MailService $mail_service, UserService $user_service)
60    {
61        $this->user_service = $user_service;
62        $this->mail_service = $mail_service;
63    }
64
65    /**
66     * @param ServerRequestInterface $request
67     *
68     * @return ResponseInterface
69     */
70    public function handle(ServerRequestInterface $request): ResponseInterface
71    {
72        $email = $request->getParsedBody()['email'] ?? '';
73        $user  = $this->user_service->findByEmail($email);
74
75        if ($user instanceof User) {
76            $token  = Str::random(self::TOKEN_LENGTH);
77            $expire = (string) Carbon::now()->addHour()->timestamp;
78            $url    = route(PasswordResetPage::class, ['token' => $token]);
79
80            $user->setPreference('password-token', $token);
81            $user->setPreference('password-token-expire', $expire);
82
83            $this->mail_service->send(
84                new SiteUser(),
85                $user,
86                new SiteUser(),
87                I18N::translate('Request a new password'),
88                view('emails/password-request-text', ['url' => $url, 'user' => $user]),
89                view('emails/password-request-html', ['url' => $url, 'user' => $user])
90            );
91
92            Log::addAuthenticationLog('Password request for user: ' . $user->userName());
93
94            $message1 = I18N::translate('A password reset link has been sent to “%s”.', e($email));
95            $message2 = I18N::translate('This link is valid for one hour.');
96            FlashMessages::addMessage($message1 . '<br>' . $message2, 'success');
97        } else {
98            $message = I18N::translate('There is no user account with the email “%s”.', e($email));
99            FlashMessages::addMessage($message, 'danger');
100        }
101
102        return redirect(route(PasswordRequestPage::class));
103    }
104}
105