1
0
Fork 0
mirror of synced 2024-06-18 18:54:55 +12:00

fix: delete audit logs for collections and documents

This commit is contained in:
Torsten Dittmann 2022-01-03 11:29:15 +01:00
parent a071cae7f0
commit 5d44e6ee78
2 changed files with 33 additions and 2 deletions

View file

@ -2076,13 +2076,15 @@ App::delete('/v1/database/collections/:collectionId/documents/:documentId')
->inject('dbForProject')
->inject('events')
->inject('audits')
->inject('deletes')
->inject('usage')
->inject('mode')
->action(function ($collectionId, $documentId, $response, $dbForProject, $events, $audits, $usage, $mode) {
->action(function ($collectionId, $documentId, $response, $dbForProject, $events, $audits, $deletes, $usage, $mode) {
/** @var Appwrite\Utopia\Response $response */
/** @var Utopia\Database\Database $dbForProject */
/** @var Appwrite\Event\Event $events */
/** @var Appwrite\Event\Event $audits */
/** @var Appwrite\Event\Event $deletes */
/** @var Appwrite\Stats\Stats $usage */
/** @var string $mode */
@ -2129,6 +2131,11 @@ App::delete('/v1/database/collections/:collectionId/documents/:documentId')
*/
$document->setAttribute('$collection', $collectionId);
$deletes
->setParam('type', DELETE_TYPE_AUDIT)
->setParam('document', $document)
;
$usage
->setParam('database.documents.delete', 1)
->setParam('collectionId', $collectionId)

View file

@ -67,7 +67,17 @@ class DeletesV1 extends Worker
break;
case DELETE_TYPE_AUDIT:
$this->deleteAuditLogs($this->args['timestamp']);
$timestamp = $this->args['timestamp'] ?? 0;
$document = new Document($this->args['document'] ?? []);
if (!empty($timestamp)) {
$this->deleteAuditLogs($this->args['timestamp']);
}
if (!$document->isEmpty()) {
$this->deleteAuditLogsByResource('document/' . $document->getId(), $projectId);
}
break;
case DELETE_TYPE_ABUSE:
@ -115,6 +125,8 @@ class DeletesV1 extends Worker
$this->deleteByGroup('indexes', [
new Query('collectionId', Query::TYPE_EQUAL, [$collectionId])
], $dbForProject);
$this->deleteAuditLogsByResource('collection/' . $collectionId, $projectId);
}
/**
@ -263,6 +275,18 @@ class DeletesV1 extends Worker
});
}
/**
* @param int $timestamp
*/
protected function deleteAuditLogsByResource(string $resource, string $projectId): void
{
$dbForProject = $this->getProjectDB($projectId);
$this->deleteByGroup(Audit::COLLECTION, [
new Query('resource', Query::TYPE_EQUAL, [$resource])
], $dbForProject);
}
/**
* @param Document $document function document
* @param string $projectId