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