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 { if (!$this->tenantContext->resolveIdentifier($tenant)) { $io->error("Tenant '{$tenant}' not found."); return Command::FAILURE; } $tenant = $this->tenantContext->requireIdentifier(); // 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->userService->createUser($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; } } }