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
8 changes: 5 additions & 3 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -1312,8 +1312,6 @@ public function sum(string $collection, string $attribute, array $queries = [],
$where = [];
$limit = \is_null($max) ? '' : 'LIMIT :max';

$permissions = (Authorization::$status) ? $this->getSQLPermissionsCondition($collection, $roles) : '1=1'; // Disable join when no authorization required

foreach ($queries as $query) {
$where[] = $this->getSQLCondition($query);
}
Expand All @@ -1322,11 +1320,15 @@ public function sum(string $collection, string $attribute, array $queries = [],
$where[] = $this->getSQLPermissionsCondition($name, $roles);
}

$sqlWhere = !empty($where)
? 'WHERE ' . \implode(' AND ', $where)
: '';

$sql = "
SELECT SUM({$attribute}) as sum FROM (
SELECT {$attribute}
FROM {$this->getSQLTable($name)} table_main
WHERE {$permissions} AND " . implode(' AND ', $where) . "
{$sqlWhere}
{$limit}
) table_count
";
Expand Down
8 changes: 2 additions & 6 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -876,15 +876,11 @@ public function getCollection(string $id): Document
*/
public function listCollections(int $limit = 25, int $offset = 0): array
{
Authorization::disable();

$result = $this->silent(fn () => $this->find(self::METADATA, [
Query::limit($limit),
Query::offset($offset)
]));

Authorization::reset();

$this->trigger(self::EVENT_COLLECTION_LIST, $result);

return $result;
Expand Down Expand Up @@ -4156,8 +4152,8 @@ public function find(string $collection, array $queries = []): array
$documentSecurity = $collection->getAttribute('documentSecurity', false);
$skipAuth = $authorization->isValid($collection->getRead());

if (!$skipAuth && !$documentSecurity) {
throw new AuthorizationException($validator->getDescription());
if (!$skipAuth && !$documentSecurity && $collection->getId() !== self::METADATA) {
throw new AuthorizationException($authorization->getDescription());
}

$relationships = \array_filter(
Expand Down
27 changes: 20 additions & 7 deletions tests/Database/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ public function testIndexValidation(): void
$this->assertEquals($errorMessage, $validator->getDescription());

try {
static::getDatabase()->createCollection($collection->getId(), $attributes, $indexes);
static::getDatabase()->createCollection($collection->getId(), $attributes, $indexes, [
Permission::read(Role::any()),
Permission::create(Role::any()),
]);
$this->fail('Failed to throw exception');
} catch (Exception $e) {
$this->assertEquals($errorMessage, $e->getMessage());
Expand Down Expand Up @@ -262,19 +265,29 @@ public function testQueryTimeout(): void
*/
public function testCreateListExistsDeleteCollection(): void
{
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createCollection('actors'));
$this->assertCount(2, static::getDatabase()->listCollections());
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createCollection('actors', permissions: [
Permission::create(Role::any()),
Permission::read(Role::any()),
]));
$this->assertCount(1, static::getDatabase()->listCollections());
$this->assertEquals(true, static::getDatabase()->exists($this->testDatabase, 'actors'));

// Collection names should not be unique
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createCollection('actors2'));
$this->assertCount(3, static::getDatabase()->listCollections());
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->createCollection('actors2', permissions: [
Permission::create(Role::any()),
Permission::read(Role::any()),
]));
$this->assertCount(2, static::getDatabase()->listCollections());
$this->assertEquals(true, static::getDatabase()->exists($this->testDatabase, 'actors2'));
$collection = static::getDatabase()->getCollection('actors2');
$collection->setAttribute('name', 'actors'); // change name to one that exists
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->updateDocument($collection->getCollection(), $collection->getId(), $collection));
$this->assertInstanceOf('Utopia\Database\Document', static::getDatabase()->updateDocument(
$collection->getCollection(),
$collection->getId(),
$collection
));
$this->assertEquals(true, static::getDatabase()->deleteCollection('actors2')); // Delete collection when finished
$this->assertCount(2, static::getDatabase()->listCollections());
$this->assertCount(1, static::getDatabase()->listCollections());

$this->assertEquals(false, static::getDatabase()->getCollection('actors')->isEmpty());
$this->assertEquals(true, static::getDatabase()->deleteCollection('actors'));
Expand Down