xref: /webtrees/app/Http/RequestHandlers/AccountUpdate.php (revision 6f4ec3cadc983f0a7294108c634bef48846b4311)
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     * @param UserService $user_service
47     */
48    public function __construct(UserService $user_service)
49    {
50        $this->user_service = $user_service;
51    }
52
53    /**
54     * @param ServerRequestInterface $request
55     *
56     * @return ResponseInterface
57     */
58    public function handle(ServerRequestInterface $request): ResponseInterface
59    {
60        $tree = Validator::attributes($request)->treeOptional();
61        $user = Validator::attributes($request)->user();
62
63        assert($user instanceof User);
64
65        $contact_method = Validator::parsedBody($request)->string('contact-method');
66        $email          = Validator::parsedBody($request)->string('email');
67        $language       = Validator::parsedBody($request)->string('language');
68        $real_name      = Validator::parsedBody($request)->string('real_name');
69        $password       = Validator::parsedBody($request)->string('password');
70        $time_zone      = Validator::parsedBody($request)->string('timezone');
71        $user_name      = Validator::parsedBody($request)->string('user_name');
72        $visible_online = Validator::parsedBody($request)->boolean('visible-online', false);
73
74        // Change the password
75        if ($password !== '') {
76            $user->setPassword($password);
77        }
78
79        // Change the username
80        if ($user_name !== $user->userName()) {
81            if ($this->user_service->findByUserName($user_name) === null) {
82                $user->setUserName($user_name);
83            } else {
84                FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'));
85            }
86        }
87
88        // Change the email
89        if ($email !== $user->email()) {
90            if ($this->user_service->findByEmail($email) === null) {
91                $user->setEmail($email);
92            } else {
93                FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.'));
94            }
95        }
96
97        $user->setRealName($real_name);
98        $user->setPreference(UserInterface::PREF_CONTACT_METHOD, $contact_method);
99        $user->setPreference(UserInterface::PREF_LANGUAGE, $language);
100        $user->setPreference(UserInterface::PREF_TIME_ZONE, $time_zone);
101        $user->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, (string) $visible_online);
102
103        if ($tree instanceof Tree) {
104            $default_xref = Validator::parsedBody($request)->string('default-xref');
105            $tree->setUserPreference($user, UserInterface::PREF_TREE_DEFAULT_XREF, $default_xref);
106        }
107
108        // Switch to the new language now
109        Session::put('language', $language);
110
111        FlashMessages::addMessage(I18N::translate('The details for “%s” have been updated.', e($user->userName())), 'success');
112
113        return redirect(route(HomePage::class, ['tree' => $tree?->name()]));
114    }
115}
116