xref: /webtrees/app/Http/RequestHandlers/UserAddAction.php (revision 1da5dadd02c9f21e0a345e21a19b2c63ae792ed3)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 Fisharebest\Webtrees\Validator;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function route;
34
35/**
36 * Add a user.
37 */
38class UserAddAction implements RequestHandlerInterface
39{
40    private UserService $user_service;
41
42    /**
43     * @param UserService $user_service
44     */
45    public function __construct(UserService $user_service)
46    {
47        $this->user_service = $user_service;
48    }
49
50    /**
51     * @param ServerRequestInterface $request
52     *
53     * @return ResponseInterface
54     */
55    public function handle(ServerRequestInterface $request): ResponseInterface
56    {
57        $username  = Validator::parsedBody($request)->string('username');
58        $real_name = Validator::parsedBody($request)->string('real_name');
59        $email     = Validator::parsedBody($request)->string('email');
60        $password  = Validator::parsedBody($request)->string('password');
61
62        $errors = false;
63        if ($this->user_service->findByUserName($username)) {
64            FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'));
65            $errors = true;
66        }
67
68        if ($this->user_service->findByEmail($email)) {
69            FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.'));
70            $errors = true;
71        }
72
73        if ($errors) {
74            $url = route(UserAddPage::class, [
75                'email'     => $email,
76                'real_name' => $real_name,
77                'username'  => $username,
78            ]);
79
80            return redirect($url);
81        }
82
83        $new_user = $this->user_service->create($username, $real_name, $email, $password);
84        $new_user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1');
85        $new_user->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '1');
86        $new_user->setPreference(UserInterface::PREF_LANGUAGE, I18N::languageTag());
87        $new_user->setPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE'));
88        $new_user->setPreference(UserInterface::PREF_TIMESTAMP_REGISTERED, date('U'));
89        $new_user->setPreference(UserInterface::PREF_TIMESTAMP_ACTIVE, '0');
90
91        Log::addAuthenticationLog('User ->' . $username . '<- created');
92
93        $url = route(UserEditPage::class, [
94            'user_id' => $new_user->id(),
95        ]);
96
97        return redirect($url);
98    }
99}
100