xref: /webtrees/app/Http/RequestHandlers/AccountUpdate.php (revision d11be7027e34e3121be11cc025421873364403f9)
1a49d0e3fSGreg Roach<?php
2a49d0e3fSGreg Roach
3a49d0e3fSGreg Roach/**
4a49d0e3fSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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
3410e06497SGreg Roachuse function assert;
35a49d0e3fSGreg Roachuse function redirect;
36a49d0e3fSGreg Roachuse function route;
37a49d0e3fSGreg Roach
38a49d0e3fSGreg Roach/**
39a49d0e3fSGreg Roach * Edit user account details.
40a49d0e3fSGreg Roach */
41a49d0e3fSGreg Roachclass AccountUpdate implements RequestHandlerInterface
42a49d0e3fSGreg Roach{
43c4943cffSGreg Roach    private UserService $user_service;
44a49d0e3fSGreg Roach
45a49d0e3fSGreg Roach    /**
46a49d0e3fSGreg Roach     * AccountController constructor.
47a49d0e3fSGreg Roach     *
48a49d0e3fSGreg Roach     * @param UserService $user_service
49a49d0e3fSGreg Roach     */
50a49d0e3fSGreg Roach    public function __construct(UserService $user_service)
51a49d0e3fSGreg Roach    {
52a49d0e3fSGreg Roach        $this->user_service = $user_service;
53a49d0e3fSGreg Roach    }
54a49d0e3fSGreg Roach
55a49d0e3fSGreg Roach    /**
56a49d0e3fSGreg Roach     * @param ServerRequestInterface $request
57a49d0e3fSGreg Roach     *
58a49d0e3fSGreg Roach     * @return ResponseInterface
59a49d0e3fSGreg Roach     */
60a49d0e3fSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
61a49d0e3fSGreg Roach    {
62b55cbc6bSGreg Roach        $tree = Validator::attributes($request)->treeOptional();
63b55cbc6bSGreg Roach        $user = Validator::attributes($request)->user();
647c4add84SGreg Roach
657c4add84SGreg Roach        assert($user instanceof User);
667c4add84SGreg Roach
67748dbe15SGreg Roach        $contact_method = Validator::parsedBody($request)->string('contact-method');
68748dbe15SGreg Roach        $email          = Validator::parsedBody($request)->string('email');
69748dbe15SGreg Roach        $language       = Validator::parsedBody($request)->string('language');
70748dbe15SGreg Roach        $real_name      = Validator::parsedBody($request)->string('real_name');
71748dbe15SGreg Roach        $password       = Validator::parsedBody($request)->string('password');
72748dbe15SGreg Roach        $time_zone      = Validator::parsedBody($request)->string('timezone');
73748dbe15SGreg Roach        $user_name      = Validator::parsedBody($request)->string('user_name');
74748dbe15SGreg Roach        $visible_online = Validator::parsedBody($request)->boolean('visible-online', false);
75a49d0e3fSGreg Roach
76a49d0e3fSGreg Roach        // Change the password
77a49d0e3fSGreg Roach        if ($password !== '') {
78a49d0e3fSGreg Roach            $user->setPassword($password);
79a49d0e3fSGreg Roach        }
80a49d0e3fSGreg Roach
81a49d0e3fSGreg Roach        // Change the username
82a49d0e3fSGreg Roach        if ($user_name !== $user->userName()) {
83a49d0e3fSGreg Roach            if ($this->user_service->findByUserName($user_name) === null) {
84a49d0e3fSGreg Roach                $user->setUserName($user_name);
85a49d0e3fSGreg Roach            } else {
86a49d0e3fSGreg Roach                FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'));
87a49d0e3fSGreg Roach            }
88a49d0e3fSGreg Roach        }
89a49d0e3fSGreg Roach
90a49d0e3fSGreg Roach        // Change the email
91a49d0e3fSGreg Roach        if ($email !== $user->email()) {
92a49d0e3fSGreg Roach            if ($this->user_service->findByEmail($email) === null) {
93a49d0e3fSGreg Roach                $user->setEmail($email);
94a49d0e3fSGreg Roach            } else {
95a49d0e3fSGreg Roach                FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.'));
96a49d0e3fSGreg Roach            }
97a49d0e3fSGreg Roach        }
98a49d0e3fSGreg Roach
997c4add84SGreg Roach        $user->setRealName($real_name);
1001fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_CONTACT_METHOD, $contact_method);
1011fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_LANGUAGE, $language);
1021fe542e9SGreg Roach        $user->setPreference(UserInterface::PREF_TIME_ZONE, $time_zone);
103748dbe15SGreg Roach        $user->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, (string) $visible_online);
104a49d0e3fSGreg Roach
105a49d0e3fSGreg Roach        if ($tree instanceof Tree) {
106748dbe15SGreg Roach            $default_xref = Validator::parsedBody($request)->string('default-xref');
1071fe542e9SGreg Roach            $tree->setUserPreference($user, UserInterface::PREF_TREE_DEFAULT_XREF, $default_xref);
108a49d0e3fSGreg Roach        }
109a49d0e3fSGreg Roach
110a49d0e3fSGreg Roach        // Switch to the new language now
111a49d0e3fSGreg Roach        Session::put('language', $language);
112a49d0e3fSGreg Roach
113b4c5c807SGreg Roach        FlashMessages::addMessage(I18N::translate('The details for “%s” have been updated.', e($user->userName())), 'success');
114a49d0e3fSGreg Roach
11581bf3221SGreg Roach        return redirect(route(HomePage::class, ['tree' => $tree?->name()]));
116a49d0e3fSGreg Roach    }
117a49d0e3fSGreg Roach}
118