xref: /webtrees/app/Http/RequestHandlers/AccountUpdate.php (revision 00b1984e374faddab4698e73088f0c964e529b65)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\FlashMessages;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\UserService;
25use Fisharebest\Webtrees\Session;
26use Fisharebest\Webtrees\Tree;
27use Fisharebest\Webtrees\User;
28use Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function redirect;
33use function route;
34
35/**
36 * Edit user account details.
37 */
38class AccountUpdate implements RequestHandlerInterface
39{
40    /** @var UserService */
41    private $user_service;
42
43    /**
44     * AccountController constructor.
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 = $request->getAttribute('tree');
61
62        $user = $request->getAttribute('user');
63        assert($user instanceof User);
64
65        $params = (array) $request->getParsedBody();
66
67        $contact_method = $params['contact-method'];
68        $email          = $params['email'];
69        $language       = $params['language'];
70        $real_name      = $params['real_name'];
71        $password       = $params['password'];
72        $time_zone      = $params['timezone'];
73        $user_name      = $params['user_name'];
74        $visible_online = $params['visible-online'] ?? '';
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(User::PREF_CONTACT_METHOD, $contact_method);
101        $user->setPreference(User::PREF_LANGUAGE, $language);
102        $user->setPreference(User::PREF_TIME_ZONE, $time_zone);
103        $user->setPreference(User::PREF_IS_VISIBLE_ONLINE, $visible_online);
104
105        if ($tree instanceof Tree) {
106            $default_xref = $params['default-xref'];
107            $tree->setUserPreference($user, User::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 instanceof Tree ? $tree->name() : null]));
116    }
117}
118