feat: console role management
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace KTXC\Console\Role;
|
||||
|
||||
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\Output\OutputInterface;
|
||||
use Symfony\Component\Console\Style\SymfonyStyle;
|
||||
|
||||
/**
|
||||
* Role Revoke Command
|
||||
*
|
||||
* Revokes a role from a user account.
|
||||
*/
|
||||
#[AsCommand(
|
||||
name: 'role:revoke',
|
||||
description: 'Revoke a role from a user',
|
||||
)]
|
||||
class RoleRevokeCommand 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)')
|
||||
->addArgument('rid', InputArgument::REQUIRED, 'Role id to revoke')
|
||||
->setHelp('This command revokes a role from a user account.')
|
||||
;
|
||||
}
|
||||
|
||||
protected function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
$io = new SymfonyStyle($input, $output);
|
||||
$tenant = $input->getArgument('tenant');
|
||||
$identity = $input->getArgument('identity');
|
||||
$rid = $input->getArgument('rid');
|
||||
|
||||
$io->title('Revoke Role');
|
||||
|
||||
try {
|
||||
$user = $this->userStore->fetchByIdentity($tenant, $identity);
|
||||
if (!$user) {
|
||||
$io->error("User '{$identity}' not found in tenant '{$tenant}'.");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$roles = (array)($user['roles'] ?? []);
|
||||
if (!in_array($rid, $roles, true)) {
|
||||
$io->text("User '{$identity}' does not have role '{$rid}'.");
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
$roles = array_values(array_filter($roles, fn($r) => $r !== $rid));
|
||||
|
||||
if (!$this->userStore->updateUser($tenant, $user['uid'], ['roles' => $roles])) {
|
||||
$io->error("Failed to revoke role '{$rid}' from '{$identity}'.");
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
$this->logger->info('Role revoked via console', [
|
||||
'tenant' => $tenant,
|
||||
'identity' => $identity,
|
||||
'rid' => $rid,
|
||||
'command' => $this->getName(),
|
||||
]);
|
||||
|
||||
$io->success("Role '{$rid}' revoked from '{$identity}' successfully!");
|
||||
|
||||
return Command::SUCCESS;
|
||||
|
||||
} catch (\Throwable $e) {
|
||||
$io->error('Failed to revoke role: ' . $e->getMessage());
|
||||
$this->logger->error('Role revoke failed', [
|
||||
'tenant' => $tenant,
|
||||
'identity' => $identity,
|
||||
'rid' => $rid,
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user