feat: user management console commands
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -0,0 +1,128 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace KTXC\Console\User;
|
||||||
|
|
||||||
|
use KTXC\Service\TenantService;
|
||||||
|
use KTXC\Stores\UserAccountsStore;
|
||||||
|
use KTXC\Stores\UserRolesStore;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User Create Command
|
||||||
|
*
|
||||||
|
* Creates a new user account within a tenant.
|
||||||
|
*/
|
||||||
|
#[AsCommand(
|
||||||
|
name: 'user:create',
|
||||||
|
description: 'Create a new user account in a tenant',
|
||||||
|
)]
|
||||||
|
class UserCreateCommand extends Command
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly TenantService $tenantService,
|
||||||
|
private readonly UserAccountsStore $userStore,
|
||||||
|
private readonly UserRolesStore $rolesStore,
|
||||||
|
private readonly LoggerInterface $logger
|
||||||
|
) {
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configure(): void
|
||||||
|
{
|
||||||
|
$this
|
||||||
|
->addArgument('tenant', InputArgument::REQUIRED, 'Tenant identifier')
|
||||||
|
->addArgument('identity', InputArgument::REQUIRED, 'User identity (email/username)')
|
||||||
|
->addOption('label', 'l', InputOption::VALUE_REQUIRED, 'Display label for the user')
|
||||||
|
->addOption('role', 'r', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Role id(s) to assign', [])
|
||||||
|
->addOption('uid', null, InputOption::VALUE_REQUIRED, 'Explicit user id (defaults to a generated UUID)')
|
||||||
|
->addOption('disabled', null, InputOption::VALUE_NONE, 'Create the account in a disabled state')
|
||||||
|
->setHelp('This command creates a new user account in a tenant. Authentication credentials are managed separately by authentication provider modules.')
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
|
{
|
||||||
|
$io = new SymfonyStyle($input, $output);
|
||||||
|
$tenant = $input->getArgument('tenant');
|
||||||
|
$identity = $input->getArgument('identity');
|
||||||
|
$roles = $input->getOption('role');
|
||||||
|
|
||||||
|
$io->title('Create User');
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Ensure the tenant exists
|
||||||
|
if (!$this->tenantService->fetchById($tenant)) {
|
||||||
|
$io->error("Tenant '{$tenant}' not found.");
|
||||||
|
return Command::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure identity is unique within the tenant
|
||||||
|
if ($this->userStore->fetchByIdentity($tenant, $identity)) {
|
||||||
|
$io->error("User '{$identity}' already exists in tenant '{$tenant}'.");
|
||||||
|
return Command::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ensure assigned roles exist
|
||||||
|
foreach ($roles as $role) {
|
||||||
|
if (!$this->rolesStore->fetchByRid($tenant, $role)) {
|
||||||
|
$io->error("Role '{$role}' not found in tenant '{$tenant}'.");
|
||||||
|
return Command::FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$userData = [
|
||||||
|
'identity' => $identity,
|
||||||
|
'label' => $input->getOption('label') ?? $identity,
|
||||||
|
'enabled' => !$input->getOption('disabled'),
|
||||||
|
'roles' => $roles,
|
||||||
|
'profile' => [],
|
||||||
|
'settings' => [],
|
||||||
|
'provider' => null,
|
||||||
|
'provider_subject' => null,
|
||||||
|
'provider_managed_fields' => [],
|
||||||
|
];
|
||||||
|
|
||||||
|
if ($input->getOption('uid')) {
|
||||||
|
$userData['uid'] = $input->getOption('uid');
|
||||||
|
}
|
||||||
|
|
||||||
|
$user = $this->userStore->createUser($tenant, $userData);
|
||||||
|
|
||||||
|
$this->logger->info('User created via console', [
|
||||||
|
'tenant' => $tenant,
|
||||||
|
'identity' => $identity,
|
||||||
|
'uid' => $user['uid'] ?? null,
|
||||||
|
'command' => $this->getName(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$io->success("User '{$identity}' created successfully!");
|
||||||
|
$io->definitionList(
|
||||||
|
['Uid' => $user['uid'] ?? ''],
|
||||||
|
['Identity' => $user['identity'] ?? ''],
|
||||||
|
['Label' => $user['label'] ?? ''],
|
||||||
|
['Enabled' => !empty($user['enabled']) ? 'yes' : 'no'],
|
||||||
|
['Roles' => implode(', ', (array)($user['roles'] ?? []))],
|
||||||
|
);
|
||||||
|
|
||||||
|
return Command::SUCCESS;
|
||||||
|
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
$io->error('Failed to create user: ' . $e->getMessage());
|
||||||
|
$this->logger->error('User create failed', [
|
||||||
|
'tenant' => $tenant,
|
||||||
|
'identity' => $identity,
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
]);
|
||||||
|
return Command::FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace KTXC\Console\User;
|
||||||
|
|
||||||
|
use KTXC\Stores\UserAccountsStore;
|
||||||
|
use Psr\Log\LoggerInterface;
|
||||||
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
|
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;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User Delete Command
|
||||||
|
*
|
||||||
|
* Deletes a user account from a tenant.
|
||||||
|
*/
|
||||||
|
#[AsCommand(
|
||||||
|
name: 'user:delete',
|
||||||
|
description: 'Delete a user account from a tenant',
|
||||||
|
)]
|
||||||
|
class UserDeleteCommand extends Command
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly UserAccountsStore $userStore,
|
||||||
|
private readonly LoggerInterface $logger
|
||||||
|
) {
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configure(): void
|
||||||
|
{
|
||||||
|
$this
|
||||||
|
->addArgument('tenant', InputArgument::REQUIRED, 'Tenant identifier')
|
||||||
|
->addArgument('identity', InputArgument::REQUIRED, 'User identity (email/username)')
|
||||||
|
->addOption('force', 'f', InputOption::VALUE_NONE, 'Skip confirmation prompt')
|
||||||
|
->setHelp('This command deletes a user account from a tenant. Credentials stored by authentication provider modules are not removed.')
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
|
{
|
||||||
|
$io = new SymfonyStyle($input, $output);
|
||||||
|
$tenant = $input->getArgument('tenant');
|
||||||
|
$identity = $input->getArgument('identity');
|
||||||
|
$force = $input->getOption('force');
|
||||||
|
|
||||||
|
$io->title('Delete User');
|
||||||
|
|
||||||
|
try {
|
||||||
|
$user = $this->userStore->fetchByIdentity($tenant, $identity);
|
||||||
|
|
||||||
|
if (!$user) {
|
||||||
|
$io->error("User '{$identity}' not found in tenant '{$tenant}'.");
|
||||||
|
return Command::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$force && !$io->confirm("Are you sure you want to delete user '{$identity}' from tenant '{$tenant}'?", false)) {
|
||||||
|
$io->text('Delete cancelled.');
|
||||||
|
return Command::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$this->userStore->deleteUser($tenant, $user['uid'])) {
|
||||||
|
$io->error("Failed to delete user '{$identity}'.");
|
||||||
|
return Command::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->logger->info('User deleted via console', [
|
||||||
|
'tenant' => $tenant,
|
||||||
|
'identity' => $identity,
|
||||||
|
'uid' => $user['uid'],
|
||||||
|
'command' => $this->getName(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
$io->success("User '{$identity}' deleted successfully!");
|
||||||
|
|
||||||
|
return Command::SUCCESS;
|
||||||
|
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
$io->error('Failed to delete user: ' . $e->getMessage());
|
||||||
|
$this->logger->error('User delete failed', [
|
||||||
|
'tenant' => $tenant,
|
||||||
|
'identity' => $identity,
|
||||||
|
'error' => $e->getMessage(),
|
||||||
|
]);
|
||||||
|
return Command::FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace KTXC\Console\User;
|
||||||
|
|
||||||
|
use KTXC\Service\TenantService;
|
||||||
|
use KTXC\Stores\UserAccountsStore;
|
||||||
|
use Symfony\Component\Console\Attribute\AsCommand;
|
||||||
|
use Symfony\Component\Console\Command\Command;
|
||||||
|
use Symfony\Component\Console\Input\InputArgument;
|
||||||
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* User List Command
|
||||||
|
*
|
||||||
|
* Lists all user accounts in a tenant.
|
||||||
|
*/
|
||||||
|
#[AsCommand(
|
||||||
|
name: 'user:list',
|
||||||
|
description: 'List user accounts in a tenant',
|
||||||
|
)]
|
||||||
|
class UserListCommand extends Command
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
private readonly TenantService $tenantService,
|
||||||
|
private readonly UserAccountsStore $userStore
|
||||||
|
) {
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function configure(): void
|
||||||
|
{
|
||||||
|
$this
|
||||||
|
->addArgument('tenant', InputArgument::REQUIRED, 'Tenant identifier')
|
||||||
|
->setHelp('This command lists all user accounts in a tenant.')
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||||
|
{
|
||||||
|
$io = new SymfonyStyle($input, $output);
|
||||||
|
$tenant = $input->getArgument('tenant');
|
||||||
|
|
||||||
|
$io->title("Users in tenant '{$tenant}'");
|
||||||
|
|
||||||
|
try {
|
||||||
|
if (!$this->tenantService->fetchById($tenant)) {
|
||||||
|
$io->error("Tenant '{$tenant}' not found.");
|
||||||
|
return Command::FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
$users = $this->userStore->listUsers($tenant);
|
||||||
|
|
||||||
|
if (empty($users)) {
|
||||||
|
$io->text('No users found.');
|
||||||
|
return Command::SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
$rows = [];
|
||||||
|
foreach ($users as $user) {
|
||||||
|
$rows[] = [
|
||||||
|
$user['uid'] ?? '',
|
||||||
|
$user['identity'] ?? '',
|
||||||
|
$user['label'] ?? '',
|
||||||
|
!empty($user['enabled']) ? 'yes' : 'no',
|
||||||
|
implode(', ', (array)($user['roles'] ?? [])),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$io->table(['Uid', 'Identity', 'Label', 'Enabled', 'Roles'], $rows);
|
||||||
|
$io->text(sprintf('Total: %d user(s)', count($rows)));
|
||||||
|
|
||||||
|
return Command::SUCCESS;
|
||||||
|
|
||||||
|
} catch (\Throwable $e) {
|
||||||
|
$io->error('Failed to list users: ' . $e->getMessage());
|
||||||
|
return Command::FAILURE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -99,12 +99,18 @@ class Module extends ModuleInstanceAbstract implements ModuleConsoleInterface, M
|
|||||||
public function registerCI(): array
|
public function registerCI(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
\KTXC\Console\ModuleListCommand::class,
|
\KTXC\Console\Module\ModuleListCommand::class,
|
||||||
\KTXC\Console\ModuleEnableCommand::class,
|
\KTXC\Console\Module\ModuleEnableCommand::class,
|
||||||
\KTXC\Console\ModuleDisableCommand::class,
|
\KTXC\Console\Module\ModuleDisableCommand::class,
|
||||||
\KTXC\Console\ModuleInstallCommand::class,
|
\KTXC\Console\Module\ModuleInstallCommand::class,
|
||||||
\KTXC\Console\ModuleUninstallCommand::class,
|
\KTXC\Console\Module\ModuleUninstallCommand::class,
|
||||||
\KTXC\Console\ModuleUpgradeCommand::class,
|
\KTXC\Console\Module\ModuleUpgradeCommand::class,
|
||||||
|
\KTXC\Console\Tenant\TenantCreateCommand::class,
|
||||||
|
\KTXC\Console\Tenant\TenantListCommand::class,
|
||||||
|
\KTXC\Console\Tenant\TenantDeleteCommand::class,
|
||||||
|
\KTXC\Console\User\UserCreateCommand::class,
|
||||||
|
\KTXC\Console\User\UserListCommand::class,
|
||||||
|
\KTXC\Console\User\UserDeleteCommand::class,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user