1a49d0e3fSGreg Roach<?php 2a49d0e3fSGreg Roach 3a49d0e3fSGreg Roach/** 4a49d0e3fSGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 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; 29b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 30a49d0e3fSGreg Roachuse Psr\Http\Message\ResponseInterface; 31a49d0e3fSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 32a49d0e3fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 33a49d0e3fSGreg Roach 3410e06497SGreg Roachuse function assert; 35a49d0e3fSGreg Roachuse function redirect; 36a49d0e3fSGreg Roachuse function route; 37a49d0e3fSGreg Roach 38a49d0e3fSGreg Roach/** 39a49d0e3fSGreg Roach * Edit user account details. 40a49d0e3fSGreg Roach */ 41a49d0e3fSGreg Roachclass AccountUpdate implements RequestHandlerInterface 42a49d0e3fSGreg Roach{ 43c4943cffSGreg Roach private UserService $user_service; 44a49d0e3fSGreg Roach 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 { 60b55cbc6bSGreg Roach $tree = Validator::attributes($request)->treeOptional(); 61b55cbc6bSGreg Roach $user = Validator::attributes($request)->user(); 627c4add84SGreg Roach 637c4add84SGreg Roach assert($user instanceof User); 647c4add84SGreg Roach 65748dbe15SGreg Roach $contact_method = Validator::parsedBody($request)->string('contact-method'); 66748dbe15SGreg Roach $email = Validator::parsedBody($request)->string('email'); 67748dbe15SGreg Roach $language = Validator::parsedBody($request)->string('language'); 68748dbe15SGreg Roach $real_name = Validator::parsedBody($request)->string('real_name'); 69748dbe15SGreg Roach $password = Validator::parsedBody($request)->string('password'); 70748dbe15SGreg Roach $time_zone = Validator::parsedBody($request)->string('timezone'); 71748dbe15SGreg Roach $user_name = Validator::parsedBody($request)->string('user_name'); 72748dbe15SGreg Roach $visible_online = Validator::parsedBody($request)->boolean('visible-online', false); 73a49d0e3fSGreg Roach 74a49d0e3fSGreg Roach // Change the password 75a49d0e3fSGreg Roach if ($password !== '') { 76a49d0e3fSGreg Roach $user->setPassword($password); 77a49d0e3fSGreg Roach } 78a49d0e3fSGreg Roach 79a49d0e3fSGreg Roach // Change the username 80a49d0e3fSGreg Roach if ($user_name !== $user->userName()) { 81a49d0e3fSGreg Roach if ($this->user_service->findByUserName($user_name) === null) { 82a49d0e3fSGreg Roach $user->setUserName($user_name); 83a49d0e3fSGreg Roach } else { 84a49d0e3fSGreg Roach FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.')); 85a49d0e3fSGreg Roach } 86a49d0e3fSGreg Roach } 87a49d0e3fSGreg Roach 88a49d0e3fSGreg Roach // Change the email 89a49d0e3fSGreg Roach if ($email !== $user->email()) { 90a49d0e3fSGreg Roach if ($this->user_service->findByEmail($email) === null) { 91a49d0e3fSGreg Roach $user->setEmail($email); 92a49d0e3fSGreg Roach } else { 93a49d0e3fSGreg Roach FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.')); 94a49d0e3fSGreg Roach } 95a49d0e3fSGreg Roach } 96a49d0e3fSGreg Roach 977c4add84SGreg Roach $user->setRealName($real_name); 981fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_CONTACT_METHOD, $contact_method); 991fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_LANGUAGE, $language); 1001fe542e9SGreg Roach $user->setPreference(UserInterface::PREF_TIME_ZONE, $time_zone); 101748dbe15SGreg Roach $user->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, (string) $visible_online); 102a49d0e3fSGreg Roach 103a49d0e3fSGreg Roach if ($tree instanceof Tree) { 104748dbe15SGreg Roach $default_xref = Validator::parsedBody($request)->string('default-xref'); 1051fe542e9SGreg Roach $tree->setUserPreference($user, UserInterface::PREF_TREE_DEFAULT_XREF, $default_xref); 106a49d0e3fSGreg Roach } 107a49d0e3fSGreg Roach 108a49d0e3fSGreg Roach // Switch to the new language now 109a49d0e3fSGreg Roach Session::put('language', $language); 110a49d0e3fSGreg Roach 111b4c5c807SGreg Roach FlashMessages::addMessage(I18N::translate('The details for “%s” have been updated.', e($user->userName())), 'success'); 112a49d0e3fSGreg Roach 11381bf3221SGreg Roach return redirect(route(HomePage::class, ['tree' => $tree?->name()])); 114a49d0e3fSGreg Roach } 115a49d0e3fSGreg Roach} 116