xref: /webtrees/app/Cli/Commands/UserList.php (revision d3aa520f698112f9a079527d6075b329bd948f1b)
1b9a4a6c6SGreg Roach<?php
2b9a4a6c6SGreg Roach
3b9a4a6c6SGreg Roach/**
4b9a4a6c6SGreg Roach * webtrees: online genealogy
5b9a4a6c6SGreg Roach * Copyright (C) 2023 webtrees development team
6b9a4a6c6SGreg Roach * This program is free software: you can redistribute it and/or modify
7b9a4a6c6SGreg Roach * it under the terms of the GNU General Public License as published by
8b9a4a6c6SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9b9a4a6c6SGreg Roach * (at your option) any later version.
10b9a4a6c6SGreg Roach * This program is distributed in the hope that it will be useful,
11b9a4a6c6SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12b9a4a6c6SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13b9a4a6c6SGreg Roach * GNU General Public License for more details.
14b9a4a6c6SGreg Roach * You should have received a copy of the GNU General Public License
15b9a4a6c6SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16b9a4a6c6SGreg Roach */
17b9a4a6c6SGreg Roach
18b9a4a6c6SGreg Roachdeclare(strict_types=1);
19b9a4a6c6SGreg Roach
20b9a4a6c6SGreg Roachnamespace Fisharebest\Webtrees\Cli\Commands;
21b9a4a6c6SGreg Roach
22b9a4a6c6SGreg Roachuse Fisharebest\Webtrees\Auth;
23b9a4a6c6SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
24b9a4a6c6SGreg Roachuse Fisharebest\Webtrees\Registry;
25b9a4a6c6SGreg Roachuse Fisharebest\Webtrees\Services\UserService;
26b9a4a6c6SGreg Roachuse Symfony\Component\Console\Command\Command;
27b9a4a6c6SGreg Roachuse Symfony\Component\Console\Helper\Table;
28b9a4a6c6SGreg Roachuse Symfony\Component\Console\Input\InputInterface;
29b9a4a6c6SGreg Roachuse Symfony\Component\Console\Output\OutputInterface;
30b9a4a6c6SGreg Roach
31b9a4a6c6SGreg Roachclass UserList extends Command
32b9a4a6c6SGreg Roach{
33b9a4a6c6SGreg Roach    public function __construct(private readonly UserService $user_service)
34b9a4a6c6SGreg Roach    {
35b9a4a6c6SGreg Roach        parent::__construct();
36b9a4a6c6SGreg Roach    }
37b9a4a6c6SGreg Roach
38b9a4a6c6SGreg Roach    protected function configure(): void
39b9a4a6c6SGreg Roach    {
40b9a4a6c6SGreg Roach        $this
41b9a4a6c6SGreg Roach            ->setName(name: 'user-list')
42b9a4a6c6SGreg Roach            ->setDescription(description: 'List users');
43b9a4a6c6SGreg Roach    }
44b9a4a6c6SGreg Roach
45b9a4a6c6SGreg Roach    protected function execute(InputInterface $input, OutputInterface $output): int
46b9a4a6c6SGreg Roach    {
47*d3aa520fSGreg Roach        $users = $this->user_service->all()->sort(callback: fn ($a, $b) => $a->id() <=> $b->id());
48b9a4a6c6SGreg Roach
49b9a4a6c6SGreg Roach        $table = new Table(output: $output);
50b9a4a6c6SGreg Roach
51bb87bb9bSGreg Roach        $table->setHeaders(headers: ['ID', 'Username', 'Real Name', 'Email', 'Admin', 'Approved', 'Verified', 'Language', 'Timezone', 'Contact', 'Registered', 'Last login']);
52b9a4a6c6SGreg Roach
53b9a4a6c6SGreg Roach        foreach ($users as $user) {
54b9a4a6c6SGreg Roach            $registered = (int) $user->getPreference(setting_name: UserInterface::PREF_TIMESTAMP_REGISTERED);
55b9a4a6c6SGreg Roach            $last_login = (int) $user->getPreference(setting_name: UserInterface::PREF_TIMESTAMP_ACTIVE);
56b9a4a6c6SGreg Roach
57b9a4a6c6SGreg Roach            if ($registered === 0) {
58b9a4a6c6SGreg Roach                $registered = 'Never';
59b9a4a6c6SGreg Roach            } else {
60b9a4a6c6SGreg Roach                $registered = Registry::timestampFactory()->make(timestamp: $registered)->format(format: 'Y-m-d H:i:s');
61b9a4a6c6SGreg Roach            }
62b9a4a6c6SGreg Roach
63b9a4a6c6SGreg Roach            if ($last_login === 0) {
64b9a4a6c6SGreg Roach                $last_login = 'Never';
65b9a4a6c6SGreg Roach            } else {
66b9a4a6c6SGreg Roach                $last_login = Registry::timestampFactory()->make(timestamp: $last_login)->format(format: 'Y-m-d H:i:s');
67b9a4a6c6SGreg Roach            }
68b9a4a6c6SGreg Roach
69b9a4a6c6SGreg Roach            $table->addRow(row: [
70b9a4a6c6SGreg Roach                $user->id(),
71b9a4a6c6SGreg Roach                $user->userName(),
72b9a4a6c6SGreg Roach                $user->realName(),
73b9a4a6c6SGreg Roach                $user->email(),
74b9a4a6c6SGreg Roach                Auth::isAdmin(user: $user) ? 'Yes' : 'No',
75b9a4a6c6SGreg Roach                $user->getPreference(setting_name: UserInterface::PREF_IS_ACCOUNT_APPROVED) ? 'Yes' : 'No',
76b9a4a6c6SGreg Roach                $user->getPreference(setting_name: UserInterface::PREF_IS_EMAIL_VERIFIED) ? 'Yes' : 'No',
77b9a4a6c6SGreg Roach                $user->getPreference(setting_name: UserInterface::PREF_LANGUAGE),
78b9a4a6c6SGreg Roach                $user->getPreference(setting_name: UserInterface::PREF_TIME_ZONE),
79bb87bb9bSGreg Roach                $user->getPreference(setting_name: UserInterface::PREF_CONTACT_METHOD),
80b9a4a6c6SGreg Roach                $registered,
81b9a4a6c6SGreg Roach                $last_login,
82b9a4a6c6SGreg Roach            ]);
83b9a4a6c6SGreg Roach        }
84b9a4a6c6SGreg Roach
85b9a4a6c6SGreg Roach        $table->render();
86b9a4a6c6SGreg Roach
87b9a4a6c6SGreg Roach        return Command::SUCCESS;
88b9a4a6c6SGreg Roach    }
89b9a4a6c6SGreg Roach}
90