1*4c3563c0SGreg Roach<?php 2*4c3563c0SGreg Roach 3*4c3563c0SGreg Roach/** 4*4c3563c0SGreg Roach * webtrees: online genealogy 5*4c3563c0SGreg Roach * Copyright (C) 2021 webtrees development team 6*4c3563c0SGreg Roach * This program is free software: you can redistribute it and/or modify 7*4c3563c0SGreg Roach * it under the terms of the GNU General Public License as published by 8*4c3563c0SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9*4c3563c0SGreg Roach * (at your option) any later version. 10*4c3563c0SGreg Roach * This program is distributed in the hope that it will be useful, 11*4c3563c0SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12*4c3563c0SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13*4c3563c0SGreg Roach * GNU General Public License for more details. 14*4c3563c0SGreg Roach * You should have received a copy of the GNU General Public License 15*4c3563c0SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16*4c3563c0SGreg Roach */ 17*4c3563c0SGreg Roach 18*4c3563c0SGreg Roachdeclare(strict_types=1); 19*4c3563c0SGreg Roach 20*4c3563c0SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21*4c3563c0SGreg Roach 22*4c3563c0SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 23*4c3563c0SGreg Roachuse Fisharebest\Webtrees\FlashMessages; 24*4c3563c0SGreg Roachuse Fisharebest\Webtrees\I18N; 25*4c3563c0SGreg Roachuse Fisharebest\Webtrees\Log; 26*4c3563c0SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 27*4c3563c0SGreg Roachuse Fisharebest\Webtrees\Site; 28*4c3563c0SGreg Roachuse Psr\Http\Message\ResponseInterface; 29*4c3563c0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 30*4c3563c0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 31*4c3563c0SGreg Roach 32*4c3563c0SGreg Roachuse function route; 33*4c3563c0SGreg Roach 34*4c3563c0SGreg Roach/** 35*4c3563c0SGreg Roach * Add a user. 36*4c3563c0SGreg Roach */ 37*4c3563c0SGreg Roachclass UserAddAction implements RequestHandlerInterface 38*4c3563c0SGreg Roach{ 39*4c3563c0SGreg Roach /** @var UserService */ 40*4c3563c0SGreg Roach private $user_service; 41*4c3563c0SGreg Roach 42*4c3563c0SGreg Roach /** 43*4c3563c0SGreg Roach * UserAddAction constructor. 44*4c3563c0SGreg Roach * 45*4c3563c0SGreg Roach * @param UserService $user_service 46*4c3563c0SGreg Roach */ 47*4c3563c0SGreg Roach public function __construct(UserService $user_service) 48*4c3563c0SGreg Roach { 49*4c3563c0SGreg Roach $this->user_service = $user_service; 50*4c3563c0SGreg Roach } 51*4c3563c0SGreg Roach 52*4c3563c0SGreg Roach /** 53*4c3563c0SGreg Roach * @param ServerRequestInterface $request 54*4c3563c0SGreg Roach * 55*4c3563c0SGreg Roach * @return ResponseInterface 56*4c3563c0SGreg Roach */ 57*4c3563c0SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 58*4c3563c0SGreg Roach { 59*4c3563c0SGreg Roach $params = (array) $request->getParsedBody(); 60*4c3563c0SGreg Roach 61*4c3563c0SGreg Roach $username = $params['username'] ?? ''; 62*4c3563c0SGreg Roach $real_name = $params['real_name'] ?? ''; 63*4c3563c0SGreg Roach $email = $params['email'] ?? ''; 64*4c3563c0SGreg Roach $password = $params['password'] ?? ''; 65*4c3563c0SGreg Roach 66*4c3563c0SGreg Roach $errors = false; 67*4c3563c0SGreg Roach if ($this->user_service->findByUserName($username)) { 68*4c3563c0SGreg Roach FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.')); 69*4c3563c0SGreg Roach $errors = true; 70*4c3563c0SGreg Roach } 71*4c3563c0SGreg Roach 72*4c3563c0SGreg Roach if ($this->user_service->findByEmail($email)) { 73*4c3563c0SGreg Roach FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.')); 74*4c3563c0SGreg Roach $errors = true; 75*4c3563c0SGreg Roach } 76*4c3563c0SGreg Roach 77*4c3563c0SGreg Roach if ($errors) { 78*4c3563c0SGreg Roach $url = route(UserAddPage::class, [ 79*4c3563c0SGreg Roach 'email' => $email, 80*4c3563c0SGreg Roach 'real_name' => $real_name, 81*4c3563c0SGreg Roach 'username' => $username, 82*4c3563c0SGreg Roach ]); 83*4c3563c0SGreg Roach 84*4c3563c0SGreg Roach return redirect($url); 85*4c3563c0SGreg Roach } 86*4c3563c0SGreg Roach 87*4c3563c0SGreg Roach $new_user = $this->user_service->create($username, $real_name, $email, $password); 88*4c3563c0SGreg Roach $new_user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, '1'); 89*4c3563c0SGreg Roach $new_user->setPreference(UserInterface::PREF_LANGUAGE, I18N::languageTag()); 90*4c3563c0SGreg Roach $new_user->setPreference(UserInterface::PREF_TIME_ZONE, Site::getPreference('TIMEZONE', 'UTC')); 91*4c3563c0SGreg Roach $new_user->setPreference(UserInterface::PREF_TIMESTAMP_REGISTERED, date('U')); 92*4c3563c0SGreg Roach $new_user->setPreference(UserInterface::PREF_TIMESTAMP_ACTIVE, '0'); 93*4c3563c0SGreg Roach 94*4c3563c0SGreg Roach Log::addAuthenticationLog('User ->' . $username . '<- created'); 95*4c3563c0SGreg Roach 96*4c3563c0SGreg Roach $url = route(UserEditPage::class, [ 97*4c3563c0SGreg Roach 'user_id' => $new_user->id(), 98*4c3563c0SGreg Roach ]); 99*4c3563c0SGreg Roach 100*4c3563c0SGreg Roach return redirect($url); 101*4c3563c0SGreg Roach } 102*4c3563c0SGreg Roach} 103