xref: /webtrees/app/Http/RequestHandlers/AccountUpdate.php (revision b4c5c807eb501a4b2f3da3cb8eb38981bf4a3a25)
1a49d0e3fSGreg Roach<?php
2a49d0e3fSGreg Roach
3a49d0e3fSGreg Roach/**
4a49d0e3fSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16a49d0e3fSGreg Roach */
17a49d0e3fSGreg Roach
18a49d0e3fSGreg Roachdeclare(strict_types=1);
19a49d0e3fSGreg Roach
20a49d0e3fSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21a49d0e3fSGreg Roach
221fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
23a49d0e3fSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
24a49d0e3fSGreg Roachuse Fisharebest\Webtrees\I18N;
25a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
26a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Session;
27a49d0e3fSGreg Roachuse Fisharebest\Webtrees\Tree;
287c4add84SGreg Roachuse Fisharebest\Webtrees\User;
29b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
30a49d0e3fSGreg Roachuse Psr\Http\Message\ResponseInterface;
31a49d0e3fSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
32a49d0e3fSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
33a49d0e3fSGreg Roach
34a49d0e3fSGreg Roachuse function redirect;
35a49d0e3fSGreg Roachuse function route;
36a49d0e3fSGreg Roach
37a49d0e3fSGreg Roach/**
38a49d0e3fSGreg Roach * Edit user account details.
39a49d0e3fSGreg Roach */
40a49d0e3fSGreg Roachclass AccountUpdate implements RequestHandlerInterface
41a49d0e3fSGreg Roach{
42c4943cffSGreg Roach    private UserService $user_service;
43a49d0e3fSGreg Roach
44a49d0e3fSGreg Roach    /**
45a49d0e3fSGreg Roach     * AccountController constructor.
46a49d0e3fSGreg Roach     *
47a49d0e3fSGreg Roach     * @param UserService $user_service
48a49d0e3fSGreg Roach     */
49a49d0e3fSGreg Roach    public function __construct(UserService $user_service)
50a49d0e3fSGreg Roach    {
51a49d0e3fSGreg Roach        $this->user_service = $user_service;
52a49d0e3fSGreg Roach    }
53a49d0e3fSGreg Roach
54a49d0e3fSGreg Roach    /**
55a49d0e3fSGreg Roach     * @param ServerRequestInterface $request
56a49d0e3fSGreg Roach     *
57a49d0e3fSGreg Roach     * @return ResponseInterface
58a49d0e3fSGreg Roach     */
59a49d0e3fSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
60a49d0e3fSGreg Roach    {
61b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->treeOptional();
62b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
637c4add84SGreg Roach
647c4add84SGreg Roach        assert($user instanceof User);
657c4add84SGreg Roach
66b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
67a49d0e3fSGreg Roach
687c4add84SGreg Roach        $contact_method = $params['contact-method'];
69a49d0e3fSGreg Roach        $email          = $params['email'];
70a49d0e3fSGreg Roach        $language       = $params['language'];
71a49d0e3fSGreg Roach        $real_name      = $params['real_name'];
72a49d0e3fSGreg Roach        $password       = $params['password'];
73a49d0e3fSGreg Roach        $time_zone      = $params['timezone'];
74a49d0e3fSGreg Roach        $user_name      = $params['user_name'];
757c4add84SGreg Roach        $visible_online = $params['visible-online'] ?? '';
76a49d0e3fSGreg Roach
77a49d0e3fSGreg Roach        // Change the password
78a49d0e3fSGreg Roach        if ($password !== '') {
79a49d0e3fSGreg Roach            $user->setPassword($password);
80a49d0e3fSGreg Roach        }
81a49d0e3fSGreg Roach
82a49d0e3fSGreg Roach        // Change the username
83a49d0e3fSGreg Roach        if ($user_name !== $user->userName()) {
84a49d0e3fSGreg Roach            if ($this->user_service->findByUserName($user_name) === null) {
85a49d0e3fSGreg Roach                $user->setUserName($user_name);
86a49d0e3fSGreg Roach            } else {
87a49d0e3fSGreg Roach                FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'));
88a49d0e3fSGreg Roach            }
89a49d0e3fSGreg Roach        }
90a49d0e3fSGreg Roach
91a49d0e3fSGreg Roach        // Change the email
92a49d0e3fSGreg Roach        if ($email !== $user->email()) {
93a49d0e3fSGreg Roach            if ($this->user_service->findByEmail($email) === null) {
94a49d0e3fSGreg Roach                $user->setEmail($email);
95a49d0e3fSGreg Roach            } else {
96a49d0e3fSGreg Roach                FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.'));
97a49d0e3fSGreg Roach            }
98a49d0e3fSGreg Roach        }
99a49d0e3fSGreg Roach
1007c4add84SGreg Roach        $user->setRealName($real_name);
1011fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_CONTACT_METHOD, $contact_method);
1021fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_LANGUAGE, $language);
1031fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_TIME_ZONE, $time_zone);
1041fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, $visible_online);
105a49d0e3fSGreg Roach
106a49d0e3fSGreg Roach        if ($tree instanceof Tree) {
1077c4add84SGreg Roach            $default_xref = $params['default-xref'];
1081fe542e9SGreg Roach            $tree->setUserPreference($user, UserInterface::PREF_TREE_DEFAULT_XREF, $default_xref);
109a49d0e3fSGreg Roach        }
110a49d0e3fSGreg Roach
111a49d0e3fSGreg Roach        // Switch to the new language now
112a49d0e3fSGreg Roach        Session::put('language', $language);
113a49d0e3fSGreg Roach
114*b4c5c807SGreg Roach        FlashMessages::addMessage(I18N::translate('The details for “%s” have been updated.', e($user->userName())), 'success');
115a49d0e3fSGreg Roach
116d98c9706SGreg Roach        return redirect(route(HomePage::class, ['tree' => $tree instanceof Tree ? $tree->name() : null]));
117a49d0e3fSGreg Roach    }
118a49d0e3fSGreg Roach}
119