Passed
Push — master ( 26e1ac...4c3563 )
by Greg
16:10 queued 10:48
created

UserAddAction::handle()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 44
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 28
c 1
b 0
f 0
nc 8
nop 1
dl 0
loc 44
rs 9.472
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\Contracts\UserInterface;
23
use Fisharebest\Webtrees\FlashMessages;
24
use Fisharebest\Webtrees\I18N;
25
use Fisharebest\Webtrees\Log;
26
use Fisharebest\Webtrees\Services\UserService;
27
use Fisharebest\Webtrees\Site;
28
use Psr\Http\Message\ResponseInterface;
29
use Psr\Http\Message\ServerRequestInterface;
30
use Psr\Http\Server\RequestHandlerInterface;
31
32
use function route;
33
34
/**
35
 * Add a user.
36
 */
37
class UserAddAction implements RequestHandlerInterface
38
{
39
    /** @var UserService */
40
    private $user_service;
41
42
    /**
43
     * UserAddAction constructor.
44
     *
45
     * @param UserService $user_service
46
     */
47
    public function __construct(UserService $user_service)
48
    {
49
        $this->user_service = $user_service;
50
    }
51
52
    /**
53
     * @param ServerRequestInterface $request
54
     *
55
     * @return ResponseInterface
56
     */
57
    public function handle(ServerRequestInterface $request): ResponseInterface
58
    {
59
        $params = (array) $request->getParsedBody();
60
61
        $username  = $params['username'] ?? '';
62
        $real_name = $params['real_name'] ?? '';
63
        $email     = $params['email'] ?? '';
64
        $password  = $params['password'] ?? '';
65
66
        $errors = false;
67
        if ($this->user_service->findByUserName($username)) {
68
            FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'));
69
            $errors = true;
70
        }
71
72
        if ($this->user_service->findByEmail($email)) {
73
            FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.'));
74
            $errors = true;
75
        }
76
77
        if ($errors) {
78
            $url = route(UserAddPage::class, [
79
                'email'     => $email,
80
                'real_name' => $real_name,
81
                'username'  => $username,
82
            ]);
83
84
            return redirect($url);
85
        }
86
87
        $new_user = $this->user_service->create($username, $real_name, $email, $password);
88
        $new_user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1');
89
        $new_user->setPreference(UserInterface::PREF_LANGUAGE, I18N::languageTag());
90
        $new_user->setPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE', 'UTC'));
91
        $new_user->setPreference(UserInterface::PREF_TIMESTAMP_REGISTERED, date('U'));
92
        $new_user->setPreference(UserInterface::PREF_TIMESTAMP_ACTIVE, '0');
93
94
        Log::addAuthenticationLog('User ->' . $username . '<- created');
95
96
        $url = route(UserEditPage::class, [
97
            'user_id' => $new_user->id(),
98
        ]);
99
100
        return redirect($url);
101
    }
102
}
103