Passed
Push ā€” master ( 213bcd...0f9753 )
by Greg
07:23
created

ForgotPasswordController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Controllers\Auth;
19
20
use Fisharebest\Webtrees\FlashMessages;
21
use Fisharebest\Webtrees\Http\Controllers\AbstractBaseController;
22
use Fisharebest\Webtrees\I18N;
23
use Fisharebest\Webtrees\Log;
24
use Fisharebest\Webtrees\Services\MailService;
25
use Fisharebest\Webtrees\Services\UserService;
26
use Fisharebest\Webtrees\Tree;
27
use Fisharebest\Webtrees\TreeUser;
28
use Fisharebest\Webtrees\User;
29
use Illuminate\Support\Str;
30
use Psr\Http\Message\ResponseInterface;
31
use Psr\Http\Message\ServerRequestInterface;
32
33
/**
34
 * Controller for requesting password resets.
35
 */
36
class ForgotPasswordController extends AbstractBaseController
37
{
38
    /**
39
     * @var MailService
40
     */
41
    private $mail_service;
42
43
    /**
44
     * MessageController constructor.
45
     *
46
     * @param MailService $mail_service
47
     */
48
    public function __construct(MailService $mail_service)
49
    {
50
        $this->mail_service = $mail_service;
51
    }
52
53
    /**
54
     * Show a password reset page.
55
     *
56
     * @return ResponseInterface
57
     */
58
    public function forgotPasswordPage(): ResponseInterface
59
    {
60
        $title = I18N::translate('Request a new password');
61
62
        return $this->viewResponse('forgot-password-page', [
63
            'title' => $title,
64
        ]);
65
    }
66
67
    /**
68
     * Send a password reset email.
69
     *
70
     * @param ServerRequestInterface $request
71
     * @param Tree                   $tree
72
     * @param UserService            $user_service
73
     *
74
     * @return ResponseInterface
75
     */
76
    public function forgotPasswordAction(ServerRequestInterface $request, Tree $tree, UserService $user_service): ResponseInterface
77
    {
78
        $identifier = $request->getParsedBody()['identifier'] ?? '';
79
80
        $user = $user_service->findByIdentifier($identifier);
81
82
        if ($user instanceof User) {
83
            $password = $this->createNewPassword();
84
            $user->setPassword($password);
85
86
            Log::addAuthenticationLog('Password request was sent to user: ' . $user->userName());
87
88
            $this->mail_service->send(
89
                new TreeUser($tree),
90
                $user,
91
                new TreeUser($tree),
92
                I18N::translate('Lost password request'),
93
                view('emails/password-reset-text', [
94
                    'user'         => $user,
95
                    'new_password' => $password,
96
                ]),
97
                view('emails/password-reset-html', [
98
                    'user'         => $user,
99
                    'new_password' => $password,
100
                ])
101
            );
102
103
            FlashMessages::addMessage(I18N::translate('A new password has been created and emailed to %s. You can change this password after you sign in.', e($identifier)), 'success');
104
105
            return redirect(route('login', ['username' => $user->userName()]));
106
        }
107
108
        FlashMessages::addMessage(I18N::translate('There is no account with the username or email ā€œ%sā€.', e($identifier)), 'danger');
109
110
        return redirect(route('forgot-password'));
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    private function createNewPassword(): string
117
    {
118
        return Str::random(8);
119
    }
120
}
121