1<?php 2 3/** 4 * webtrees: online genealogy 5 * 'Copyright (C) 2023 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fisharebest\Webtrees\Contracts\UserInterface; 23use Fisharebest\Webtrees\FlashMessages; 24use Fisharebest\Webtrees\I18N; 25use Fisharebest\Webtrees\Services\UserService; 26use Fisharebest\Webtrees\Session; 27use Fisharebest\Webtrees\Tree; 28use Fisharebest\Webtrees\User; 29use Fisharebest\Webtrees\Validator; 30use Psr\Http\Message\ResponseInterface; 31use Psr\Http\Message\ServerRequestInterface; 32use Psr\Http\Server\RequestHandlerInterface; 33 34use function assert; 35use function redirect; 36use function route; 37 38/** 39 * Edit user account details. 40 */ 41class AccountUpdate implements RequestHandlerInterface 42{ 43 private UserService $user_service; 44 45 /** 46 * AccountController constructor. 47 * 48 * @param UserService $user_service 49 */ 50 public function __construct(UserService $user_service) 51 { 52 $this->user_service = $user_service; 53 } 54 55 /** 56 * @param ServerRequestInterface $request 57 * 58 * @return ResponseInterface 59 */ 60 public function handle(ServerRequestInterface $request): ResponseInterface 61 { 62 $tree = Validator::attributes($request)->treeOptional(); 63 $user = Validator::attributes($request)->user(); 64 65 assert($user instanceof User); 66 67 $contact_method = Validator::parsedBody($request)->string('contact-method'); 68 $email = Validator::parsedBody($request)->string('email'); 69 $language = Validator::parsedBody($request)->string('language'); 70 $real_name = Validator::parsedBody($request)->string('real_name'); 71 $password = Validator::parsedBody($request)->string('password'); 72 $time_zone = Validator::parsedBody($request)->string('timezone'); 73 $user_name = Validator::parsedBody($request)->string('user_name'); 74 $visible_online = Validator::parsedBody($request)->boolean('visible-online', false); 75 76 // Change the password 77 if ($password !== '') { 78 $user->setPassword($password); 79 } 80 81 // Change the username 82 if ($user_name !== $user->userName()) { 83 if ($this->user_service->findByUserName($user_name) === null) { 84 $user->setUserName($user_name); 85 } else { 86 FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.')); 87 } 88 } 89 90 // Change the email 91 if ($email !== $user->email()) { 92 if ($this->user_service->findByEmail($email) === null) { 93 $user->setEmail($email); 94 } else { 95 FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.')); 96 } 97 } 98 99 $user->setRealName($real_name); 100 $user->setPreference(UserInterface::PREF_CONTACT_METHOD, $contact_method); 101 $user->setPreference(UserInterface::PREF_LANGUAGE, $language); 102 $user->setPreference(UserInterface::PREF_TIME_ZONE, $time_zone); 103 $user->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, (string) $visible_online); 104 105 if ($tree instanceof Tree) { 106 $default_xref = Validator::parsedBody($request)->string('default-xref'); 107 $tree->setUserPreference($user, UserInterface::PREF_TREE_DEFAULT_XREF, $default_xref); 108 } 109 110 // Switch to the new language now 111 Session::put('language', $language); 112 113 FlashMessages::addMessage(I18N::translate('The details for “%s” have been updated.', e($user->userName())), 'success'); 114 115 return redirect(route(HomePage::class, ['tree' => $tree?->name()])); 116 } 117} 118