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