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