xref: /webtrees/app/Http/RequestHandlers/AccountUpdate.php (revision d98c970665def1ecf381b8131cdd95cc653b0aec)
1a49d0e3fSGreg Roach<?php
2a49d0e3fSGreg Roach
3a49d0e3fSGreg Roach/**
4a49d0e3fSGreg Roach * webtrees: online genealogy
5a49d0e3fSGreg Roach * Copyright (C) 2019 webtrees development team
6a49d0e3fSGreg Roach * This program is free software: you can redistribute it and/or modify
7a49d0e3fSGreg Roach * it under the terms of the GNU General Public License as published by
8a49d0e3fSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a49d0e3fSGreg Roach * (at your option) any later version.
10a49d0e3fSGreg Roach * This program is distributed in the hope that it will be useful,
11a49d0e3fSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a49d0e3fSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a49d0e3fSGreg Roach * GNU General Public License for more details.
14a49d0e3fSGreg Roach * You should have received a copy of the GNU General Public License
15a49d0e3fSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16a49d0e3fSGreg Roach */
17a49d0e3fSGreg Roach
18a49d0e3fSGreg Roachdeclare(strict_types=1);
19a49d0e3fSGreg Roach
20a49d0e3fSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21a49d0e3fSGreg Roach
22a49d0e3fSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
23a49d0e3fSGreg Roachuse Fisharebest\Webtrees\I18N;
24a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
25a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Session;
26a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Tree;
27a49d0e3fSGreg Roachuse Psr\Http\Message\ResponseInterface;
28a49d0e3fSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
29a49d0e3fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
30a49d0e3fSGreg Roach
31a49d0e3fSGreg Roachuse function redirect;
32a49d0e3fSGreg Roachuse function route;
33a49d0e3fSGreg Roach
34a49d0e3fSGreg Roach/**
35a49d0e3fSGreg Roach * Edit user account details.
36a49d0e3fSGreg Roach */
37a49d0e3fSGreg Roachclass AccountUpdate implements RequestHandlerInterface
38a49d0e3fSGreg Roach{
39a49d0e3fSGreg Roach    /** @var UserService */
40a49d0e3fSGreg Roach    private $user_service;
41a49d0e3fSGreg Roach
42a49d0e3fSGreg Roach    /**
43a49d0e3fSGreg Roach     * AccountController constructor.
44a49d0e3fSGreg Roach     *
45a49d0e3fSGreg Roach     * @param UserService $user_service
46a49d0e3fSGreg Roach     */
47a49d0e3fSGreg Roach    public function __construct(UserService $user_service)
48a49d0e3fSGreg Roach    {
49a49d0e3fSGreg Roach        $this->user_service = $user_service;
50a49d0e3fSGreg Roach    }
51a49d0e3fSGreg Roach
52a49d0e3fSGreg Roach    /**
53a49d0e3fSGreg Roach     * @param ServerRequestInterface $request
54a49d0e3fSGreg Roach     *
55a49d0e3fSGreg Roach     * @return ResponseInterface
56a49d0e3fSGreg Roach     */
57a49d0e3fSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
58a49d0e3fSGreg Roach    {
59a49d0e3fSGreg Roach        $tree   = $request->getAttribute('tree');
60a49d0e3fSGreg Roach        $user   = $request->getAttribute('user');
61a49d0e3fSGreg Roach        $params = $request->getParsedBody();
62a49d0e3fSGreg Roach
63a49d0e3fSGreg Roach        $contact_method = $params['contact_method'];
64a49d0e3fSGreg Roach        $email          = $params['email'];
65a49d0e3fSGreg Roach        $language       = $params['language'];
66a49d0e3fSGreg Roach        $real_name      = $params['real_name'];
67a49d0e3fSGreg Roach        $password       = $params['password'];
68a49d0e3fSGreg Roach        $time_zone      = $params['timezone'];
69a49d0e3fSGreg Roach        $user_name      = $params['user_name'];
70a49d0e3fSGreg Roach        $visible_online = $params['visible_online'] ?? '';
71a49d0e3fSGreg Roach
72a49d0e3fSGreg Roach        // Change the password
73a49d0e3fSGreg Roach        if ($password !== '') {
74a49d0e3fSGreg Roach            $user->setPassword($password);
75a49d0e3fSGreg Roach        }
76a49d0e3fSGreg Roach
77a49d0e3fSGreg Roach        // Change the username
78a49d0e3fSGreg Roach        if ($user_name !== $user->userName()) {
79a49d0e3fSGreg Roach            if ($this->user_service->findByUserName($user_name) === null) {
80a49d0e3fSGreg Roach                $user->setUserName($user_name);
81a49d0e3fSGreg Roach            } else {
82a49d0e3fSGreg Roach                FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'));
83a49d0e3fSGreg Roach            }
84a49d0e3fSGreg Roach        }
85a49d0e3fSGreg Roach
86a49d0e3fSGreg Roach        // Change the email
87a49d0e3fSGreg Roach        if ($email !== $user->email()) {
88a49d0e3fSGreg Roach            if ($this->user_service->findByEmail($email) === null) {
89a49d0e3fSGreg Roach                $user->setEmail($email);
90a49d0e3fSGreg Roach            } else {
91a49d0e3fSGreg Roach                FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.'));
92a49d0e3fSGreg Roach            }
93a49d0e3fSGreg Roach        }
94a49d0e3fSGreg Roach
95a49d0e3fSGreg Roach        $user
96a49d0e3fSGreg Roach            ->setRealName($real_name)
97a49d0e3fSGreg Roach            ->setPreference('contactmethod', $contact_method)
98a49d0e3fSGreg Roach            ->setPreference('language', $language)
99a49d0e3fSGreg Roach            ->setPreference('TIMEZONE', $time_zone)
100a49d0e3fSGreg Roach            ->setPreference('visibleonline', $visible_online);
101a49d0e3fSGreg Roach
102a49d0e3fSGreg Roach        if ($tree instanceof Tree) {
103a49d0e3fSGreg Roach            $rootid = $params['root_id'];
104a49d0e3fSGreg Roach            $tree->setUserPreference($user, 'rootid', $rootid);
105a49d0e3fSGreg Roach        }
106a49d0e3fSGreg Roach
107a49d0e3fSGreg Roach        // Switch to the new language now
108a49d0e3fSGreg Roach        Session::put('language', $language);
109a49d0e3fSGreg Roach
110a49d0e3fSGreg Roach        FlashMessages::addMessage(I18N::translate('The details for “%s” have been updated.', e($user->username())), 'success');
111a49d0e3fSGreg Roach
112*d98c9706SGreg Roach        return redirect(route(HomePage::class, ['tree' => $tree instanceof Tree ? $tree->name() : null]));
113a49d0e3fSGreg Roach    }
114a49d0e3fSGreg Roach}
115