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: 9 additions & 11 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -2934,8 +2934,8 @@ public function updateDocument(string $collection, string $id, Document $documen
$time = DateTime::now();
$old = Authorization::skip(fn () => $this->silent(fn () => $this->getDocument($collection, $id))); // Skip ensures user does not need read permission for this
$document = \array_merge($old->getArrayCopy(), $document->getArrayCopy());
$document['$collection'] = $old->getAttribute('$collection'); // Make sure user doesn't switch collectionID
$document['$createdAt'] = $old->getCreatedAt(); // Make sure user doesn't switch createdAt
$document['$collection'] = $old->getAttribute('$collection'); // Make sure user doesn't switch collectionID
$document['$createdAt'] = $old->getCreatedAt(); // Make sure user doesn't switch createdAt
$document = new Document($document);

$collection = $this->silent(fn () => $this->getCollection($collection));
Expand Down Expand Up @@ -4062,8 +4062,13 @@ public function find(string $collection, array $queries = [], ?int $timeout = nu
}

$authorization = new Authorization(self::PERMISSION_READ);
$documentSecurity = $collection->getAttribute('documentSecurity', false);
$skipAuth = $authorization->isValid($collection->getRead());

if (!$skipAuth && !$documentSecurity) {
throw new AuthorizationException($validator->getDescription());
}

$relationships = \array_filter(
$collection->getAttribute('attributes', []),
fn (Document $attribute) => $attribute->getAttribute('type') === self::VAR_RELATIONSHIP
Expand Down Expand Up @@ -4092,7 +4097,6 @@ public function find(string $collection, array $queries = [], ?int $timeout = nu

$selections = $this->validateSelections($collection, $selects);
$nestedSelections = [];
$nestedQueries = [];

foreach ($queries as $index => &$query) {
switch ($query->getMethod()) {
Expand Down Expand Up @@ -4129,14 +4133,14 @@ public function find(string $collection, array $queries = [], ?int $timeout = nu
break;
default:
if (\str_contains($query->getAttribute(), '.')) {
$nestedQueries[] = $query;
unset($queries[$index]);
}
break;
}
}

$queries = \array_values($queries);

$getResults = fn () => $this->adapter->find(
$collection->getId(),
$queries,
Expand All @@ -4151,13 +4155,7 @@ public function find(string $collection, array $queries = [], ?int $timeout = nu

$results = $skipAuth ? Authorization::skip($getResults) : $getResults();

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

$relationships = $this->resolveRelationships
? \array_filter($attributes, fn (Document $attribute) => $attribute->getAttribute('type') === self::VAR_RELATIONSHIP)
: [];

foreach ($results as $index => &$node) {
foreach ($results as &$node) {
if ($this->resolveRelationships && (empty($selects) || !empty($nestedSelections))) {
$node = $this->silent(fn () => $this->populateDocumentRelationships($collection, $node, $nestedSelections));
}
Expand Down
19 changes: 8 additions & 11 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -11315,18 +11315,17 @@ public function testCollectionPermissionsFindWorks(array $data): array
Authorization::cleanRoles();
Authorization::setRole(Role::users()->toString());

$documents = static::getDatabase()->find(
$collection->getId()
);
$documents = static::getDatabase()->find($collection->getId());
$this->assertNotEmpty($documents);

Authorization::cleanRoles();
Authorization::setRole(Role::user('random')->toString());

$documents = static::getDatabase()->find(
$collection->getId()
);
$this->assertNotEmpty($documents);
try {
static::getDatabase()->find($collection->getId());
$this->fail('Failed to throw exception');
} catch (AuthorizationException) {
}

return $data;
}
Expand All @@ -11342,10 +11341,8 @@ public function testCollectionPermissionsFindThrowsException(array $data): void
Authorization::cleanRoles();
Authorization::setRole(Role::any()->toString());

$documents = static::getDatabase()->find(
$collection->getId()
);
$this->assertEmpty($documents);
$this->expectException(AuthorizationException::class);
static::getDatabase()->find($collection->getId());
}

/**
Expand Down