Passed
Push — master ( 502cab...a00bcc )
by Greg
05:16
created

PasswordResetAction::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
 */
16
declare(strict_types=1);
17
18
namespace Fisharebest\Webtrees\Http\RequestHandlers;
19
20
use Fig\Http\Message\StatusCodeInterface;
21
use Fisharebest\Webtrees\Auth;
22
use Fisharebest\Webtrees\FlashMessages;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Log;
25
use Fisharebest\Webtrees\Services\UserService;
26
use Fisharebest\Webtrees\User;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
31
/**
32
 * Set a new password.
33
 */
34
class 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