14c3563c0SGreg Roach<?php 24c3563c0SGreg Roach 34c3563c0SGreg Roach/** 44c3563c0SGreg Roach * webtrees: online genealogy 55bfc6897SGreg Roach * Copyright (C) 2022 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*748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator; 294c3563c0SGreg Roachuse Psr\Http\Message\ResponseInterface; 304c3563c0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 314c3563c0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 324c3563c0SGreg Roach 334c3563c0SGreg Roachuse function route; 344c3563c0SGreg Roach 354c3563c0SGreg Roach/** 364c3563c0SGreg Roach * Add a user. 374c3563c0SGreg Roach */ 384c3563c0SGreg Roachclass UserAddAction implements RequestHandlerInterface 394c3563c0SGreg Roach{ 40c4943cffSGreg Roach private UserService $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 { 59*748dbe15SGreg Roach $username = Validator::parsedBody($request)->string('username'); 60*748dbe15SGreg Roach $real_name = Validator::parsedBody($request)->string('real_name'); 61*748dbe15SGreg Roach $email = Validator::parsedBody($request)->string('email'); 62*748dbe15SGreg Roach $password = Validator::parsedBody($request)->string('password'); 634c3563c0SGreg Roach 644c3563c0SGreg Roach $errors = false; 654c3563c0SGreg Roach if ($this->user_service->findByUserName($username)) { 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 704c3563c0SGreg Roach if ($this->user_service->findByEmail($email)) { 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