Skip to content

Commit 7281582

Browse files
authored
Merge pull request #639 from utopia-php/null-permissions
Fix encoding decoding
2 parents bb4f0e4 + 10e7a0a commit 7281582

File tree

3 files changed

+58
-13
lines changed

3 files changed

+58
-13
lines changed

src/Database/Database.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6313,12 +6313,9 @@ public function encode(Document $collection, Document $document): Document
63136313
{
63146314
$attributes = $collection->getAttribute('attributes', []);
63156315

6316-
$internalAttributes = \array_filter(Database::INTERNAL_ATTRIBUTES, function ($attribute) {
6317-
// We don't want to encode permissions into a JSON string
6318-
return $attribute['$id'] !== '$permissions';
6319-
});
6320-
6321-
$attributes = \array_merge($attributes, $internalAttributes);
6316+
foreach ($this->getInternalAttributes() as $attribute) {
6317+
$attributes[] = $attribute;
6318+
}
63226319

63236320
foreach ($attributes as $attribute) {
63246321
$key = $attribute['$id'] ?? '';
@@ -6327,6 +6324,13 @@ public function encode(Document $collection, Document $document): Document
63276324
$filters = $attribute['filters'] ?? [];
63286325
$value = $document->getAttribute($key);
63296326

6327+
if ($key === '$permissions') {
6328+
if (empty($value)) {
6329+
$document->setAttribute('$permissions', []); // set default value
6330+
}
6331+
continue;
6332+
}
6333+
63306334
// Continue on optional param with no default
63316335
if (is_null($value) && is_null($default)) {
63326336
continue;
@@ -6394,7 +6398,9 @@ public function decode(Document $collection, Document $document, array $selectio
63946398
}
63956399
}
63966400

6397-
$attributes = \array_merge($attributes, $this->getInternalAttributes());
6401+
foreach ($this->getInternalAttributes() as $attribute) {
6402+
$attributes[] = $attribute;
6403+
}
63986404

63996405
foreach ($attributes as $attribute) {
64006406
$key = $attribute['$id'] ?? '';
@@ -6413,10 +6419,11 @@ public function decode(Document $collection, Document $document, array $selectio
64136419
$value = ($array) ? $value : [$value];
64146420
$value = (is_null($value)) ? [] : $value;
64156421

6416-
foreach ($value as &$node) {
6417-
foreach (\array_reverse($filters) as $filter) {
6422+
foreach ($value as $index => $node) {
6423+
foreach (array_reverse($filters) as $filter) {
64186424
$node = $this->decodeAttribute($filter, $node, $document, $key);
64196425
}
6426+
$value[$index] = $node;
64206427
}
64216428

64226429
if (
@@ -6447,7 +6454,9 @@ public function casting(Document $collection, Document $document): Document
64476454

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

6450-
$attributes = \array_merge($attributes, $this->getInternalAttributes());
6457+
foreach ($this->getInternalAttributes() as $attribute) {
6458+
$attributes[] = $attribute;
6459+
}
64516460

64526461
foreach ($attributes as $attribute) {
64536462
$key = $attribute['$id'] ?? '';
@@ -6466,7 +6475,7 @@ public function casting(Document $collection, Document $document): Document
64666475
$value = [$value];
64676476
}
64686477

6469-
foreach ($value as &$node) {
6478+
foreach ($value as $index => $node) {
64706479
switch ($type) {
64716480
case self::VAR_BOOLEAN:
64726481
$node = (bool)$node;
@@ -6480,6 +6489,8 @@ public function casting(Document $collection, Document $document): Document
64806489
default:
64816490
break;
64826491
}
6492+
6493+
$value[$index] = $node;
64836494
}
64846495

64856496
$document->setAttribute($key, ($array) ? $value : $value[0]);

src/Database/Document.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ class Document extends ArrayObject
2727
*/
2828
public function __construct(array $input = [])
2929
{
30-
if (isset($input['$id']) && !\is_string($input['$id'])) {
30+
if (array_key_exists('$id', $input) && !\is_string($input['$id'])) {
3131
throw new StructureException('$id must be of type string');
3232
}
3333

34-
if (isset($input['$permissions']) && !is_array($input['$permissions'])) {
34+
if (array_key_exists('$permissions', $input) && !is_array($input['$permissions'])) {
3535
throw new StructureException('$permissions must be of type array');
3636
}
3737

tests/e2e/Adapter/Scopes/PermissionTests.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,40 @@
1414

1515
trait PermissionTests
1616
{
17+
public function testCreateDocumentsEmptyPermission(): void
18+
{
19+
/** @var Database $database */
20+
$database = static::getDatabase();
21+
22+
$database->createCollection(__FUNCTION__);
23+
24+
/**
25+
* Validate the decode function does not add $permissions null entry when no permissions are provided
26+
*/
27+
28+
$document = $database->createDocument(__FUNCTION__, new Document());
29+
30+
$this->assertArrayHasKey('$permissions', $document);
31+
$this->assertEquals([], $document->getAttribute('$permissions'));
32+
33+
$documents = [];
34+
35+
for ($i = 0; $i < 2; $i++) {
36+
$documents[] = new Document();
37+
}
38+
39+
$results = [];
40+
$count = $database->createDocuments(__FUNCTION__, $documents, onNext: function ($doc) use (&$results) {
41+
$results[] = $doc;
42+
});
43+
44+
$this->assertEquals(2, $count);
45+
foreach ($results as $result) {
46+
$this->assertArrayHasKey('$permissions', $result);
47+
$this->assertEquals([], $result->getAttribute('$permissions'));
48+
}
49+
}
50+
1751
public function testReadPermissionsFailure(): Document
1852
{
1953
Authorization::cleanRoles();

0 commit comments

Comments
 (0)