xref: /webtrees/app/Http/RequestHandlers/UserAddAction.php (revision b6ec1ccf23d5adc5a335304551953cfb6f52eef2)
14c3563c0SGreg Roach<?php
24c3563c0SGreg Roach
34c3563c0SGreg Roach/**
44c3563c0SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
64c3563c0SGreg Roach * This program is free software: you can redistribute it and/or modify
74c3563c0SGreg Roach * it under the terms of the GNU General Public License as published by
84c3563c0SGreg Roach * the Free Software Foundation, either version 3 of the License, or
94c3563c0SGreg Roach * (at your option) any later version.
104c3563c0SGreg Roach * This program is distributed in the hope that it will be useful,
114c3563c0SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
124c3563c0SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
134c3563c0SGreg Roach * GNU General Public License for more details.
144c3563c0SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
164c3563c0SGreg Roach */
174c3563c0SGreg Roach
184c3563c0SGreg Roachdeclare(strict_types=1);
194c3563c0SGreg Roach
204c3563c0SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
214c3563c0SGreg Roach
224c3563c0SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
234c3563c0SGreg Roachuse Fisharebest\Webtrees\FlashMessages;
244c3563c0SGreg Roachuse Fisharebest\Webtrees\I18N;
254c3563c0SGreg Roachuse Fisharebest\Webtrees\Log;
264c3563c0SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
274c3563c0SGreg Roachuse Fisharebest\Webtrees\Site;
28*b6ec1ccfSGreg Roachuse Fisharebest\Webtrees\User;
29748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
304c3563c0SGreg Roachuse Psr\Http\Message\ResponseInterface;
314c3563c0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
324c3563c0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
334c3563c0SGreg Roach
344c3563c0SGreg Roachuse function route;
354c3563c0SGreg Roach
364c3563c0SGreg Roach/**
374c3563c0SGreg Roach * Add a user.
384c3563c0SGreg Roach */
394c3563c0SGreg Roachclass UserAddAction implements RequestHandlerInterface
404c3563c0SGreg Roach{
41c4943cffSGreg Roach    private UserService $user_service;
424c3563c0SGreg Roach
434c3563c0SGreg Roach    /**
444c3563c0SGreg Roach     * @param UserService $user_service
454c3563c0SGreg Roach     */
464c3563c0SGreg Roach    public function __construct(UserService $user_service)
474c3563c0SGreg Roach    {
484c3563c0SGreg Roach        $this->user_service = $user_service;
494c3563c0SGreg Roach    }
504c3563c0SGreg Roach
514c3563c0SGreg Roach    /**
524c3563c0SGreg Roach     * @param ServerRequestInterface $request
534c3563c0SGreg Roach     *
544c3563c0SGreg Roach     * @return ResponseInterface
554c3563c0SGreg Roach     */
564c3563c0SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
574c3563c0SGreg Roach    {
58748dbe15SGreg Roach        $username  = Validator::parsedBody($request)->string('username');
59748dbe15SGreg Roach        $real_name = Validator::parsedBody($request)->string('real_name');
60748dbe15SGreg Roach        $email     = Validator::parsedBody($request)->string('email');
61748dbe15SGreg Roach        $password  = Validator::parsedBody($request)->string('password');
624c3563c0SGreg Roach
634c3563c0SGreg Roach        $errors = false;
64*b6ec1ccfSGreg Roach
65*b6ec1ccfSGreg Roach        if ($this->user_service->findByUserName($username) instanceof User) {
664c3563c0SGreg Roach            FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'));
674c3563c0SGreg Roach            $errors = true;
684c3563c0SGreg Roach        }
694c3563c0SGreg Roach
70*b6ec1ccfSGreg Roach        if ($this->user_service->findByEmail($email) instanceof User) {
714c3563c0SGreg Roach            FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.'));
724c3563c0SGreg Roach            $errors = true;
734c3563c0SGreg Roach        }
744c3563c0SGreg Roach
754c3563c0SGreg Roach        if ($errors) {
764c3563c0SGreg Roach            $url = route(UserAddPage::class, [
774c3563c0SGreg Roach                'email'     => $email,
784c3563c0SGreg Roach                'real_name' => $real_name,
794c3563c0SGreg Roach                'username'  => $username,
804c3563c0SGreg Roach            ]);
814c3563c0SGreg Roach
824c3563c0SGreg Roach            return redirect($url);
834c3563c0SGreg Roach        }
844c3563c0SGreg Roach
854c3563c0SGreg Roach        $new_user = $this->user_service->create($username, $real_name, $email, $password);
864c3563c0SGreg Roach        $new_user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1');
874fc92d7dSGreg Roach        $new_user->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, '1');
884c3563c0SGreg Roach        $new_user->setPreference(UserInterface::PREF_LANGUAGE, I18N::languageTag());
898a07c98eSGreg Roach        $new_user->setPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE'));
904c3563c0SGreg Roach        $new_user->setPreference(UserInterface::PREF_TIMESTAMP_REGISTERED, date('U'));
914c3563c0SGreg Roach        $new_user->setPreference(UserInterface::PREF_TIMESTAMP_ACTIVE, '0');
924c3563c0SGreg Roach
934c3563c0SGreg Roach        Log::addAuthenticationLog('User ->' . $username . '<- created');
944c3563c0SGreg Roach
954c3563c0SGreg Roach        $url = route(UserEditPage::class, [
964c3563c0SGreg Roach            'user_id' => $new_user->id(),
974c3563c0SGreg Roach        ]);
984c3563c0SGreg Roach
994c3563c0SGreg Roach        return redirect($url);
1004c3563c0SGreg Roach    }
1014c3563c0SGreg Roach}
102