diff --git a/appinfo/info.xml b/appinfo/info.xml index 059adc950c..63947ac0b7 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -85,6 +85,7 @@ Learn more about the Nextcloud Ethical AI Rating [in our blog](https://nextcloud OCA\Mail\Command\DiagnoseAccount OCA\Mail\Command\ExportAccount OCA\Mail\Command\ExportAccountThreads + OCA\Mail\Command\ListAccounts OCA\Mail\Command\PredictImportance OCA\Mail\Command\SyncAccount OCA\Mail\Command\Thread diff --git a/lib/Command/ListAccounts.php b/lib/Command/ListAccounts.php new file mode 100644 index 0000000000..cb76286e00 --- /dev/null +++ b/lib/Command/ListAccounts.php @@ -0,0 +1,177 @@ +setName('mail:account:list') + ->setDescription('List mail accounts') + ->addArgument( + self::ARGUMENT_USER_ID, + InputArgument::OPTIONAL, + 'User ID to list accounts for' + ) + ->addOption( + self::OPTION_FULL, + 'f', + InputOption::VALUE_NONE, + 'Show full account details including server configuration' + ) + ->addOption( + self::OPTION_ALL, + 'a', + InputOption::VALUE_NONE, + 'List accounts for all users' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $userId = $input->getArgument(self::ARGUMENT_USER_ID); + $showFull = $input->getOption(self::OPTION_FULL); + $listAll = $input->getOption(self::OPTION_ALL); + + if ($listAll) { + return $this->listAllAccounts($output, $showFull); + } + + if ($userId === null) { + $output->writeln('Please provide a user-id or use --all to list all accounts'); + return self::FAILURE; + } + + if (!$this->userManager->userExists($userId)) { + $output->writeln("User <$userId> does not exist"); + return self::FAILURE; + } + + return $this->listUserAccounts($output, $userId, $showFull); + } + + private function listUserAccounts(OutputInterface $output, string $userId, bool $showFull): int { + $accounts = $this->accountService->findByUserId($userId); + + if (count($accounts) === 0) { + $output->writeln("User <$userId> has no mail accounts"); + return self::SUCCESS; + } + + $mailAccounts = array_map(fn ($account) => $account->getMailAccount(), $accounts); + + $output->writeln("Mail accounts for user <$userId>:"); + $this->renderAccountsTable($output, $mailAccounts, $showFull, false); + + return self::SUCCESS; + } + + private function listAllAccounts(OutputInterface $output, bool $showFull): int { + $mailAccounts = $this->accountService->getAllAcounts(); + + if (count($mailAccounts) === 0) { + $output->writeln('No mail accounts found'); + return self::SUCCESS; + } + + $output->writeln('All mail accounts:'); + $this->renderAccountsTable($output, $mailAccounts, $showFull, true); + + return self::SUCCESS; + } + + /** + * @param array $mailAccounts + */ + private function renderAccountsTable(OutputInterface $output, array $mailAccounts, bool $showFull, bool $showUserId): void { + $table = new Table($output); + + if ($showFull) { + $headers = ['ID']; + if ($showUserId) { + $headers[] = 'User ID'; + } + $headers = array_merge($headers, [ + 'Email', + 'Name', + 'IMAP Host', + 'IMAP Port', + 'IMAP SSL', + 'SMTP Host', + 'SMTP Port', + 'SMTP SSL', + 'Provisioned', + 'Debug', + ]); + $table->setHeaders($headers); + + foreach ($mailAccounts as $mailAccount) { + $row = [$mailAccount->getId()]; + if ($showUserId) { + $row[] = $mailAccount->getUserId(); + } + $row = array_merge($row, [ + $mailAccount->getEmail(), + $mailAccount->getName(), + $mailAccount->getInboundHost(), + $mailAccount->getInboundPort(), + $mailAccount->getInboundSslMode(), + $mailAccount->getOutboundHost(), + $mailAccount->getOutboundPort(), + $mailAccount->getOutboundSslMode(), + $mailAccount->getProvisioningId() !== null ? 'Yes' : 'No', + $mailAccount->getDebug() ? 'Yes' : 'No', + ]); + $table->addRow($row); + } + } else { + $headers = ['ID']; + if ($showUserId) { + $headers[] = 'User ID'; + } + $headers = array_merge($headers, ['Email', 'Name', 'Provisioned', 'Debug']); + $table->setHeaders($headers); + + foreach ($mailAccounts as $mailAccount) { + $row = [$mailAccount->getId()]; + if ($showUserId) { + $row[] = $mailAccount->getUserId(); + } + $row = array_merge($row, [ + $mailAccount->getEmail(), + $mailAccount->getName(), + $mailAccount->getProvisioningId() !== null ? 'Yes' : 'No', + $mailAccount->getDebug() ? 'Yes' : 'No', + ]); + $table->addRow($row); + } + } + + $table->render(); + } +}