14c3563c0SGreg Roach<?php 24c3563c0SGreg Roach 34c3563c0SGreg Roach/** 44c3563c0SGreg Roach * webtrees: online genealogy 5*5bfc6897SGreg 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\Auth; 234c3563c0SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 244c3563c0SGreg Roachuse Fisharebest\Webtrees\FlashMessages; 2581b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; 264c3563c0SGreg Roachuse Fisharebest\Webtrees\I18N; 274c3563c0SGreg Roachuse Fisharebest\Webtrees\Services\EmailService; 284c3563c0SGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 294c3563c0SGreg Roachuse Fisharebest\Webtrees\Services\UserService; 304c3563c0SGreg Roachuse Fisharebest\Webtrees\SiteUser; 314c3563c0SGreg Roachuse Fisharebest\Webtrees\User; 32b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 334c3563c0SGreg Roachuse Psr\Http\Message\ResponseInterface; 344c3563c0SGreg Roachuse Psr\Http\Message\ServerRequestInterface; 354c3563c0SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 364c3563c0SGreg Roach 374c3563c0SGreg Roachuse function route; 384c3563c0SGreg Roach 394c3563c0SGreg Roach/** 404c3563c0SGreg Roach * Edit a user. 414c3563c0SGreg Roach */ 424c3563c0SGreg Roachclass UserEditAction implements RequestHandlerInterface 434c3563c0SGreg Roach{ 44c4943cffSGreg Roach private EmailService $email_service; 454c3563c0SGreg Roach 46c4943cffSGreg Roach private UserService $user_service; 474c3563c0SGreg Roach 48c4943cffSGreg Roach private TreeService $tree_service; 494c3563c0SGreg Roach 504c3563c0SGreg Roach /** 514c3563c0SGreg Roach * UserEditAction constructor. 524c3563c0SGreg Roach * 534c3563c0SGreg Roach * @param EmailService $email_service 544c3563c0SGreg Roach * @param TreeService $tree_service 554c3563c0SGreg Roach * @param UserService $user_service 564c3563c0SGreg Roach */ 574c3563c0SGreg Roach public function __construct( 584c3563c0SGreg Roach EmailService $email_service, 594c3563c0SGreg Roach TreeService $tree_service, 604c3563c0SGreg Roach UserService $user_service 614c3563c0SGreg Roach ) { 624c3563c0SGreg Roach $this->email_service = $email_service; 634c3563c0SGreg Roach $this->tree_service = $tree_service; 644c3563c0SGreg Roach $this->user_service = $user_service; 654c3563c0SGreg Roach } 664c3563c0SGreg Roach 674c3563c0SGreg Roach /** 684c3563c0SGreg Roach * @param ServerRequestInterface $request 694c3563c0SGreg Roach * 704c3563c0SGreg Roach * @return ResponseInterface 714c3563c0SGreg Roach */ 724c3563c0SGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 734c3563c0SGreg Roach { 74b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 754c3563c0SGreg Roach 764c3563c0SGreg Roach $params = (array) $request->getParsedBody(); 774c3563c0SGreg Roach 784c3563c0SGreg Roach $user_id = (int) $params['user_id']; 794c3563c0SGreg Roach $username = $params['username'] ?? ''; 804c3563c0SGreg Roach $real_name = $params['real_name'] ?? ''; 814c3563c0SGreg Roach $email = $params['email'] ?? ''; 824c3563c0SGreg Roach $password = $params['password'] ?? ''; 834c3563c0SGreg Roach $theme = $params['theme'] ?? ''; 844c3563c0SGreg Roach $language = $params['language'] ?? ''; 854c3563c0SGreg Roach $timezone = $params['timezone'] ?? ''; 864c3563c0SGreg Roach $contact_method = $params['contact-method'] ?? ''; 874c3563c0SGreg Roach $comment = $params['comment'] ?? ''; 884c3563c0SGreg Roach $auto_accept = (bool) ($params[UserInterface::PREF_AUTO_ACCEPT_EDITS] ?? ''); 894c3563c0SGreg Roach $canadmin = (bool) ($params[UserInterface::PREF_IS_ADMINISTRATOR] ?? ''); 904c3563c0SGreg Roach $visible_online = (bool) ($params['visible-online'] ?? ''); 914c3563c0SGreg Roach $verified = (bool) ($params[UserInterface::PREF_IS_EMAIL_VERIFIED] ?? ''); 924c3563c0SGreg Roach $approved = (bool) ($params['approved'] ?? ''); 934c3563c0SGreg Roach 944c3563c0SGreg Roach $edit_user = $this->user_service->find($user_id); 954c3563c0SGreg Roach 964c3563c0SGreg Roach if ($edit_user === null) { 974566681eSGreg Roach throw new HttpNotFoundException(I18N::translate('%s does not exist.', 'user_id:' . $user_id)); 984c3563c0SGreg Roach } 994c3563c0SGreg Roach 1004c3563c0SGreg Roach // We have just approved a user. Tell them 1014c3563c0SGreg Roach if ($approved && $edit_user->getPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED) !== '1') { 1024c3563c0SGreg Roach I18N::init($edit_user->getPreference(UserInterface::PREF_LANGUAGE)); 1034c3563c0SGreg Roach 104b55cbc6bSGreg Roach $base_url = Validator::attributes($request)->string('base_url'); 1054c3563c0SGreg Roach 1064c3563c0SGreg Roach $this->email_service->send( 1074c3563c0SGreg Roach new SiteUser(), 1084c3563c0SGreg Roach $edit_user, 1094c3563c0SGreg Roach Auth::user(), 1104c3563c0SGreg Roach /* I18N: %s is a server name/URL */ 1114c3563c0SGreg Roach I18N::translate('New user at %s', $base_url), 1124c3563c0SGreg Roach view('emails/approve-user-text', ['user' => $edit_user, 'base_url' => $base_url]), 1134c3563c0SGreg Roach view('emails/approve-user-html', ['user' => $edit_user, 'base_url' => $base_url]) 1144c3563c0SGreg Roach ); 1154c3563c0SGreg Roach } 1164c3563c0SGreg Roach 1174c3563c0SGreg Roach $edit_user->setRealName($real_name); 1184c3563c0SGreg Roach $edit_user->setPreference(UserInterface::PREF_THEME, $theme); 1194c3563c0SGreg Roach $edit_user->setPreference(UserInterface::PREF_LANGUAGE, $language); 1204c3563c0SGreg Roach $edit_user->setPreference(UserInterface::PREF_TIME_ZONE, $timezone); 1214c3563c0SGreg Roach $edit_user->setPreference(UserInterface::PREF_CONTACT_METHOD, $contact_method); 1224c3563c0SGreg Roach $edit_user->setPreference(UserInterface::PREF_NEW_ACCOUNT_COMMENT, $comment); 1234c3563c0SGreg Roach $edit_user->setPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS, (string) $auto_accept); 1244c3563c0SGreg Roach $edit_user->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, (string) $visible_online); 1254c3563c0SGreg Roach $edit_user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, (string) $verified); 1264c3563c0SGreg Roach $edit_user->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, (string) $approved); 1274c3563c0SGreg Roach 1284c3563c0SGreg Roach if ($password !== '') { 1294c3563c0SGreg Roach $edit_user->setPassword($password); 1304c3563c0SGreg Roach } 1314c3563c0SGreg Roach 1324c3563c0SGreg Roach // We cannot change our own admin status. Another admin will need to do it. 1334c3563c0SGreg Roach if ($edit_user->id() !== $user->id()) { 1344c3563c0SGreg Roach $edit_user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, $canadmin ? '1' : ''); 1354c3563c0SGreg Roach } 1364c3563c0SGreg Roach 1374c3563c0SGreg Roach foreach ($this->tree_service->all() as $tree) { 1384c3563c0SGreg Roach $path_length = (int) $params['RELATIONSHIP_PATH_LENGTH' . $tree->id()]; 1394c3563c0SGreg Roach $gedcom_id = $params['gedcomid' . $tree->id()] ?? ''; 1404c3563c0SGreg Roach $can_edit = $params['canedit' . $tree->id()] ?? ''; 1414c3563c0SGreg Roach 1424c3563c0SGreg Roach // Do not allow a path length to be set if the individual ID is not 1434c3563c0SGreg Roach if ($gedcom_id === '') { 1444c3563c0SGreg Roach $path_length = 0; 1454c3563c0SGreg Roach } 1464c3563c0SGreg Roach 1474c3563c0SGreg Roach $tree->setUserPreference($edit_user, UserInterface::PREF_TREE_ACCOUNT_XREF, $gedcom_id); 1484c3563c0SGreg Roach $tree->setUserPreference($edit_user, UserInterface::PREF_TREE_ROLE, $can_edit); 1494c3563c0SGreg Roach $tree->setUserPreference($edit_user, UserInterface::PREF_TREE_PATH_LENGTH, (string) $path_length); 1504c3563c0SGreg Roach } 1514c3563c0SGreg Roach 1524c3563c0SGreg Roach if ($edit_user->email() !== $email && $this->user_service->findByEmail($email) instanceof User) { 1534c3563c0SGreg Roach FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.') . $email, 'danger'); 1544c3563c0SGreg Roach 1554c3563c0SGreg Roach return redirect(route('admin-users-edit', ['user_id' => $edit_user->id()])); 1564c3563c0SGreg Roach } 1574c3563c0SGreg Roach 1584c3563c0SGreg Roach if ($edit_user->userName() !== $username && $this->user_service->findByUserName($username) instanceof User) { 1594c3563c0SGreg Roach FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'), 'danger'); 1604c3563c0SGreg Roach 1614c3563c0SGreg Roach return redirect(route(UserEditPage::class, ['user_id' => $edit_user->id()])); 1624c3563c0SGreg Roach } 1634c3563c0SGreg Roach 1644c3563c0SGreg Roach $edit_user 1654c3563c0SGreg Roach ->setEmail($email) 1664c3563c0SGreg Roach ->setUserName($username); 1674c3563c0SGreg Roach 1684c3563c0SGreg Roach return redirect(route(UserListPage::class)); 1694c3563c0SGreg Roach } 1704c3563c0SGreg Roach} 171