Skip to content

Commit

Permalink
Merge pull request #348 from utopia-php/fix-authorization-skip-list-c…
Browse files Browse the repository at this point in the history
…ollections

fix: remove authorization skip from `listCollections`
  • Loading branch information
abnegate authored Oct 26, 2023
2 parents b2d403c + f107449 commit b0c3fd8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 16 deletions.
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

0 comments on commit b0c3fd8

Please sign in to comment.