50 lines
1.5 KiB
PHP
50 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Console;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
/**
|
|
* The accounts of the manager, with their groups. `--json` for scripts and agents.
|
|
*
|
|
* php artisan manager:users [--json]
|
|
*/
|
|
class UserListCommand extends Command
|
|
{
|
|
protected $signature = 'manager:users {--json : Print JSON}';
|
|
|
|
protected $description = 'List the accounts of the manager with their groups';
|
|
|
|
public function handle(): int
|
|
{
|
|
$model = config('manager.user_model');
|
|
$rows = [];
|
|
|
|
foreach ($model::query()->with('managerGroups')->orderBy('id')->get() as $user) {
|
|
$primary = $user->managerPrimaryGroup();
|
|
|
|
$rows[] = [
|
|
'id' => $user->getKey(),
|
|
'name' => (string) $user->name,
|
|
'email' => (string) $user->email,
|
|
'super_admin' => $user->isManagerSuperAdmin(),
|
|
'primary_group' => $primary?->name,
|
|
'groups' => $user->managerGroups->pluck('name')->sort()->values()->all(),
|
|
];
|
|
}
|
|
|
|
if ($this->option('json')) {
|
|
$this->line(json_encode($rows, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
|
|
$this->table(['Id', 'Name', 'Email', 'Super admin', 'Primary group', 'Groups'], array_map(
|
|
fn ($row) => [$row['id'], $row['name'], $row['email'], $row['super_admin'] ? 'yes' : '', $row['primary_group'] ?? '', implode(', ', $row['groups'])],
|
|
$rows,
|
|
));
|
|
|
|
return self::SUCCESS;
|
|
}
|
|
}
|