diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 4d640c900..e56678f8e 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -394,6 +394,8 @@ public function getDocument(Document $collection, string $id, array $queries = [ $sql .= " {$forUpdate}"; } + $sql = $this->trigger(Database::EVENT_DOCUMENT_READ, $sql); + $stmt = $this->getPDO()->prepare($sql); $stmt->bindValue(':_uid', $id); diff --git a/src/Database/Database.php b/src/Database/Database.php index 0e8c7a74f..f9fad6808 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -732,10 +732,10 @@ public function on(string $event, string $name, ?callable $callback): static * * @param string $event * @param string $name - * @param callable $callback + * @param ?callable $callback * @return $this */ - public function before(string $event, string $name, callable $callback): static + public function before(string $event, string $name, ?callable $callback): static { $this->adapter->before($event, $name, $callback); diff --git a/tests/e2e/Adapter/Scopes/CollectionTests.php b/tests/e2e/Adapter/Scopes/CollectionTests.php index d5d23e129..1cbebd1db 100644 --- a/tests/e2e/Adapter/Scopes/CollectionTests.php +++ b/tests/e2e/Adapter/Scopes/CollectionTests.php @@ -3,6 +3,7 @@ namespace Tests\E2E\Adapter\Scopes; use Exception; +use Utopia\Database\Adapter\SQL; use Utopia\Database\Database; use Utopia\Database\Document; use Utopia\Database\Exception as DatabaseException; @@ -1670,13 +1671,25 @@ public function testTransformations(): void 'name' => 'value1', ])); - $database->before(Database::EVENT_DOCUMENT_READ, 'test', function (string $query) { - return "SELECT 1"; + $database->setMetadata('scope', 'api.users'); + + $capturedSql = ''; + $database->before(Database::EVENT_DOCUMENT_READ, 'test', function (string $sql) use (&$capturedSql) { + $sql .= ' AND 1=0'; + $capturedSql = $sql; + return $sql; }); $result = $database->getDocument('docs', 'doc1'); $this->assertTrue($result->isEmpty()); + + if ($database->getAdapter() instanceof SQL) { + $this->assertStringContainsString('/* scope: api.users */', $capturedSql); + } + + $database->before(Database::EVENT_DOCUMENT_READ, 'test', null); + $database->resetMetadata(); } public function testSetGlobalCollection(): void