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