xref: /webtrees/app/Http/RequestHandlers/UserEditAction.php (revision ac71572d8462e396ed5a307f05b29381e49f9e6e)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2022 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\Contracts\UserInterface;
24use Fisharebest\Webtrees\FlashMessages;
25use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Services\EmailService;
28use Fisharebest\Webtrees\Services\TreeService;
29use Fisharebest\Webtrees\Services\UserService;
30use Fisharebest\Webtrees\SiteUser;
31use Fisharebest\Webtrees\User;
32use Fisharebest\Webtrees\Validator;
33use Psr\Http\Message\ResponseInterface;
34use Psr\Http\Message\ServerRequestInterface;
35use Psr\Http\Server\RequestHandlerInterface;
36
37use function route;
38
39/**
40 * Edit a user.
41 */
42class UserEditAction implements RequestHandlerInterface
43{
44    private EmailService $email_service;
45
46    private UserService $user_service;
47
48    private TreeService $tree_service;
49
50    /**
51     * UserEditAction constructor.
52     *
53     * @param EmailService $email_service
54     * @param TreeService  $tree_service
55     * @param UserService  $user_service
56     */
57    public function __construct(
58        EmailService $email_service,
59        TreeService $tree_service,
60        UserService $user_service
61    ) {
62        $this->email_service = $email_service;
63        $this->tree_service  = $tree_service;
64        $this->user_service  = $user_service;
65    }
66
67    /**
68     * @param ServerRequestInterface $request
69     *
70     * @return ResponseInterface
71     */
72    public function handle(ServerRequestInterface $request): ResponseInterface
73    {
74        $user           = Validator::attributes($request)->user();
75        $user_id        = Validator::parsedBody($request)->integer('user_id');
76        $username       = Validator::parsedBody($request)->string('username');
77        $real_name      = Validator::parsedBody($request)->string('real_name');
78        $email          = Validator::parsedBody($request)->string('email');
79        $password       = Validator::parsedBody($request)->string('password');
80        $theme          = Validator::parsedBody($request)->string('theme');
81        $language       = Validator::parsedBody($request)->string('language');
82        $timezone       = Validator::parsedBody($request)->string('timezone');
83        $contact_method = Validator::parsedBody($request)->string('contact-method');
84        $comment        = Validator::parsedBody($request)->string('comment');
85        $auto_accept    = Validator::parsedBody($request)->boolean('auto_accept', false);
86        $canadmin       = Validator::parsedBody($request)->boolean('canadmin', false);
87        $visible_online = Validator::parsedBody($request)->boolean('visible-online', false);
88        $verified       = Validator::parsedBody($request)->boolean('verified', false);
89        $approved       = Validator::parsedBody($request)->boolean('approved', false);
90
91        $edit_user = $this->user_service->find($user_id);
92
93        if ($edit_user === null) {
94            throw new HttpNotFoundException(I18N::translate('%s does not exist.', 'user_id:' . $user_id));
95        }
96
97        // We have just approved a user.  Tell them
98        if ($approved && $edit_user->getPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED) !== '1') {
99            I18N::init($edit_user->getPreference(UserInterface::PREF_LANGUAGE));
100
101            $base_url = Validator::attributes($request)->string('base_url');
102
103            $this->email_service->send(
104                new SiteUser(),
105                $edit_user,
106                Auth::user(),
107                /* I18N: %s is a server name/URL */
108                I18N::translate('New user at %s', $base_url),
109                view('emails/approve-user-text', ['user' => $edit_user, 'base_url' => $base_url]),
110                view('emails/approve-user-html', ['user' => $edit_user, 'base_url' => $base_url])
111            );
112        }
113
114        $edit_user->setRealName($real_name);
115        $edit_user->setPreference(UserInterface::PREF_THEME, $theme);
116        $edit_user->setPreference(UserInterface::PREF_LANGUAGE, $language);
117        $edit_user->setPreference(UserInterface::PREF_TIME_ZONE, $timezone);
118        $edit_user->setPreference(UserInterface::PREF_CONTACT_METHOD, $contact_method);
119        $edit_user->setPreference(UserInterface::PREF_NEW_ACCOUNT_COMMENT, $comment);
120        $edit_user->setPreference(UserInterface::PREF_AUTO_ACCEPT_EDITS, (string) $auto_accept);
121        $edit_user->setPreference(UserInterface::PREF_IS_VISIBLE_ONLINE, (string) $visible_online);
122        $edit_user->setPreference(UserInterface::PREF_IS_EMAIL_VERIFIED, (string) $verified);
123        $edit_user->setPreference(UserInterface::PREF_IS_ACCOUNT_APPROVED, (string) $approved);
124
125        if ($password !== '') {
126            $edit_user->setPassword($password);
127        }
128
129        // We cannot change our own admin status. Another admin will need to do it.
130        if ($edit_user->id() !== $user->id()) {
131            $edit_user->setPreference(UserInterface::PREF_IS_ADMINISTRATOR, $canadmin ? '1' : '');
132        }
133
134        foreach ($this->tree_service->all() as $tree) {
135            $path_length = Validator::parsedBody($request)->integer('RELATIONSHIP_PATH_LENGTH' . $tree->id(), 0);
136            $gedcom_id   = Validator::parsedBody($request)->string('gedcomid' . $tree->id(), '');
137            $can_edit    = Validator::parsedBody($request)->string('canedit' . $tree->id(), '');
138
139            // Do not allow a path length to be set if the individual ID is not
140            if ($gedcom_id === '') {
141                $path_length = 0;
142            }
143
144            $tree->setUserPreference($edit_user, UserInterface::PREF_TREE_ACCOUNT_XREF, $gedcom_id);
145            $tree->setUserPreference($edit_user, UserInterface::PREF_TREE_ROLE, $can_edit);
146            $tree->setUserPreference($edit_user, UserInterface::PREF_TREE_PATH_LENGTH, (string) $path_length);
147        }
148
149        if ($edit_user->email() !== $email && $this->user_service->findByEmail($email) instanceof User) {
150            FlashMessages::addMessage(I18N::translate('Duplicate email address. A user with that email already exists.') . $email, 'danger');
151
152            return redirect(route('admin-users-edit', ['user_id' => $edit_user->id()]));
153        }
154
155        if ($edit_user->userName() !== $username && $this->user_service->findByUserName($username) instanceof User) {
156            FlashMessages::addMessage(I18N::translate('Duplicate username. A user with that username already exists. Please choose another username.'), 'danger');
157
158            return redirect(route(UserEditPage::class, ['user_id' => $edit_user->id()]));
159        }
160
161        $edit_user
162            ->setEmail($email)
163            ->setUserName($username);
164
165        return redirect(route(UserListPage::class));
166    }
167}
168