Skip to content

Commit

Permalink
Merge pull request #8993 from nextcloud/fix/provisioning/clean-up-orp…
Browse files Browse the repository at this point in the history
…han-accounts

fix(provisioning): Clean up orphaned accounts
  • Loading branch information
ChristophWurst authored Dec 27, 2023
2 parents dff7c56 + ed228bb commit 09b8929
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/Db/MailAccount.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
* @method string getEditorMode()
* @method void setEditorMode(string $editorMode)
* @method int|null getProvisioningId()
* @method void setProvisioningId(int $provisioningId)
* @method void setProvisioningId(int|null $provisioningId)
* @method int getOrder()
* @method void setOrder(int $order)
* @method bool|null getShowSubscribedOnly()
Expand Down
18 changes: 18 additions & 0 deletions lib/Db/MailAccountMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ public function deleteProvisionedAccountsByUid(string $uid): void {
$delete->executeStatement();
}

public function deleteProvisionedOrphanAccounts(): void {
$inner = $this->db->getQueryBuilder()
->select('id')
->from('mail_provisionings');

$delete = $this->db->getQueryBuilder();
$delete->delete($this->getTableName())
->where(
$delete->expr()->isNotNull('provisioning_id'),
$delete->expr()->notIn(
'provisioning_id',
$delete->createFunction($inner->getSQL()),
IQueryBuilder::PARAM_INT_ARRAY
));

$delete->executeStatement();
}

/**
* @return MailAccount[]
*/
Expand Down
9 changes: 8 additions & 1 deletion lib/Service/CleanupService.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

use OCA\Mail\Db\AliasMapper;
use OCA\Mail\Db\CollectedAddressMapper;
use OCA\Mail\Db\MailAccountMapper;
use OCA\Mail\Db\MailboxMapper;
use OCA\Mail\Db\MessageMapper;
use OCA\Mail\Db\MessageRetentionMapper;
Expand All @@ -38,6 +39,8 @@
use Psr\Log\LoggerInterface;

class CleanupService {
private MailAccountMapper $mailAccountMapper;

/** @var AliasMapper */
private $aliasMapper;

Expand All @@ -60,7 +63,8 @@ class CleanupService {
private PersistenceService $classifierPersistenceService;
private ITimeFactory $timeFactory;

public function __construct(AliasMapper $aliasMapper,
public function __construct(MailAccountMapper $mailAccountMapper,
AliasMapper $aliasMapper,
MailboxMapper $mailboxMapper,
MessageMapper $messageMapper,
CollectedAddressMapper $collectedAddressMapper,
Expand All @@ -77,6 +81,7 @@ public function __construct(AliasMapper $aliasMapper,
$this->messageRetentionMapper = $messageRetentionMapper;
$this->messageSnoozeMapper = $messageSnoozeMapper;
$this->classifierPersistenceService = $classifierPersistenceService;
$this->mailAccountMapper = $mailAccountMapper;
$this->timeFactory = $timeFactory;
}

Expand All @@ -85,6 +90,8 @@ public function cleanUp(LoggerInterface $logger): void {
$this->timeFactory,
$logger
))->start('clean up');
$this->mailAccountMapper->deleteProvisionedOrphanAccounts();
$task->step('delete orphan provisioned accounts');
$this->aliasMapper->deleteOrphans();
$task->step('delete orphan aliases');
$this->mailboxMapper->deleteOrphans();
Expand Down
13 changes: 13 additions & 0 deletions tests/Integration/Db/MailAccountMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@
use OC;
use OCA\Mail\Db\MailAccount;
use OCA\Mail\Db\MailAccountMapper;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\IDBConnection;
use function random_int;

/**
* @group DB
* @covers \OCA\Mail\Db\MailAccountMapper
*/
class MailAccountMapperTest extends TestCase {
use DatabaseTransaction;
Expand Down Expand Up @@ -106,4 +109,14 @@ public function testSave() {
$this->assertNotNull($b->getId());
$this->assertEquals($b->getId(), $c->getId());
}

public function testDeleteProvisionedOrphans(): void {
$this->account->setProvisioningId(random_int(1, 10000));
$this->mapper->insert($this->account);

$this->mapper->deleteProvisionedOrphanAccounts();

$this->expectException(DoesNotExistException::class);
$this->mapper->findById($this->account->getId());
}
}

0 comments on commit 09b8929

Please sign in to comment.