Skip to content

Commit

Permalink
feat: add JSON to occ mail export
Browse files Browse the repository at this point in the history
Signed-off-by: Peter-J. G. <[email protected]>
  • Loading branch information
germelmann committed Jul 12, 2024
1 parent b018ffe commit 6b725ac
Showing 1 changed file with 48 additions and 9 deletions.
57 changes: 48 additions & 9 deletions lib/Command/ExportAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
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;

class ExportAccount extends Command {
public const ARGUMENT_USER_ID = 'user-id';
public const ARGUMENT_OUTPUT_FORMAT = 'output';

private AccountService $accountService;
private ICrypto $crypto;
Expand All @@ -34,22 +36,59 @@ protected function configure() {
$this->setName('mail:account:export');
$this->setDescription('Exports a user\'s IMAP account(s)');
$this->addArgument(self::ARGUMENT_USER_ID, InputArgument::REQUIRED);
$this->addOption(self::ARGUMENT_OUTPUT_FORMAT, '', InputOption::VALUE_OPTIONAL);
}

private function getAccountsData($accounts) {
$accountsData = [];

foreach ($accounts as $account) {
$accountsData[] = [
'id' => $account->getId(),
'email' => $account->getEmail(),
'name' => $account->getName(),
'provision' => [
'status' => $account->getMailAccount()->getProvisioningId() ? "set" : "none",
'id' => $account->getMailAccount()->getProvisioningId() ?: "N/A"
],
'imap' => [
'user' => $account->getMailAccount()->getInboundUser(),
'host' => $account->getMailAccount()->getInboundHost(),
'port' => $account->getMailAccount()->getInboundPort(),
'security' => $account->getMailAccount()->getInboundSslMode()
],
'smtp' => [
'user' => $account->getMailAccount()->getOutboundUser(),
'host' => $account->getMailAccount()->getOutboundHost(),
'port' => $account->getMailAccount()->getOutboundPort(),
'security' => $account->getMailAccount()->getOutboundSslMode()
]
];
}

return $accountsData;
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$userId = $input->getArgument(self::ARGUMENT_USER_ID);

$accounts = $this->accountService->findByUserId($userId);

foreach ($accounts as $account) {
$output->writeln("<info>Account " . $account->getId() . ":</info>");
$output->writeln("- E-Mail: " . $account->getEmail());
$output->writeln("- Name: " . $account->getName());
$output->writeln("- Provision: " . ($account->getMailAccount()->getProvisioningId() ? "set" : "none"). " ID: " . ($account->getMailAccount()->getProvisioningId() ? $account->getMailAccount()->getProvisioningId():"N/A"));
$output->writeln("- IMAP user: " . $account->getMailAccount()->getInboundUser());
$output->writeln("- IMAP host: " . $account->getMailAccount()->getInboundHost() . ":" . $account->getMailAccount()->getInboundPort() . ", security: " . $account->getMailAccount()->getInboundSslMode());
$output->writeln("- SMTP user: " . $account->getMailAccount()->getOutboundUser());
$output->writeln("- SMTP host: " . $account->getMailAccount()->getOutboundHost() . ":" . $account->getMailAccount()->getOutboundPort() . ", security: " . $account->getMailAccount()->getOutboundSslMode());
if ($input->getOption(self::ARGUMENT_OUTPUT_FORMAT) === 'json') {
$output->writeln(json_encode($this->getAccountsData($accounts)));
} elseif ($input->getOption(self::ARGUMENT_OUTPUT_FORMAT) === 'json_pretty') {
$output->writeln(json_encode($this->getAccountsData($accounts), JSON_PRETTY_PRINT));
} else {
foreach ($accounts as $account) {
$output->writeln("<info>Account " . $account->getId() . ":</info>");
$output->writeln("- E-Mail: " . $account->getEmail());
$output->writeln("- Name: " . $account->getName());
$output->writeln("- Provision: " . ($account->getMailAccount()->getProvisioningId() ? "set" : "none"). " ID: " . ($account->getMailAccount()->getProvisioningId() ? $account->getMailAccount()->getProvisioningId():"N/A"));
$output->writeln("- IMAP user: " . $account->getMailAccount()->getInboundUser());
$output->writeln("- IMAP host: " . $account->getMailAccount()->getInboundHost() . ":" . $account->getMailAccount()->getInboundPort() . ", security: " . $account->getMailAccount()->getInboundSslMode());
$output->writeln("- SMTP user: " . $account->getMailAccount()->getOutboundUser());
$output->writeln("- SMTP host: " . $account->getMailAccount()->getOutboundHost() . ":" . $account->getMailAccount()->getOutboundPort() . ", security: " . $account->getMailAccount()->getOutboundSslMode());
}
}

return 0;
Expand Down

0 comments on commit 6b725ac

Please sign in to comment.