xref: /webtrees/app/Http/RequestHandlers/UserAddAction.php (revision 9e3c2cf9009c4c2140f50ddfc80b91f2f82cdce5)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Contracts\UserInterface;
23use Fisharebest\Webtrees\FlashMessages;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Log;
26use Fisharebest\Webtrees\Services\UserService;
27use Fisharebest\Webtrees\Site;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function route;
33
34/**
35 * Add a user.
36 */
37class UserAddAction implements RequestHandlerInterface
38{
39    private UserService $user_service;
40
41    /**
42     * UserAddAction constructor.
43     *
44     * @param UserService $user_service
45     */
46    public function __construct(UserService $user_service)
47    {
48        $this->user_service = $user_service;
49    }
50
51    /**
52     * @param ServerRequestInterface $request
53     *
54     * @return ResponseInterface
55     */
56    public function handle(ServerRequestInterface $request): ResponseInterface
57    {
58        $params = (array) $request->getParsedBody();
59
60        $username  = $params['username'] ?? '';
61        $real_name = $params['real_name'] ?? '';
62        $email     = $params['email'] ?? '';
63        $password  = $params['password'] ?? '';
64
65        $errors = false;
66        if ($this->user_service->findByUserName($username)) {
67            FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'));
68            $errors = true;
69        }
70
71        if ($this->user_service->findByEmail($email)) {
72            FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.'));
73            $errors = true;
74        }
75
76        if ($errors) {
77            $url = route(UserAddPage::class, [
78                'email'     => $email,
79                'real_name' => $real_name,
80                'username'  => $username,
81            ]);
82
83            return redirect($url);
84        }
85
86        $new_user = $this->user_service->create($username, $real_name, $email, $password);
87        $new_user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1');
88        $new_user->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '1');
89        $new_user->setPreference(UserInterface::PREF_LANGUAGE, I18N::languageTag());
90        $new_user->setPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE'));
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