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 #295

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
19 changes: 16 additions & 3 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2791,24 +2791,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
fanatic75 marked this conversation as resolved.
Show resolved Hide resolved
foreach ($document as $key=>$value) {
//skip the nested documents as they will be checked later in recursions.
fanatic75 marked this conversation as resolved.
Show resolved Hide resolved
if ($old->getAttribute($key) instanceof Document) {
fanatic75 marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -2728,13 +2728,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());

fanatic75 marked this conversation as resolved.
Show resolved Hide resolved

$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();
fanatic75 marked this conversation as resolved.
Show resolved Hide resolved
//no changes in document
fanatic75 marked this conversation as resolved.
Show resolved Hide resolved
$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'));
stnguyen90 marked this conversation as resolved.
Show resolved Hide resolved
$documentToUpdate->setAttribute('$updatedAt', $document->getAttribute('$updatedAt'));
stnguyen90 marked this conversation as resolved.
Show resolved Hide resolved
$updatedDocument = static::getDatabase()->updateDocument('documents', $document->getId(), $documentToUpdate);
fanatic75 marked this conversation as resolved.
Show resolved Hide resolved


// 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 @@ -10597,15 +10638,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
Loading