diff options
author | Christoph Wurst <ChristophWurst@users.noreply.github.com> | 2021-02-19 12:03:16 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-19 12:03:16 +0300 |
commit | b2747ba1a9a3b013585a11dc999acc4e7a46802f (patch) | |
tree | 914ef8723d4b4691e94b48335a2bc679da928b07 /lib | |
parent | 026bfc467ad6170b9c09292a9acf49b7e60211eb (diff) | |
parent | ebe98b47f7d8b89fea6356c661b54eb0548af605 (diff) |
Merge pull request #4556 from nextcloud/enhancement/add-feature-occ-mail-account-delete
Add delete command to OCC
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Command/DeleteAccount.php | 92 | ||||
-rw-r--r-- | lib/Service/AccountService.php | 15 |
2 files changed, 107 insertions, 0 deletions
diff --git a/lib/Command/DeleteAccount.php b/lib/Command/DeleteAccount.php new file mode 100644 index 000000000..c32b41fa0 --- /dev/null +++ b/lib/Command/DeleteAccount.php @@ -0,0 +1,92 @@ +<?php + +declare(strict_types=1); + +/** + * @author Anna Larch <anna.larch@nextcloud.com> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +namespace OCA\Mail\Command; + +use OCA\Mail\Account; +use Psr\Log\LoggerInterface; +use OCA\Mail\Service\AccountService; +use OCA\Mail\Exception\ClientException; +use OCP\AppFramework\Db\DoesNotExistException; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Input\InputArgument; +use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Output\OutputInterface; + +class DeleteAccount extends Command { + public const ARGUMENT_ACCOUNT_ID = 'account-id'; + + /** @var AccountService */ + private $accountService; + + /** @var LoggerInterface */ + private $logger; + + public function __construct(AccountService $service, + LoggerInterface $logger) { + parent::__construct(); + + $this->accountService = $service; + $this->logger = $logger; + } + + /** + * @return void + */ + protected function configure() { + $this->setName('mail:account:delete'); + $this->setDescription('Delete an IMAP account'); + $this->addArgument(self::ARGUMENT_ACCOUNT_ID, InputArgument::REQUIRED); + } + + protected function execute(InputInterface $input, OutputInterface $output): int { + $accountId = (int)$input->getArgument(self::ARGUMENT_ACCOUNT_ID); + + try { + $account = $this->accountService->findById($accountId); + } catch (DoesNotExistException $e) { + $output->writeLn('<error>This account does not exist</error>'); + return 1; + } + $output->writeLn("<info>Found account with email: " . $account->getEmail() . "</info>"); + + if ($account->getMailAccount()->getProvisioned() === true) { + $output->writeLn('<error>This is a provisioned account which can not be deleted from CLI. Use the Provisioning UI instead.</error>'); + return 2; + } + $output->writeLn("<info>Deleting " . $account->getEmail() . "</info>"); + $this->delete($account, $output); + + return 0; + } + + private function delete(Account $account, OutputInterface $output): void { + $id = $account->getId(); + try { + $this->accountService->deleteByAccountId($account->getId()); + } catch (ClientException $e) { + throw $e; + } + $output->writeLn("<info>Deleted account $id </info>"); + } +} diff --git a/lib/Service/AccountService.php b/lib/Service/AccountService.php index 90766e98c..13462ff2d 100644 --- a/lib/Service/AccountService.php +++ b/lib/Service/AccountService.php @@ -126,6 +126,21 @@ class AccountService { } /** + * @param int $accountId + * + * @throws ClientException + */ + public function deleteByAccountId(int $accountId): void { + try { + $mailAccount = $this->mapper->findById($accountId); + } catch (DoesNotExistException $e) { + throw new ClientException("Account $accountId does not exist", 0, $e); + } + $this->aliasesService->deleteAll($accountId); + $this->mapper->delete($mailAccount); + } + + /** * @param MailAccount $newAccount * @return MailAccount */ |