Skip to content
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
20 changes: 17 additions & 3 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -3615,6 +3615,10 @@ public function createDocument(string $collection, Document $document): Document
->setAttribute('$createdAt', ($createdAt === null || !$this->preserveDates) ? $time : $createdAt)
->setAttribute('$updatedAt', ($updatedAt === null || !$this->preserveDates) ? $time : $updatedAt);

if (empty($document->getPermissions())) {
$document->setAttribute('$permissions', []);
}

if ($this->adapter->getSharedTables()) {
if ($this->adapter->getTenantPerDocument()) {
if (
Expand Down Expand Up @@ -3715,6 +3719,10 @@ public function createDocuments(
->setAttribute('$createdAt', ($createdAt === null || !$this->preserveDates) ? $time : $createdAt)
->setAttribute('$updatedAt', ($updatedAt === null || !$this->preserveDates) ? $time : $updatedAt);

if (empty($document->getPermissions())) {
$document->setAttribute('$permissions', []);
}

if ($this->adapter->getSharedTables()) {
if ($this->adapter->getTenantPerDocument()) {
if ($document->getTenant() === null) {
Expand Down Expand Up @@ -4456,6 +4464,7 @@ public function updateDocuments(

$document = $this->encode($collection, $document);
}

$this->adapter->updateDocuments(
$collection->getId(),
$updates,
Expand Down Expand Up @@ -6350,9 +6359,6 @@ public function encode(Document $collection, Document $document): Document
}

if ($key === '$permissions') {
if (empty($value)) {
$document->setAttribute('$permissions', []); // set default value
}
continue;
}

Expand Down Expand Up @@ -6433,6 +6439,10 @@ public function decode(Document $collection, Document $document, array $selectio
$filters = $attribute['filters'] ?? [];
$value = $document->getAttribute($key);

if ($key === '$permissions') {
continue;
}

if (\is_null($value)) {
$value = $document->getAttribute($this->adapter->filter($key));

Expand Down Expand Up @@ -6492,6 +6502,10 @@ public function casting(Document $collection, Document $document): Document
continue;
}

if ($key === '$permissions') {
continue;
}

if ($array) {
$value = !is_string($value)
? $value
Expand Down
4 changes: 1 addition & 3 deletions src/Database/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@ public function setAttributes(array $attributes): static
*/
public function removeAttribute(string $key): static
{
if (\array_key_exists($key, (array)$this)) {
unset($this[$key]);
}
unset($this[$key]);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to check + copy array since unset is silent


/* @phpstan-ignore-next-line */
return $this;
Expand Down
5 changes: 5 additions & 0 deletions tests/e2e/Adapter/Scopes/DocumentTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,11 @@ public function testSkipPermissions(): void

$this->assertEquals(2, \count($results));
$this->assertEquals(2, $count);

foreach ($results as $result) {
$this->assertArrayHasKey('$permissions', $result);
$this->assertEquals([], $result->getAttribute('$permissions'));
}
}

public function testUpsertDocuments(): void
Expand Down