Skip to content

Commit 0c58cf2

Browse files
feat: add occ command to list mail accounts
Signed-off-by: SebastianKrupinski <[email protected]>
1 parent 95dc9dc commit 0c58cf2

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ Learn more about the Nextcloud Ethical AI Rating [in our blog](https://nextcloud
8585
<command>OCA\Mail\Command\DiagnoseAccount</command>
8686
<command>OCA\Mail\Command\ExportAccount</command>
8787
<command>OCA\Mail\Command\ExportAccountThreads</command>
88+
<command>OCA\Mail\Command\ListAccounts</command>
8889
<command>OCA\Mail\Command\PredictImportance</command>
8990
<command>OCA\Mail\Command\SyncAccount</command>
9091
<command>OCA\Mail\Command\Thread</command>

lib/Command/ListAccounts.php

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
7+
* SPDX-License-Identifier: AGPL-3.0-or-later
8+
*/
9+
10+
namespace OCA\Mail\Command;
11+
12+
use OCA\Mail\Service\AccountService;
13+
use OCP\IUserManager;
14+
use Symfony\Component\Console\Command\Command;
15+
use Symfony\Component\Console\Helper\Table;
16+
use Symfony\Component\Console\Input\InputArgument;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Input\InputOption;
19+
use Symfony\Component\Console\Output\OutputInterface;
20+
21+
class ListAccounts extends Command {
22+
public const ARGUMENT_USER_ID = 'user';
23+
public const OPTION_FULL = 'full';
24+
public const OPTION_ALL = 'all';
25+
26+
public function __construct(
27+
private IUserManager $userManager,
28+
private AccountService $accountService,
29+
) {
30+
parent::__construct();
31+
}
32+
33+
protected function configure(): void {
34+
$this
35+
->setName('mail:account:list')
36+
->setDescription('List mail accounts')
37+
->addArgument(
38+
self::ARGUMENT_USER_ID,
39+
InputArgument::OPTIONAL,
40+
'User ID to list accounts for'
41+
)
42+
->addOption(
43+
self::OPTION_FULL,
44+
'f',
45+
InputOption::VALUE_NONE,
46+
'Show full account details including server configuration'
47+
)
48+
->addOption(
49+
self::OPTION_ALL,
50+
'a',
51+
InputOption::VALUE_NONE,
52+
'List accounts for all users'
53+
);
54+
}
55+
56+
protected function execute(InputInterface $input, OutputInterface $output): int {
57+
$userId = $input->getArgument(self::ARGUMENT_USER_ID);
58+
$showFull = $input->getOption(self::OPTION_FULL);
59+
$listAll = $input->getOption(self::OPTION_ALL);
60+
61+
if ($listAll) {
62+
return $this->listAllAccounts($output, $showFull);
63+
}
64+
65+
if ($userId === null) {
66+
$output->writeln('<error>Please provide a user-id or use --all to list all accounts</error>');
67+
return self::FAILURE;
68+
}
69+
70+
if (!$this->userManager->userExists($userId)) {
71+
$output->writeln("<error>User <$userId> does not exist</error>");
72+
return self::FAILURE;
73+
}
74+
75+
return $this->listUserAccounts($output, $userId, $showFull);
76+
}
77+
78+
private function listUserAccounts(OutputInterface $output, string $userId, bool $showFull): int {
79+
$accounts = $this->accountService->findByUserId($userId);
80+
81+
if (count($accounts) === 0) {
82+
$output->writeln("<info>User <$userId> has no mail accounts</info>");
83+
return self::SUCCESS;
84+
}
85+
86+
$mailAccounts = array_map(fn ($account) => $account->getMailAccount(), $accounts);
87+
88+
$output->writeln("<info>Mail accounts for user <$userId>:</info>");
89+
$this->renderAccountsTable($output, $mailAccounts, $showFull, false);
90+
91+
return self::SUCCESS;
92+
}
93+
94+
private function listAllAccounts(OutputInterface $output, bool $showFull): int {
95+
$mailAccounts = $this->accountService->getAllAcounts();
96+
97+
if (count($mailAccounts) === 0) {
98+
$output->writeln('<info>No mail accounts found</info>');
99+
return self::SUCCESS;
100+
}
101+
102+
$output->writeln('<info>All mail accounts:</info>');
103+
$this->renderAccountsTable($output, $mailAccounts, $showFull, true);
104+
105+
return self::SUCCESS;
106+
}
107+
108+
/**
109+
* @param array<MailAccount> $mailAccounts
110+
*/
111+
private function renderAccountsTable(OutputInterface $output, array $mailAccounts, bool $showFull, bool $showUserId): void {
112+
$table = new Table($output);
113+
114+
if ($showFull) {
115+
$headers = ['ID'];
116+
if ($showUserId) {
117+
$headers[] = 'User ID';
118+
}
119+
$headers = array_merge($headers, [
120+
'Email',
121+
'Name',
122+
'IMAP Host',
123+
'IMAP Port',
124+
'IMAP SSL',
125+
'SMTP Host',
126+
'SMTP Port',
127+
'SMTP SSL',
128+
'Provisioned',
129+
'Debug',
130+
]);
131+
$table->setHeaders($headers);
132+
133+
foreach ($mailAccounts as $mailAccount) {
134+
$row = [$mailAccount->getId()];
135+
if ($showUserId) {
136+
$row[] = $mailAccount->getUserId();
137+
}
138+
$row = array_merge($row, [
139+
$mailAccount->getEmail(),
140+
$mailAccount->getName(),
141+
$mailAccount->getInboundHost(),
142+
$mailAccount->getInboundPort(),
143+
$mailAccount->getInboundSslMode(),
144+
$mailAccount->getOutboundHost(),
145+
$mailAccount->getOutboundPort(),
146+
$mailAccount->getOutboundSslMode(),
147+
$mailAccount->getProvisioningId() !== null ? 'Yes' : 'No',
148+
$mailAccount->getDebug() ? 'Yes' : 'No',
149+
]);
150+
$table->addRow($row);
151+
}
152+
} else {
153+
$headers = ['ID'];
154+
if ($showUserId) {
155+
$headers[] = 'User ID';
156+
}
157+
$headers = array_merge($headers, ['Email', 'Name', 'Provisioned', 'Debug']);
158+
$table->setHeaders($headers);
159+
160+
foreach ($mailAccounts as $mailAccount) {
161+
$row = [$mailAccount->getId()];
162+
if ($showUserId) {
163+
$row[] = $mailAccount->getUserId();
164+
}
165+
$row = array_merge($row, [
166+
$mailAccount->getEmail(),
167+
$mailAccount->getName(),
168+
$mailAccount->getProvisioningId() !== null ? 'Yes' : 'No',
169+
$mailAccount->getDebug() ? 'Yes' : 'No',
170+
]);
171+
$table->addRow($row);
172+
}
173+
}
174+
175+
$table->render();
176+
}
177+
}

0 commit comments

Comments
 (0)