summaryrefslogtreecommitdiff
path: root/app/Cli/Commands/UserEdit.php
blob: 1139a5a24f9d0325d684af45117d5ce088463f95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
<?php

/**
 * webtrees: online genealogy
 * Copyright (C) 2025 webtrees development team
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

declare(strict_types=1);

namespace Fisharebest\Webtrees\Cli\Commands;

use Fisharebest\Webtrees\Contracts\UserInterface;
use Fisharebest\Webtrees\Services\MessageService;
use Fisharebest\Webtrees\Services\UserService;
use Fisharebest\Webtrees\Site;
use Fisharebest\Webtrees\User;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

final class UserEdit extends AbstractCommand
{
    public function __construct(private readonly UserService $user_service)
    {
        parent::__construct();
    }

    protected function configure(): void
    {
        $this
            ->setName(name: 'user')
            ->setDescription(description: 'Create/delete/edit a user')
            ->addArgument(name: 'user-name', mode: InputArgument::REQUIRED, description: 'The username')
            ->addOption(name: 'create', mode: InputOption::VALUE_NONE, description: 'Create a new user')
            ->addOption(name: 'delete', mode: InputOption::VALUE_NONE, description: 'Delete an existing user')
            ->addOption(name: 'real-name', mode: InputOption::VALUE_REQUIRED, description: 'Set the real name of the user')
            ->addOption(name: 'email', mode: InputOption::VALUE_REQUIRED, description: 'Set the email of the user')
            ->addOption(name: 'password', mode: InputOption::VALUE_REQUIRED, description: 'Set the password of the user');
    }

    protected function execute(InputInterface $input, OutputInterface $output): int
    {
        $io = new SymfonyStyle(input: $input, output: $output);

        $user_name = $this->stringArgument(input: $input, name: 'user-name');
        $real_name = $this->stringOption(input: $input, name: 'real-name');
        $email     = $this->stringOption(input: $input, name: 'email');
        $password  = $this->stringOption(input: $input, name: 'password');
        $create    = $this->boolOption(input: $input, name: 'create');
        $delete    = $this->boolOption(input: $input, name: 'delete');

        if ($user_name === '') {
            $io->error(message: 'The user- name cannot be empty.');

            return Command::INVALID;
        }

        if ($delete && $create) {
            $io->error(message: 'Invalid options: cannot use --delete and --create at the same time.');

            return Command::INVALID;
        }

        if ($delete && $real_name !== '') {
            $io->error(message: 'Invalid options: cannot use --delete and --real-name at the same time.');

            return Command::INVALID;
        }

        if ($delete && $email !== '') {
            $io->error(message: 'Invalid options: cannot use --delete and --email at the same time.');

            return Command::INVALID;
        }

        if ($delete && $password !== '') {
            $io->error(message: 'Invalid options: cannot use --delete and --password at the same time.');

            return Command::INVALID;
        }

        $user = $this->user_service->all()->first(callback: static fn (User $user): bool => $user->userName() === $user_name);

        if ($create) {
            if ($user instanceof User) {
                $io->error(message: 'A user with the username "' . $user_name . '" already exists.');

                return Command::FAILURE;
            }

            if ($real_name === '') {
                $io->error(message: 'Invalid options: --real-name is required when using --create.');

                return Command::FAILURE;
            }

            if ($email === '') {
                $io->error(message: 'Invalid options: --email is required when using --create.');

                return Command::FAILURE;
            }

            if ($password === '') {
                $password = bin2hex(string: random_bytes(length: 8));
                $io->info(message: 'No password specified. Using a random password ‘' . $password . '’.');
            }

            $user = $this->user_service->create(
                user_name: $user_name,
                real_name: $real_name,
                email: $email,
                password: $password,
            );

            // Some preferences need to be set for all users.
            $user->setPreference(
                setting_name: UserInterface::PREF_LANGUAGE,
                setting_value: 'en-US',
            );
            $user->setPreference(
                setting_name: UserInterface::PREF_TIME_ZONE,
                setting_value: Site::getPreference('TIMEZONE'),
            );
            $user->setPreference(
                setting_name: UserInterface::PREF_IS_EMAIL_VERIFIED,
                setting_value: '1',
            );
            $user->setPreference(
                setting_name: UserInterface::PREF_IS_ACCOUNT_APPROVED,
                setting_value: '1',
            );
            $user->setPreference(
                setting_name: UserInterface::PREF_CONTACT_METHOD,
                setting_value: MessageService::CONTACT_METHOD_INTERNAL_AND_EMAIL,
            );
            $user->setPreference(
                setting_name: UserInterface::PREF_IS_VISIBLE_ONLINE,
                setting_value: '1',
            );
            $io->info(message: 'User ‘' . $user_name . '’ was created.  Account set to approved and verified.');

            return self::SUCCESS;
        }

        if ($user === null) {
            $io->error(message: 'User ‘' . $user_name . '’ does not exist.');

            return Command::FAILURE;
        }

        if ($delete) {
            $this->user_service->delete(user: $user);
            $io->success(message: 'User ‘' . $user_name . '’ was deleted..');

            return self::SUCCESS;
        }

        if ($real_name === '' && $email === '' && $password === '') {
            $io->info(message: 'Nothing to do. Specify --real-name, --email or --password.');

            return Command::INVALID;
        }

        if ($real_name !== '') {
            $user->setRealName($real_name);
            $io->info(message: 'Real name set to ‘' . $real_name . '’.');
        }

        if ($email !== '') {
            $user->setEmail($email);
            $io->info(message: 'E-mail set to ‘' . $email . '’.');
        }

        if ($password !== '') {
            $user->setPassword($password);
            $io->info(message: 'Password set to ‘' . $password . '’.');
        }

        return self::SUCCESS;
    }
}