xref: /webtrees/app/Http/RequestHandlers/AccountUpdate.php (revision b6017f990d38d8c56e04c0096ce9a7e8745ad4ba)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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 Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function redirect;
34use function route;
35
36/**
37 * Edit user account details.
38 */
39class AccountUpdate implements RequestHandlerInterface
40{
41    /** @var UserService */
42    private $user_service;
43
44    /**
45     * AccountController constructor.
46     *
47     * @param UserService $user_service
48     */
49    public function __construct(UserService $user_service)
50    {
51        $this->user_service = $user_service;
52    }
53
54    /**
55     * @param ServerRequestInterface $request
56     *
57     * @return ResponseInterface
58     */
59    public function handle(ServerRequestInterface $request): ResponseInterface
60    {
61        $tree = $request->getAttribute('tree');
62
63        $user = $request->getAttribute('user');
64        assert($user instanceof User);
65
66        $params = (array) $request->getParsedBody();
67
68        $contact_method = $params['contact-method'];
69        $email          = $params['email'];
70        $language       = $params['language'];
71        $real_name      = $params['real_name'];
72        $password       = $params['password'];
73        $time_zone      = $params['timezone'];
74        $user_name      = $params['user_name'];
75        $visible_online = $params['visible-online'] ?? '';
76
77        // Change the password
78        if ($password !== '') {
79            $user->setPassword($password);
80        }
81
82        // Change the username
83        if ($user_name !== $user->userName()) {
84            if ($this->user_service->findByUserName($user_name) === null) {
85                $user->setUserName($user_name);
86            } else {
87                FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'));
88            }
89        }
90
91        // Change the email
92        if ($email !== $user->email()) {
93            if ($this->user_service->findByEmail($email) === null) {
94                $user->setEmail($email);
95            } else {
96                FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.'));
97            }
98        }
99
100        $user->setRealName($real_name);
101        $user->setPreference(UserInterface::PREF_CONTACT_METHOD, $contact_method);
102        $user->setPreference(UserInterface::PREF_LANGUAGE, $language);
103        $user->setPreference(UserInterface::PREF_TIME_ZONE, $time_zone);
104        $user->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, $visible_online);
105
106        if ($tree instanceof Tree) {
107            $default_xref = $params['default-xref'];
108            $tree->setUserPreference($user, UserInterface::PREF_TREE_DEFAULT_XREF, $default_xref);
109        }
110
111        // Switch to the new language now
112        Session::put('language', $language);
113
114        FlashMessages::addMessage(I18N::translate('The details for “%s” have been updated.', e($user->username())), 'success');
115
116        return redirect(route(HomePage::class, ['tree' => $tree instanceof Tree ? $tree->name() : null]));
117    }
118}
119