Skip to content
Snippets Groups Projects
Unverified Commit b2747ba1 authored by Christoph Wurst's avatar Christoph Wurst Committed by GitHub
Browse files

Merge pull request #4556 from nextcloud/enhancement/add-feature-occ-mail-account-delete

Add delete command to OCC
parents 026bfc46 ebe98b47
No related branches found
No related tags found
No related merge requests found
...@@ -48,6 +48,7 @@ ...@@ -48,6 +48,7 @@
<commands> <commands>
<command>OCA\Mail\Command\CleanUp</command> <command>OCA\Mail\Command\CleanUp</command>
<command>OCA\Mail\Command\CreateAccount</command> <command>OCA\Mail\Command\CreateAccount</command>
<command>OCA\Mail\Command\DeleteAccount</command>
<command>OCA\Mail\Command\DiagnoseAccount</command> <command>OCA\Mail\Command\DiagnoseAccount</command>
<command>OCA\Mail\Command\ExportAccount</command> <command>OCA\Mail\Command\ExportAccount</command>
<command>OCA\Mail\Command\ExportAccountThreads</command> <command>OCA\Mail\Command\ExportAccountThreads</command>
......
<?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>");
}
}
...@@ -125,6 +125,21 @@ class AccountService { ...@@ -125,6 +125,21 @@ class AccountService {
$this->mapper->delete($mailAccount); $this->mapper->delete($mailAccount);
} }
/**
* @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 * @param MailAccount $newAccount
* @return MailAccount * @return MailAccount
......
<?php
declare(strict_types=1);
/**
* @author Anna Larch <anna.larch@nextcloud.com>
*
* Mail
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Mail\Tests\Unit\Command;
use ChristophWurst\Nextcloud\Testing\TestCase;
use OCA\Mail\Command\DeleteAccount;
use OCA\Mail\Service\AccountService;
use Psr\Log\LoggerInterface;
class DeleteAccountTest extends TestCase {
private $accountService;
private $logger;
private $command;
private $args = [
'account-id',
];
protected function setUp(): void {
parent::setUp();
$this->accountService = $this->getMockBuilder(AccountService::class)
->disableOriginalConstructor()
->getMock();
$this->logger = $this->getMockBuilder(LoggerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->command = new DeleteAccount($this->accountService, $this->logger);
}
public function testName() {
$this->assertSame('mail:account:delete', $this->command->getName());
}
public function testDescription() {
$this->assertSame('Delete an IMAP account', $this->command->getDescription());
}
public function testArguments() {
$actual = $this->command->getDefinition()->getArguments();
foreach ($actual as $actArg) {
$this->assertTrue($actArg->isRequired());
$this->assertTrue(in_array($actArg->getName(), $this->args));
}
}
}
...@@ -106,6 +106,21 @@ class AccountServiceTest extends TestCase { ...@@ -106,6 +106,21 @@ class AccountServiceTest extends TestCase {
$this->assertEquals($expected, $actual); $this->assertEquals($expected, $actual);
} }
public function testFindById() {
$accountId = 123;
$this->mapper->expects($this->once())
->method('findById')
->with($accountId)
->will($this->returnValue($this->account1));
$expected = new Account($this->account1);
$actual = $this->accountService->findById($accountId);
$this->assertEquals($expected, $actual);
}
public function testDelete() { public function testDelete() {
$accountId = 33; $accountId = 33;
...@@ -120,6 +135,20 @@ class AccountServiceTest extends TestCase { ...@@ -120,6 +135,20 @@ class AccountServiceTest extends TestCase {
$this->accountService->delete($this->user, $accountId); $this->accountService->delete($this->user, $accountId);
} }
public function testDeleteByAccountId() {
$accountId = 33;
$this->mapper->expects($this->once())
->method('findById')
->with($accountId)
->will($this->returnValue($this->account1));
$this->mapper->expects($this->once())
->method('delete')
->with($this->account1);
$this->accountService->deleteByAccountId($accountId);
}
public function testSave() { public function testSave() {
$account = new MailAccount(); $account = new MailAccount();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment