Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
33 changes: 19 additions & 14 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -6292,12 +6292,11 @@ public function encode(Document $collection, Document $document): Document
{
$attributes = $collection->getAttribute('attributes', []);

$internalAttributes = \array_filter(Database::INTERNAL_ATTRIBUTES, function ($attribute) {
// We don't want to encode permissions into a JSON string
return $attribute['$id'] !== '$permissions';
});

$attributes = \array_merge($attributes, $internalAttributes);
foreach ($this->getInternalAttributes() as $attribute) {
if ($attribute['$id'] !== '$permissions') { // Don't encode permissions into a JSON string
$attributes[] = new Document($attribute);
}
}

foreach ($attributes as $attribute) {
$key = $attribute['$id'] ?? '';
Expand Down Expand Up @@ -6362,18 +6361,19 @@ public function decode(Document $collection, Document $document, array $selectio
foreach ($relationships as $relationship) {
$key = $relationship['$id'] ?? '';

if (
\array_key_exists($key, (array)$document)
|| \array_key_exists($this->adapter->filter($key), (array)$document)
) {
if ($document->offsetExists($key) || $document->offsetExists($this->adapter->filter($key))) {
Comment thread
fogelito marked this conversation as resolved.
Outdated
$value = $document->getAttribute($key);
$value ??= $document->getAttribute($this->adapter->filter($key));
$document->removeAttribute($this->adapter->filter($key));
$document->setAttribute($key, $value);
}
}

$attributes = \array_merge($attributes, $this->getInternalAttributes());
foreach ($this->getInternalAttributes() as $attribute) {
if ($attribute['$id'] !== '$permissions') {
$attributes[] = new Document($attribute);
}
}

foreach ($attributes as $attribute) {
$key = $attribute['$id'] ?? '';
Expand All @@ -6392,10 +6392,11 @@ public function decode(Document $collection, Document $document, array $selectio
$value = ($array) ? $value : [$value];
$value = (is_null($value)) ? [] : $value;

foreach ($value as &$node) {
foreach (\array_reverse($filters) as $filter) {
foreach ($value as $k => $node) {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Remove reference

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Let's use a better name, $key, or something more descriptive if that collides

Copy link
Copy Markdown
Contributor Author

@fogelito fogelito Jul 28, 2025

Choose a reason for hiding this comment

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

Changed to $index since $key is in use

foreach (array_reverse($filters) as $filter) {
$node = $this->decodeAttribute($filter, $node, $document, $key);
}
$value[$k] = $node;
}

if (
Expand Down Expand Up @@ -6426,7 +6427,11 @@ public function casting(Document $collection, Document $document): Document

$attributes = $collection->getAttribute('attributes', []);

$attributes = \array_merge($attributes, $this->getInternalAttributes());
foreach ($this->getInternalAttributes() as $attribute) {
if ($attribute['$id'] !== '$permissions') {
$attributes[] = new Document($attribute);
}
}

foreach ($attributes as $attribute) {
$key = $attribute['$id'] ?? '';
Expand Down
4 changes: 2 additions & 2 deletions src/Database/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ class Document extends ArrayObject
*/
public function __construct(array $input = [])
{
if (isset($input['$id']) && !\is_string($input['$id'])) {
if (array_key_exists('$id', $input) && !\is_string($input['$id'])) {
throw new StructureException('$id must be of type string');
}

if (isset($input['$permissions']) && !is_array($input['$permissions'])) {
if (array_key_exists('$permissions', $input) && !is_array($input['$permissions'])) {
Comment thread
abnegate marked this conversation as resolved.
throw new StructureException('$permissions must be of type array');
}

Expand Down
34 changes: 34 additions & 0 deletions tests/e2e/Adapter/Scopes/PermissionTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,40 @@

trait PermissionTests
{
public function testCreateDocumentsEmptyPermission(): void
{
/** @var Database $database */
$database = static::getDatabase();

$database->createCollection(__FUNCTION__);

/**
* Validate the decode function does not add $permissions null entry when no permissions are provided
*/

$document = $database->createDocument(__FUNCTION__, new Document());

$this->assertArrayNotHasKey('$permissions', $document);
$this->assertEquals(null, $document->getAttribute('$permissions'));

$documents = [];

for ($i = 0; $i < 2; $i++) {
$documents[] = new Document();
}

$results = [];
$count = $database->createDocuments(__FUNCTION__, $documents, onNext: function ($doc) use (&$results) {
$results[] = $doc;
});

$this->assertEquals(2, $count);
foreach ($results as $result) {
$this->assertArrayNotHasKey('$permissions', $result);
$this->assertEquals(null, $result->getAttribute('$permissions'));
}
}

public function testReadPermissionsFailure(): Document
{
Authorization::cleanRoles();
Expand Down