Skip to content

Commit

Permalink
proper variable naming and fixes lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatic75 committed Jul 26, 2023
1 parent 92dddab commit 246e603
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 21 deletions.
8 changes: 4 additions & 4 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2796,7 +2796,7 @@ public function updateDocument(string $collection, string $id, Document $documen
$collection = $this->silent(fn () => $this->getCollection($collection));

$validator = new Authorization(self::PERMISSION_UPDATE);
$skipPermission = true;
$shouldUpdate = false;

if ($collection->getId() !== self::METADATA) {
$documentSecurity = $collection->getAttribute('documentSecurity', false);
Expand All @@ -2808,19 +2808,19 @@ public function updateDocument(string $collection, string $id, Document $documen
continue;
}
if ($old->getAttribute($key) !== $value) {
$skipPermission = false;
$shouldUpdate = true;
break;
}
}
if (!$skipPermission && !$validator->isValid([
if ($shouldUpdate && !$validator->isValid([
...$collection->getUpdate(),
...($documentSecurity ? $old->getUpdate() : [])
])) {
throw new AuthorizationException($validator->getDescription());
}
}

if (!$skipPermission) {
if ($shouldUpdate) {
$document->setAttribute('$updatedAt', $time);
}

Expand Down
19 changes: 2 additions & 17 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2746,10 +2746,8 @@ public function testWritePermissionsUpdateFailure(Document $document): Document
*/
public function testNoChangeUpdateDocumentWithoutPermission(Document $document): Document
{
Authorization::cleanRoles();
Authorization::setRole(Role::any()->toString());


$document = static::getDatabase()->createDocument('documents', new Document([
'string' => 'text📝',
'integer' => 5,
Expand All @@ -2758,22 +2756,9 @@ public function testNoChangeUpdateDocumentWithoutPermission(Document $document):
'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);

Authorization::cleanRoles();
$updatedDocument = static::getDatabase()->updateDocument('documents', $document->getId(), $document);

// Document should not be updated as there is no change. It should also not throw any authorization exception without any permission because of no change.
$this->assertEquals($updatedDocument->getUpdatedAt(), $document->getUpdatedAt());
Expand Down

0 comments on commit 246e603

Please sign in to comment.