xref: /webtrees/app/Http/RequestHandlers/PasswordResetAction.php (revision 0b4092ed8e31e3588eb143d0ade6c4a411367da8)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2019 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16declare(strict_types=1);
17
18namespace Fisharebest\Webtrees\Http\RequestHandlers;
19
20use Fig\Http\Message\StatusCodeInterface;
21use Fisharebest\Webtrees\Auth;
22use Fisharebest\Webtrees\FlashMessages;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Log;
25use Fisharebest\Webtrees\Services\UserService;
26use Fisharebest\Webtrees\User;
27use Psr\Http\Message\ResponseInterface;
28use Psr\Http\Message\ServerRequestInterface;
29use Psr\Http\Server\RequestHandlerInterface;
30
31/**
32 * Set a new password.
33 */
34class PasswordResetAction implements RequestHandlerInterface, StatusCodeInterface
35{
36    /** @var UserService */
37    private $user_service;
38
39    /**
40     * PasswordRequestForm constructor.
41     *
42     * @param UserService $user_service
43     */
44    public function __construct(UserService $user_service)
45    {
46        $this->user_service = $user_service;
47    }
48
49    /**
50     * @param ServerRequestInterface $request
51     *
52     * @return ResponseInterface
53     */
54    public function handle(ServerRequestInterface $request): ResponseInterface
55    {
56        $token = $request->getParsedBody()['token'] ?? '';
57        $user  = $this->user_service->findByToken($token);
58
59        if ($user instanceof User) {
60            $password = $request->getParsedBody()['password'] ?? '';
61
62            $user
63                ->setPreference('password-token', '')
64                ->setPreference('password-token-expire', '')
65                ->setPassword($password);
66
67            Auth::login($user);
68
69            Log::addAuthenticationLog('Password reset for user: ' . $user->userName());
70
71            $message = I18N::translate('Your password has been updated.');
72
73            FlashMessages::addMessage($message, 'success');
74
75            return redirect(route('user-page'));
76        }
77
78        $message1 = I18N::translate('The password reset link has expired.');
79        $message2 = I18N::translate('Please try again.');
80        $message  = $message1 . '<br>' . $message2;
81
82        FlashMessages::addMessage($message, 'danger');
83
84        return redirect(route('password-request'));
85    }
86}
87