xref: /webtrees/app/Http/RequestHandlers/UserAddAction.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
14c3563c0SGreg Roach<?php
24c3563c0SGreg Roach
34c3563c0SGreg Roach/**
44c3563c0SGreg Roach * webtrees: online genealogy
54c3563c0SGreg Roach * Copyright (C) 2021 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
15*89f7189bSGreg 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;
284c3563c0SGreg Roachuse Psr\Http\Message\ResponseInterface;
294c3563c0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
304c3563c0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
314c3563c0SGreg Roach
324c3563c0SGreg Roachuse function route;
334c3563c0SGreg Roach
344c3563c0SGreg Roach/**
354c3563c0SGreg Roach * Add a user.
364c3563c0SGreg Roach */
374c3563c0SGreg Roachclass UserAddAction implements RequestHandlerInterface
384c3563c0SGreg Roach{
394c3563c0SGreg Roach    /** @var UserService */
404c3563c0SGreg Roach    private $user_service;
414c3563c0SGreg Roach
424c3563c0SGreg Roach    /**
434c3563c0SGreg Roach     * UserAddAction constructor.
444c3563c0SGreg Roach     *
454c3563c0SGreg Roach     * @param UserService $user_service
464c3563c0SGreg Roach     */
474c3563c0SGreg Roach    public function __construct(UserService $user_service)
484c3563c0SGreg Roach    {
494c3563c0SGreg Roach        $this->user_service = $user_service;
504c3563c0SGreg Roach    }
514c3563c0SGreg Roach
524c3563c0SGreg Roach    /**
534c3563c0SGreg Roach     * @param ServerRequestInterface $request
544c3563c0SGreg Roach     *
554c3563c0SGreg Roach     * @return ResponseInterface
564c3563c0SGreg Roach     */
574c3563c0SGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
584c3563c0SGreg Roach    {
594c3563c0SGreg Roach        $params = (array) $request->getParsedBody();
604c3563c0SGreg Roach
614c3563c0SGreg Roach        $username  = $params['username'] ?? '';
624c3563c0SGreg Roach        $real_name = $params['real_name'] ?? '';
634c3563c0SGreg Roach        $email     = $params['email'] ?? '';
644c3563c0SGreg Roach        $password  = $params['password'] ?? '';
654c3563c0SGreg Roach
664c3563c0SGreg Roach        $errors = false;
674c3563c0SGreg Roach        if ($this->user_service->findByUserName($username)) {
684c3563c0SGreg Roach            FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'));
694c3563c0SGreg Roach            $errors = true;
704c3563c0SGreg Roach        }
714c3563c0SGreg Roach
724c3563c0SGreg Roach        if ($this->user_service->findByEmail($email)) {
734c3563c0SGreg Roach            FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.'));
744c3563c0SGreg Roach            $errors = true;
754c3563c0SGreg Roach        }
764c3563c0SGreg Roach
774c3563c0SGreg Roach        if ($errors) {
784c3563c0SGreg Roach            $url = route(UserAddPage::class, [
794c3563c0SGreg Roach                'email'     => $email,
804c3563c0SGreg Roach                'real_name' => $real_name,
814c3563c0SGreg Roach                'username'  => $username,
824c3563c0SGreg Roach            ]);
834c3563c0SGreg Roach
844c3563c0SGreg Roach            return redirect($url);
854c3563c0SGreg Roach        }
864c3563c0SGreg Roach
874c3563c0SGreg Roach        $new_user = $this->user_service->create($username, $real_name, $email, $password);
884c3563c0SGreg Roach        $new_user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1');
894c3563c0SGreg Roach        $new_user->setPreference(UserInterface::PREF_LANGUAGE, I18N::languageTag());
904c3563c0SGreg Roach        $new_user->setPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE', 'UTC'));
914c3563c0SGreg Roach        $new_user->setPreference(UserInterface::PREF_TIMESTAMP_REGISTERED, date('U'));
924c3563c0SGreg Roach        $new_user->setPreference(UserInterface::PREF_TIMESTAMP_ACTIVE, '0');
934c3563c0SGreg Roach
944c3563c0SGreg Roach        Log::addAuthenticationLog('User ->' . $username . '<- created');
954c3563c0SGreg Roach
964c3563c0SGreg Roach        $url = route(UserEditPage::class, [
974c3563c0SGreg Roach            'user_id' => $new_user->id(),
984c3563c0SGreg Roach        ]);
994c3563c0SGreg Roach
1004c3563c0SGreg Roach        return redirect($url);
1014c3563c0SGreg Roach    }
1024c3563c0SGreg Roach}
103