xref: /webtrees/app/Cli/Commands/UserList.php (revision b9a4a6c608ac9f9f3bf6c12fbe1fbe871f30fedb)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\Cli\Commands;
21
22use Composer\Console\Input\InputOption;
23use Fisharebest\Webtrees\Auth;
24use Fisharebest\Webtrees\Contracts\UserInterface;
25use Fisharebest\Webtrees\DB;
26use Fisharebest\Webtrees\Registry;
27use Fisharebest\Webtrees\Services\UserService;
28use Symfony\Component\Console\Command\Command;
29use Symfony\Component\Console\Helper\Table;
30use Symfony\Component\Console\Input\InputArgument;
31use Symfony\Component\Console\Input\InputInterface;
32use Symfony\Component\Console\Output\OutputInterface;
33use Symfony\Component\Console\Style\SymfonyStyle;
34
35use function bin2hex;
36use function random_bytes;
37
38class UserList extends Command
39{
40    public function __construct(private readonly UserService $user_service)
41    {
42        parent::__construct();
43    }
44
45    protected function configure(): void
46    {
47        $this
48            ->setName(name: 'user-list')
49            ->setDescription(description: 'List users');
50    }
51
52    protected function execute(InputInterface $input, OutputInterface $output): int
53    {
54        $io = new SymfonyStyle(input: $input, output: $output);
55
56        $users = $this->user_service->all()->sort(fn($a, $b) => $a->id() <=> $b->id());
57
58        $table = new Table(output: $output);
59
60        $table->setHeaders(headers: ['ID', 'Username', 'Real Name', 'Email', 'Admin', 'Approved', 'Verified', 'Language', 'Time zone', 'Registered', 'Last login']);
61
62        foreach ($users as $user) {
63            $registered = (int) $user->getPreference(setting_name: UserInterface::PREF_TIMESTAMP_REGISTERED);
64            $last_login = (int) $user->getPreference(setting_name: UserInterface::PREF_TIMESTAMP_ACTIVE);
65
66            if ($registered === 0) {
67                $registered = 'Never';
68            } else {
69                $registered = Registry::timestampFactory()->make(timestamp: $registered)->format(format: 'Y-m-d H:i:s');
70            }
71
72            if ($last_login === 0) {
73                $last_login = 'Never';
74            } else {
75                $last_login = Registry::timestampFactory()->make(timestamp: $last_login)->format(format: 'Y-m-d H:i:s');
76            }
77
78            $table->addRow(row: [
79                $user->id(),
80                $user->userName(),
81                $user->realName(),
82                $user->email(),
83                Auth::isAdmin(user: $user) ? 'Yes' : 'No',
84                $user->getPreference(setting_name: UserInterface::PREF_IS_ACCOUNT_APPROVED) ? 'Yes' : 'No',
85                $user->getPreference(setting_name: UserInterface::PREF_IS_EMAIL_VERIFIED) ? 'Yes' : 'No',
86                $user->getPreference(setting_name: UserInterface::PREF_LANGUAGE),
87                $user->getPreference(setting_name: UserInterface::PREF_TIME_ZONE),
88                $registered,
89                $last_login,
90            ]);
91        }
92
93        $table->render();
94
95        return Command::SUCCESS;
96    }
97}
98