Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Make authorization per-instance #343

Merged
merged 14 commits into from
Jan 25, 2024
20 changes: 10 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions src/Database/Adapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Validator\Authorization;

abstract class Adapter
{
Expand All @@ -24,6 +25,23 @@ abstract class Adapter

protected static ?int $timeout = null;

/**
* @var Authorization
*/
protected Authorization $authorization;

/**
* @param Authorization $authorization
*
* @return $this
*/
public function setAuthorization(Authorization $authorization): self
{
$this->authorization = $authorization;

return $this;
}

/**
* @param string $key
* @param mixed $value
Expand Down
13 changes: 6 additions & 7 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Utopia\Database\Exception\Duplicate;
use Utopia\Database\Exception\Timeout;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;

class MariaDB extends SQL
{
Expand Down Expand Up @@ -953,7 +952,7 @@ public function deleteDocument(string $collection, string $id): bool
public function find(string $collection, array $queries = [], ?int $limit = 25, ?int $offset = null, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER, ?int $timeout = null): array
{
$name = $this->filter($collection);
$roles = Authorization::getRoles();
$roles = $this->authorization->getRoles();
$where = [];
$orders = [];

Expand Down Expand Up @@ -1039,7 +1038,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
}


if (Authorization::$status) {
if ($this->authorization->status) {
$where[] = $this->getSQLPermissionsCondition($name, $roles);
}

Expand Down Expand Up @@ -1144,15 +1143,15 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
public function count(string $collection, array $queries = [], ?int $max = null, ?int $timeout = null): int
{
$name = $this->filter($collection);
$roles = Authorization::getRoles();
$roles = $this->authorization->getRoles();
$where = [];
$limit = \is_null($max) ? '' : 'LIMIT :max';

foreach ($queries as $query) {
$where[] = $this->getSQLCondition($query);
}

if (Authorization::$status) {
if ($this->authorization->status) {
Meldiron marked this conversation as resolved.
Show resolved Hide resolved
$where[] = $this->getSQLPermissionsCondition($name, $roles);
}

Expand Down Expand Up @@ -1200,15 +1199,15 @@ public function count(string $collection, array $queries = [], ?int $max = null,
public function sum(string $collection, string $attribute, array $queries = [], ?int $max = null, ?int $timeout = null): int|float
{
$name = $this->filter($collection);
$roles = Authorization::getRoles();
$roles = $this->authorization->getRoles();
$where = [];
$limit = \is_null($max) ? '' : 'LIMIT :max';

foreach ($queries as $query) {
$where[] = $this->getSQLCondition($query);
}

if (Authorization::$status) {
if ($this->authorization->status) {
$where[] = $this->getSQLPermissionsCondition($name, $roles);
}

Expand Down
12 changes: 6 additions & 6 deletions src/Database/Adapter/Mongo.php
Original file line number Diff line number Diff line change
Expand Up @@ -825,8 +825,8 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
$filters = $this->buildFilters($queries);

// permissions
if (Authorization::$status) { // skip if authorization is disabled
$roles = \implode('|', Authorization::getRoles());
if ($this->authorization->status) { // skip if authorization is disabled
$roles = \implode('|', $this->authorization->getRoles());
$filters['_permissions']['$in'] = [new Regex("read\(\".*(?:{$roles}).*\"\)", 'i')];
}

Expand Down Expand Up @@ -1096,8 +1096,8 @@ public function count(string $collection, array $queries = [], ?int $max = null,
$filters = $this->buildFilters($queries);

// permissions
if (Authorization::$status) { // skip if authorization is disabled
$roles = \implode('|', Authorization::getRoles());
if ($this->authorization->status) { // skip if authorization is disabled
$roles = \implode('|', $this->authorization->getRoles());
$filters['_permissions']['$in'] = [new Regex("read\(\".*(?:{$roles}).*\"\)", 'i')];
}

Expand Down Expand Up @@ -1129,8 +1129,8 @@ public function sum(string $collection, string $attribute, array $queries = [],
$filters = $this->buildFilters($queries);

// permissions
if (Authorization::$status) { // skip if authorization is disabled
$roles = \implode('|', Authorization::getRoles());
if ($this->authorization->status) { // skip if authorization is disabled
$roles = \implode('|', $this->authorization->getRoles());
$filters['_permissions']['$in'] = [new Regex("read\(\".*(?:{$roles}).*\"\)", 'i')];
}

Expand Down
14 changes: 7 additions & 7 deletions src/Database/Adapter/Postgres.php
Original file line number Diff line number Diff line change
Expand Up @@ -966,7 +966,7 @@ public function deleteDocument(string $collection, string $id): bool
public function find(string $collection, array $queries = [], ?int $limit = 25, ?int $offset = null, array $orderAttributes = [], array $orderTypes = [], array $cursor = [], string $cursorDirection = Database::CURSOR_AFTER, ?int $timeout = null): array
{
$name = $this->filter($collection);
$roles = Authorization::getRoles();
$roles = $this->authorization->getRoles();
$where = [];
$orders = [];

Expand Down Expand Up @@ -1046,7 +1046,7 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
}


if (Authorization::$status) {
if ($this->authorization->status) {
$where[] = $this->getSQLPermissionsCondition($name, $roles);
}

Expand Down Expand Up @@ -1151,15 +1151,15 @@ public function find(string $collection, array $queries = [], ?int $limit = 25,
public function count(string $collection, array $queries = [], ?int $max = null, ?int $timeout = null): int
{
$name = $this->filter($collection);
$roles = Authorization::getRoles();
$roles = $this->authorization->getRoles();
$where = [];
$limit = \is_null($max) ? '' : 'LIMIT :max';

foreach ($queries as $query) {
$where[] = $this->getSQLCondition($query);
}

if (Authorization::$status) {
if ($this->authorization->status) {
$where[] = $this->getSQLPermissionsCondition($name, $roles);
}

Expand Down Expand Up @@ -1209,17 +1209,17 @@ public function count(string $collection, array $queries = [], ?int $max = null,
public function sum(string $collection, string $attribute, array $queries = [], ?int $max = null, ?int $timeout = null): int|float
{
$name = $this->filter($collection);
$roles = Authorization::getRoles();
$roles = $this->authorization->getRoles();
$where = [];
$limit = \is_null($max) ? '' : 'LIMIT :max';

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

foreach ($queries as $query) {
$where[] = $this->getSQLCondition($query);
}

if (Authorization::$status) {
if ($this->authorization->status) {
$where[] = $this->getSQLPermissionsCondition($name, $roles);
}

Expand Down
Loading