xref: /webtrees/app/Http/RequestHandlers/PasswordRequestAction.php (revision 99125a335d0be11b975e213c69fc4ede0c5303f1)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
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\EmailService;
28use Fisharebest\Webtrees\Services\UserService;
29use Fisharebest\Webtrees\SiteUser;
30use Fisharebest\Webtrees\Tree;
31use Fisharebest\Webtrees\User;
32use Illuminate\Support\Str;
33use Psr\Http\Message\ResponseInterface;
34use Psr\Http\Message\ServerRequestInterface;
35use Psr\Http\Server\RequestHandlerInterface;
36
37use function e;
38use function redirect;
39use function route;
40use function view;
41
42/**
43 * Request a new password.
44 */
45class PasswordRequestAction implements RequestHandlerInterface, StatusCodeInterface
46{
47    private const TOKEN_LENGTH = 40;
48
49    private EmailService $email_service;
50
51    private UserService $user_service;
52
53    /**
54     * PasswordRequestForm constructor.
55     *
56     * @param EmailService $email_service
57     * @param UserService  $user_service
58     */
59    public function __construct(EmailService $email_service, UserService $user_service)
60    {
61        $this->user_service  = $user_service;
62        $this->email_service = $email_service;
63    }
64
65    /**
66     * @param ServerRequestInterface $request
67     *
68     * @return ResponseInterface
69     */
70    public function handle(ServerRequestInterface $request): ResponseInterface
71    {
72        $tree = $request->getAttribute('tree');
73
74        $params = (array) $request->getParsedBody();
75
76        $email = $params['email'] ?? '';
77        $user  = $this->user_service->findByEmail($email);
78
79        if ($user instanceof User) {
80            $token  = Str::random(self::TOKEN_LENGTH);
81            $expire = (string) Carbon::now()->addHour()->getTimestamp();
82            $url    = route(PasswordResetPage::class, [
83                'token' => $token,
84                'tree'  => $tree instanceof Tree ? $tree->name() : null,
85            ]);
86
87            $user->setPreference('password-token', $token);
88            $user->setPreference('password-token-expire', $expire);
89
90            $this->email_service->send(
91                new SiteUser(),
92                $user,
93                new SiteUser(),
94                I18N::translate('Request a new password'),
95                view('emails/password-request-text', ['url' => $url, 'user' => $user]),
96                view('emails/password-request-html', ['url' => $url, 'user' => $user])
97            );
98
99            Log::addAuthenticationLog('Password request for user: ' . $user->userName());
100
101            $message1 = I18N::translate('A password reset link has been sent to “%s”.', e($email));
102            $message2 = I18N::translate('This link is valid for one hour.');
103            FlashMessages::addMessage($message1 . '<br>' . $message2, 'success');
104        } else {
105            $message = I18N::translate('There is no user account with the email “%s”.', e($email));
106            FlashMessages::addMessage($message, 'danger');
107        }
108
109        return redirect(route(LoginPage::class, ['tree' => $tree instanceof Tree ? $tree->name() : null]));
110    }
111}
112