diff options
author | Anna Larch <anna@nextcloud.com> | 2021-02-18 16:41:33 +0300 |
---|---|---|
committer | Anna Larch <anna@nextcloud.com> | 2021-02-19 11:41:41 +0300 |
commit | ebe98b47f7d8b89fea6356c661b54eb0548af605 (patch) | |
tree | c6e6cd259abff58fe11215905e1c68e8dea464a8 /lib | |
parent | 324ddd3e0b4cce10da8f31172b91349170d214e9 (diff) |
Add delete command to OCC
Signed-off-by: Anna Larch <anna@nextcloud.com>
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 */ |