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 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; 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{ 39*c4943cffSGreg Roach private UserService $user_service; 404c3563c0SGreg Roach 414c3563c0SGreg Roach /** 424c3563c0SGreg Roach * UserAddAction constructor. 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 { 584c3563c0SGreg Roach $params = (array) $request->getParsedBody(); 594c3563c0SGreg Roach 604c3563c0SGreg Roach $username = $params['username'] ?? ''; 614c3563c0SGreg Roach $real_name = $params['real_name'] ?? ''; 624c3563c0SGreg Roach $email = $params['email'] ?? ''; 634c3563c0SGreg Roach $password = $params['password'] ?? ''; 644c3563c0SGreg Roach 654c3563c0SGreg Roach $errors = false; 664c3563c0SGreg Roach if ($this->user_service->findByUserName($username)) { 674c3563c0SGreg Roach FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.')); 684c3563c0SGreg Roach $errors = true; 694c3563c0SGreg Roach } 704c3563c0SGreg Roach 714c3563c0SGreg Roach if ($this->user_service->findByEmail($email)) { 724c3563c0SGreg Roach FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.')); 734c3563c0SGreg Roach $errors = true; 744c3563c0SGreg Roach } 754c3563c0SGreg Roach 764c3563c0SGreg Roach if ($errors) { 774c3563c0SGreg Roach $url = route(UserAddPage::class, [ 784c3563c0SGreg Roach 'email' => $email, 794c3563c0SGreg Roach 'real_name' => $real_name, 804c3563c0SGreg Roach 'username' => $username, 814c3563c0SGreg Roach ]); 824c3563c0SGreg Roach 834c3563c0SGreg Roach return redirect($url); 844c3563c0SGreg Roach } 854c3563c0SGreg Roach 864c3563c0SGreg Roach $new_user = $this->user_service->create($username, $real_name, $email, $password); 874c3563c0SGreg Roach $new_user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1'); 884c3563c0SGreg Roach $new_user->setPreference(UserInterface::PREF_LANGUAGE, I18N::languageTag()); 894c3563c0SGreg Roach $new_user->setPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE', 'UTC')); 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