Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(provisioning): Clean up orphaned accounts #8993

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}