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

No Exception when no change in update document function #293

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2855,24 +2855,37 @@ public function updateDocument(string $collection, string $id, Document $documen
}

$time = DateTime::now();
$document->setAttribute('$updatedAt', $time);

$old = Authorization::skip(fn () => $this->silent(fn () => $this->getDocument($collection, $id))); // Skip ensures user does not need read permission for this

$collection = $this->silent(fn () => $this->getCollection($collection));

$validator = new Authorization(self::PERMISSION_UPDATE);

if ($collection->getId() !== self::METADATA) {
$documentSecurity = $collection->getAttribute('documentSecurity', false);

if (!$validator->isValid([
$skipPermission = true;
//compare if the document any changes
foreach ($document as $key=>$value) {
//skip the nested documents as they will be checked later in recursions.
if ($old->getAttribute($key) instanceof Document) {
continue;
}
if ($old->getAttribute($key) !== $value) {
$skipPermission = false;
break;
}
}
if (!$skipPermission && !$validator->isValid([
...$collection->getUpdate(),
...($documentSecurity ? $old->getUpdate() : [])
])) {
throw new AuthorizationException($validator->getDescription());
}
}

$document->setAttribute('$updatedAt', $time);

// Check if document was updated after the request timestamp
$oldUpdatedAt = new \DateTime($old->getUpdatedAt());
if (!is_null($this->timestamp) && $oldUpdatedAt > $this->timestamp) {
Expand Down
44 changes: 42 additions & 2 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2993,13 +2993,54 @@ public function testWritePermissionsUpdateFailure(Document $document): Document
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
'string' => 'text📝',
'integer' => 6,
'bigint' => 8589934592, // 2^33
'float' => 5.55,
'boolean' => true,
'colors' => ['pink', 'green', 'blue'],
]));

return $document;
}


/**
* @depends testCreateDocument
*/
public function testNoChangeUpdateDocumentWithoutPermission(Document $document): Document
{
Authorization::cleanRoles();
Authorization::setRole(Role::any()->toString());


$document = static::getDatabase()->createDocument('documents', new Document([
'string' => 'text📝',
'integer' => 5,
'bigint' => 8589934592, // 2^33
'float' => 5.55,
'boolean' => true,
'colors' => ['pink', 'green', 'blue'],
]));
Authorization::cleanRoles();
//no changes in document
$documentToUpdate = new Document([
'$id' => ID::custom($document->getId()),
'string' => 'text📝',
'integer' => 5,
'bigint' => 8589934592, // 2^33
'float' => 5.55,
'boolean' => true,
'colors' => ['pink', 'green', 'blue'],
'$collection' => 'documents',
]);
$documentToUpdate->setAttribute('$createdAt', $document->getAttribute('$createdAt'));
$documentToUpdate->setAttribute('$updatedAt', $document->getAttribute('$updatedAt'));
$updatedDocument = static::getDatabase()->updateDocument('documents', $document->getId(), $documentToUpdate);


// Document should be updated without any problem and without any authorization exception as there is no change in document.
$this->assertEquals(true, $updatedDocument->getUpdatedAt() > $document->getUpdatedAt());

return $document;
}
Expand Down Expand Up @@ -10764,15 +10805,14 @@ public function testCollectionPermissionsUpdateWorks(array $data): array
public function testCollectionPermissionsUpdateThrowsException(array $data): void
{
[$collection, $document] = $data;

Authorization::cleanRoles();
Authorization::setRole(Role::any()->toString());

$this->expectException(AuthorizationException::class);
$document = static::getDatabase()->updateDocument(
$collection->getId(),
$document->getId(),
$document->setAttribute('test', 'ipsum')
$document->setAttribute('test', 'lorem')
);
}

Expand Down