1a49d0e3fSGreg Roach<?php 2a49d0e3fSGreg Roach 3a49d0e3fSGreg Roach/** 4a49d0e3fSGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team 6a49d0e3fSGreg Roach * This program is free software: you can redistribute it and/or modify 7a49d0e3fSGreg Roach * it under the terms of the GNU General Public License as published by 8a49d0e3fSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a49d0e3fSGreg Roach * (at your option) any later version. 10a49d0e3fSGreg Roach * This program is distributed in the hope that it will be useful, 11a49d0e3fSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a49d0e3fSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a49d0e3fSGreg Roach * GNU General Public License for more details. 14a49d0e3fSGreg 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/>. 16a49d0e3fSGreg Roach */ 17a49d0e3fSGreg Roach 18a49d0e3fSGreg Roachdeclare(strict_types=1); 19a49d0e3fSGreg Roach 20a49d0e3fSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21a49d0e3fSGreg Roach 221fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 23a49d0e3fSGreg Roachuse Fisharebest\Webtrees\FlashMessages; 24a49d0e3fSGreg Roachuse Fisharebest\Webtrees\I18N; 25a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Services\UserService; 26a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Session; 27a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Tree; 287c4add84SGreg Roachuse Fisharebest\Webtrees\User; 29a49d0e3fSGreg Roachuse Psr\Http\Message\ResponseInterface; 30a49d0e3fSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 31a49d0e3fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 32a49d0e3fSGreg Roach 33a49d0e3fSGreg Roachuse function redirect; 34a49d0e3fSGreg Roachuse function route; 35a49d0e3fSGreg Roach 36a49d0e3fSGreg Roach/** 37a49d0e3fSGreg Roach * Edit user account details. 38a49d0e3fSGreg Roach */ 39a49d0e3fSGreg Roachclass AccountUpdate implements RequestHandlerInterface 40a49d0e3fSGreg Roach{ 41*c4943cffSGreg Roach private UserService $user_service; 42a49d0e3fSGreg Roach 43a49d0e3fSGreg Roach /** 44a49d0e3fSGreg Roach * AccountController constructor. 45a49d0e3fSGreg Roach * 46a49d0e3fSGreg Roach * @param UserService $user_service 47a49d0e3fSGreg Roach */ 48a49d0e3fSGreg Roach public function __construct(UserService $user_service) 49a49d0e3fSGreg Roach { 50a49d0e3fSGreg Roach $this->user_service = $user_service; 51a49d0e3fSGreg Roach } 52a49d0e3fSGreg Roach 53a49d0e3fSGreg Roach /** 54a49d0e3fSGreg Roach * @param ServerRequestInterface $request 55a49d0e3fSGreg Roach * 56a49d0e3fSGreg Roach * @return ResponseInterface 57a49d0e3fSGreg Roach */ 58a49d0e3fSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 59a49d0e3fSGreg Roach { 60a49d0e3fSGreg Roach $tree = $request->getAttribute('tree'); 617c4add84SGreg Roach 62a49d0e3fSGreg Roach $user = $request->getAttribute('user'); 637c4add84SGreg Roach assert($user instanceof User); 647c4add84SGreg Roach 65b46c87bdSGreg Roach $params = (array) $request->getParsedBody(); 66a49d0e3fSGreg Roach 677c4add84SGreg Roach $contact_method = $params['contact-method']; 68a49d0e3fSGreg Roach $email = $params['email']; 69a49d0e3fSGreg Roach $language = $params['language']; 70a49d0e3fSGreg Roach $real_name = $params['real_name']; 71a49d0e3fSGreg Roach $password = $params['password']; 72a49d0e3fSGreg Roach $time_zone = $params['timezone']; 73a49d0e3fSGreg Roach $user_name = $params['user_name']; 747c4add84SGreg Roach $visible_online = $params['visible-online'] ?? ''; 75a49d0e3fSGreg Roach 76a49d0e3fSGreg Roach // Change the password 77a49d0e3fSGreg Roach if ($password !== '') { 78a49d0e3fSGreg Roach $user->setPassword($password); 79a49d0e3fSGreg Roach } 80a49d0e3fSGreg Roach 81a49d0e3fSGreg Roach // Change the username 82a49d0e3fSGreg Roach if ($user_name !== $user->userName()) { 83a49d0e3fSGreg Roach if ($this->user_service->findByUserName($user_name) === null) { 84a49d0e3fSGreg Roach $user->setUserName($user_name); 85a49d0e3fSGreg Roach } else { 86a49d0e3fSGreg Roach FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.')); 87a49d0e3fSGreg Roach } 88a49d0e3fSGreg Roach } 89a49d0e3fSGreg Roach 90a49d0e3fSGreg Roach // Change the email 91a49d0e3fSGreg Roach if ($email !== $user->email()) { 92a49d0e3fSGreg Roach if ($this->user_service->findByEmail($email) === null) { 93a49d0e3fSGreg Roach $user->setEmail($email); 94a49d0e3fSGreg Roach } else { 95a49d0e3fSGreg Roach FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.')); 96a49d0e3fSGreg Roach } 97a49d0e3fSGreg Roach } 98a49d0e3fSGreg Roach 997c4add84SGreg Roach $user->setRealName($real_name); 1001fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_CONTACT_METHOD, $contact_method); 1011fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_LANGUAGE, $language); 1021fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_TIME_ZONE, $time_zone); 1031fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, $visible_online); 104a49d0e3fSGreg Roach 105a49d0e3fSGreg Roach if ($tree instanceof Tree) { 1067c4add84SGreg Roach $default_xref = $params['default-xref']; 1071fe542e9SGreg Roach $tree->setUserPreference($user, UserInterface::PREF_TREE_DEFAULT_XREF, $default_xref); 108a49d0e3fSGreg Roach } 109a49d0e3fSGreg Roach 110a49d0e3fSGreg Roach // Switch to the new language now 111a49d0e3fSGreg Roach Session::put('language', $language); 112a49d0e3fSGreg Roach 113a49d0e3fSGreg Roach FlashMessages::addMessage(I18N::translate('The details for “%s” have been updated.', e($user->username())), 'success'); 114a49d0e3fSGreg Roach 115d98c9706SGreg Roach return redirect(route(HomePage::class, ['tree' => $tree instanceof Tree ? $tree->name() : null])); 116a49d0e3fSGreg Roach } 117a49d0e3fSGreg Roach} 118