xref: /webtrees/app/Http/RequestHandlers/AccountUpdate.php (revision 4a9a60957058ca2a34e30a0d6f783e1540606590)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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        $params = (array) $request->getParsedBody();
68
69        $contact_method = $params['contact-method'];
70        $email          = $params['email'];
71        $language       = $params['language'];
72        $real_name      = $params['real_name'];
73        $password       = $params['password'];
74        $time_zone      = $params['timezone'];
75        $user_name      = $params['user_name'];
76        $visible_online = $params['visible-online'] ?? '';
77
78        // Change the password
79        if ($password !== '') {
80            $user->setPassword($password);
81        }
82
83        // Change the username
84        if ($user_name !== $user->userName()) {
85            if ($this->user_service->findByUserName($user_name) === null) {
86                $user->setUserName($user_name);
87            } else {
88                FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'));
89            }
90        }
91
92        // Change the email
93        if ($email !== $user->email()) {
94            if ($this->user_service->findByEmail($email) === null) {
95                $user->setEmail($email);
96            } else {
97                FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.'));
98            }
99        }
100
101        $user->setRealName($real_name);
102        $user->setPreference(UserInterface::PREF_CONTACT_METHOD, $contact_method);
103        $user->setPreference(UserInterface::PREF_LANGUAGE, $language);
104        $user->setPreference(UserInterface::PREF_TIME_ZONE, $time_zone);
105        $user->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, $visible_online);
106
107        if ($tree instanceof Tree) {
108            $default_xref = $params['default-xref'];
109            $tree->setUserPreference($user, UserInterface::PREF_TREE_DEFAULT_XREF, $default_xref);
110        }
111
112        // Switch to the new language now
113        Session::put('language', $language);
114
115        FlashMessages::addMessage(I18N::translate('The details for “%s” have been updated.', e($user->userName())), 'success');
116
117        return redirect(route(HomePage::class, ['tree' => $tree instanceof Tree ? $tree->name() : null]));
118    }
119}
120