diff --git a/composer.lock b/composer.lock index 64c732c..5562302 100644 --- a/composer.lock +++ b/composer.lock @@ -216,16 +216,16 @@ }, { "name": "utopia-php/cache", - "version": "0.9.0", + "version": "0.9.1", "source": { "type": "git", "url": "https://github.com/utopia-php/cache.git", - "reference": "4fc7b4789b5f0ce74835c1ecfec4f3afe6f0e34e" + "reference": "552b4c554bb14d0c529631ce304cdf4a2b9d06a6" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/cache/zipball/4fc7b4789b5f0ce74835c1ecfec4f3afe6f0e34e", - "reference": "4fc7b4789b5f0ce74835c1ecfec4f3afe6f0e34e", + "url": "https://api.github.com/repos/utopia-php/cache/zipball/552b4c554bb14d0c529631ce304cdf4a2b9d06a6", + "reference": "552b4c554bb14d0c529631ce304cdf4a2b9d06a6", "shasum": "" }, "require": { @@ -260,9 +260,9 @@ ], "support": { "issues": "https://github.com/utopia-php/cache/issues", - "source": "https://github.com/utopia-php/cache/tree/0.9.0" + "source": "https://github.com/utopia-php/cache/tree/0.9.1" }, - "time": "2024-01-07T18:11:23+00:00" + "time": "2024-03-19T17:07:20+00:00" }, { "name": "utopia-php/database", @@ -270,12 +270,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/database.git", - "reference": "bdd9140e40c77faadb0cf2f5050b466c34eef673" + "reference": "6b8264a4e269d679182fc376179281b7108a4242" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/database/zipball/bdd9140e40c77faadb0cf2f5050b466c34eef673", - "reference": "bdd9140e40c77faadb0cf2f5050b466c34eef673", + "url": "https://api.github.com/repos/utopia-php/database/zipball/6b8264a4e269d679182fc376179281b7108a4242", + "reference": "6b8264a4e269d679182fc376179281b7108a4242", "shasum": "" }, "require": { @@ -319,7 +319,7 @@ "issues": "https://github.com/utopia-php/database/issues", "source": "https://github.com/utopia-php/database/tree/feat-framework-v2" }, - "time": "2024-03-07T16:55:44+00:00" + "time": "2024-04-18T12:50:22+00:00" }, { "name": "utopia-php/fetch", @@ -843,16 +843,16 @@ }, { "name": "phpstan/phpstan", - "version": "1.10.60", + "version": "1.10.67", "source": { "type": "git", "url": "https://github.com/phpstan/phpstan.git", - "reference": "95dcea7d6c628a3f2f56d091d8a0219485a86bbe" + "reference": "16ddbe776f10da6a95ebd25de7c1dbed397dc493" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/phpstan/phpstan/zipball/95dcea7d6c628a3f2f56d091d8a0219485a86bbe", - "reference": "95dcea7d6c628a3f2f56d091d8a0219485a86bbe", + "url": "https://api.github.com/repos/phpstan/phpstan/zipball/16ddbe776f10da6a95ebd25de7c1dbed397dc493", + "reference": "16ddbe776f10da6a95ebd25de7c1dbed397dc493", "shasum": "" }, "require": { @@ -895,13 +895,9 @@ { "url": "https://github.com/phpstan", "type": "github" - }, - { - "url": "https://tidelift.com/funding/github/packagist/phpstan/phpstan", - "type": "tidelift" } ], - "time": "2024-03-07T13:30:19+00:00" + "time": "2024-04-16T07:22:02+00:00" }, { "name": "phpunit/php-code-coverage", @@ -1224,16 +1220,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.6.17", + "version": "9.6.19", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "1a156980d78a6666721b7e8e8502fe210b587fcd" + "reference": "a1a54a473501ef4cdeaae4e06891674114d79db8" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/1a156980d78a6666721b7e8e8502fe210b587fcd", - "reference": "1a156980d78a6666721b7e8e8502fe210b587fcd", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/a1a54a473501ef4cdeaae4e06891674114d79db8", + "reference": "a1a54a473501ef4cdeaae4e06891674114d79db8", "shasum": "" }, "require": { @@ -1307,7 +1303,7 @@ "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", "security": "https://github.com/sebastianbergmann/phpunit/security/policy", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.17" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.6.19" }, "funding": [ { @@ -1323,7 +1319,7 @@ "type": "tidelift" } ], - "time": "2024-02-23T13:14:51+00:00" + "time": "2024-04-05T04:35:58+00:00" }, { "name": "sebastian/cli-parser", @@ -2127,16 +2123,16 @@ }, { "name": "sebastian/resource-operations", - "version": "3.0.3", + "version": "3.0.4", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/resource-operations.git", - "reference": "0f4443cb3a1d92ce809899753bc0d5d5a8dd19a8" + "reference": "05d5692a7993ecccd56a03e40cd7e5b09b1d404e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/0f4443cb3a1d92ce809899753bc0d5d5a8dd19a8", - "reference": "0f4443cb3a1d92ce809899753bc0d5d5a8dd19a8", + "url": "https://api.github.com/repos/sebastianbergmann/resource-operations/zipball/05d5692a7993ecccd56a03e40cd7e5b09b1d404e", + "reference": "05d5692a7993ecccd56a03e40cd7e5b09b1d404e", "shasum": "" }, "require": { @@ -2148,7 +2144,7 @@ "type": "library", "extra": { "branch-alias": { - "dev-master": "3.0-dev" + "dev-main": "3.0-dev" } }, "autoload": { @@ -2169,8 +2165,7 @@ "description": "Provides a list of PHP built-in functions that operate on resources", "homepage": "https://www.github.com/sebastianbergmann/resource-operations", "support": { - "issues": "https://github.com/sebastianbergmann/resource-operations/issues", - "source": "https://github.com/sebastianbergmann/resource-operations/tree/3.0.3" + "source": "https://github.com/sebastianbergmann/resource-operations/tree/3.0.4" }, "funding": [ { @@ -2178,7 +2173,7 @@ "type": "github" } ], - "time": "2020-09-28T06:45:17+00:00" + "time": "2024-03-14T16:00:52+00:00" }, { "name": "sebastian/type", diff --git a/docker-compose.yml b/docker-compose.yml index bf22fd1..84dc821 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -2,7 +2,7 @@ version: '3' services: mariadb: - image: mariadb:10.6 + image: mariadb:10.11 environment: - MYSQL_ROOT_PASSWORD=password networks: @@ -18,6 +18,10 @@ services: - abuse depends_on: - mariadb + volumes: + - ./phpunit.xml:/code/phpunit.xml + - ./src:/code/src + - ./tests:/code/tests networks: abuse: \ No newline at end of file diff --git a/src/Audit/Audit.php b/src/Audit/Audit.php index abddc84..2820920 100644 --- a/src/Audit/Audit.php +++ b/src/Audit/Audit.php @@ -2,6 +2,8 @@ namespace Utopia\Audit; +use Exception; +use Throwable; use Utopia\Database\Database; use Utopia\Database\DateTime; use Utopia\Database\Document; @@ -9,20 +11,16 @@ use Utopia\Database\Exception\Duplicate as DuplicateException; use Utopia\Database\Exception\Structure as StructureException; use Utopia\Database\Query; -use Utopia\Database\Validator\Authorization; -use Utopia\Exception; class Audit { public const COLLECTION = 'audit'; private Database $db; - private Authorization $auth; - public function __construct(Database $db, Authorization $auth) + public function __construct(Database $db) { $this->db = $db; - $this->auth = $auth; } /** @@ -30,13 +28,12 @@ public function __construct(Database $db, Authorization $auth) * * @return void * - * @throws DuplicateException - * @throws \Exception + * @throws Exception */ public function setup(): void { - if (! $this->db->exists($this->db->getDatabase())) { - throw new \Exception('You need to create the database before running Audit setup'); + if (!$this->db->exists($this->db->getDatabase())) { + throw new Exception('You need to create the database before running Audit setup'); } $attributes = [ @@ -167,12 +164,12 @@ public function setup(): void * * @throws AuthorizationException * @throws StructureException - * @throws \Exception - * @throws \Throwable + * @throws Exception + * @throws Throwable */ public function log(string $userId, string $event, string $resource, string $userAgent, string $ip, string $location, array $data = []): bool { - $this->auth->skip(function () use ($userId, $event, $resource, $userAgent, $ip, $location, $data) { + $this->db->getAuthorization()->skip(function () use ($userId, $event, $resource, $userAgent, $ip, $location, $data) { $this->db->createDocument(Audit::COLLECTION, new Document([ '$permissions' => [], 'userId' => $userId, @@ -198,12 +195,12 @@ public function log(string $userId, string $event, string $resource, string $use * @param Document|null $orderAfter * @return array * - * @throws \Exception + * @throws Exception */ public function getLogsByUser(string $userId, ?int $limit = null, ?int $offset = null, ?Document $orderAfter = null): array { /** @var array $result */ - $result = $this->auth->skip(function () use ($userId, $limit, $offset, $orderAfter) { + $result = $this->db->getAuthorization()->skip(function () use ($userId, $limit, $offset, $orderAfter) { $queries[] = Query::equal('userId', [$userId]); $queries[] = Query::orderDesc(''); @@ -235,7 +232,7 @@ public function getLogsByUser(string $userId, ?int $limit = null, ?int $offset = public function countLogsByUser(string $userId): int { /** @var int $count */ - $count = $this->auth->skip(function () use ($userId) { + $count = $this->db->getAuthorization()->skip(function () use ($userId) { return $this->db->count( collection: Audit::COLLECTION, queries: [Query::equal('userId', [$userId])] @@ -254,12 +251,12 @@ public function countLogsByUser(string $userId): int * @param Document|null $orderAfter * @return array * - * @throws \Exception + * @throws Exception */ public function getLogsByResource(string $resource, ?int $limit = 25, ?int $offset = null, ?Document $orderAfter = null): array { /** @var array $result */ - $result = $this->auth->skip(function () use ($resource, $limit, $offset, $orderAfter) { + $result = $this->db->getAuthorization()->skip(function () use ($resource, $limit, $offset, $orderAfter) { $queries[] = Query::equal('resource', [$resource]); $queries[] = Query::orderDesc(''); @@ -288,12 +285,12 @@ public function getLogsByResource(string $resource, ?int $limit = 25, ?int $offs * @param string $resource * @return int * - * @throws \Exception + * @throws Exception */ public function countLogsByResource(string $resource): int { /** @var int $count */ - $count = $this->auth->skip(function () use ($resource) { + $count = $this->db->getAuthorization()->skip(function () use ($resource) { return $this->db->count( collection: Audit::COLLECTION, queries: [Query::equal('resource', [$resource])] @@ -313,12 +310,12 @@ public function countLogsByResource(string $resource): int * @param Document|null $orderAfter * @return array * - * @throws \Exception + * @throws Exception */ public function getLogsByUserAndEvents(string $userId, array $events, ?int $limit = null, ?int $offset = null, ?Document $orderAfter = null): array { /** @var array $result */ - $result = $this->auth->skip(function () use ($userId, $events, $limit, $offset, $orderAfter) { + $result = $this->db->getAuthorization()->skip(function () use ($userId, $events, $limit, $offset, $orderAfter) { $queries[] = Query::equal('userId', [$userId]); $queries[] = Query::equal('event', $events); $queries[] = Query::orderDesc(''); @@ -349,12 +346,12 @@ public function getLogsByUserAndEvents(string $userId, array $events, ?int $limi * @param array $events * @return int * - * @throws \Exception + * @throws Exception */ public function countLogsByUserAndEvents(string $userId, array $events): int { /** @var int $count */ - $count = $this->auth->skip(function () use ($userId, $events) { + $count = $this->db->getAuthorization()->skip(function () use ($userId, $events) { return $this->db->count( collection: Audit::COLLECTION, queries: [ @@ -377,12 +374,12 @@ public function countLogsByUserAndEvents(string $userId, array $events): int * @param Document|null $orderAfter * @return array * - * @throws \Exception + * @throws Exception */ public function getLogsByResourceAndEvents(string $resource, array $events, ?int $limit = null, ?int $offset = null, ?Document $orderAfter = null): array { /** @var array $result */ - $result = $this->auth->skip(function () use ($resource, $events, $limit, $offset, $orderAfter) { + $result = $this->db->getAuthorization()->skip(function () use ($resource, $events, $limit, $offset, $orderAfter) { $queries[] = Query::equal('resource', [$resource]); $queries[] = Query::equal('event', $events); $queries[] = Query::orderDesc(''); @@ -413,12 +410,12 @@ public function getLogsByResourceAndEvents(string $resource, array $events, ?int * @param array $events * @return int * - * @throws \Exception + * @throws Exception */ public function countLogsByResourceAndEvents(string $resource, array $events): int { /** @var int $count */ - $count = $this->auth->skip(function () use ($resource, $events) { + $count = $this->db->getAuthorization()->skip(function () use ($resource, $events) { return $this->db->count( collection: Audit::COLLECTION, queries: [ @@ -438,11 +435,11 @@ public function countLogsByResourceAndEvents(string $resource, array $events): i * @return bool * * @throws AuthorizationException - * @throws \Exception + * @throws Throwable */ public function cleanup(string $datetime): bool { - $this->auth->skip(function () use ($datetime) { + $this->db->getAuthorization()->skip(function () use ($datetime) { do { $documents = $this->db->find( collection: Audit::COLLECTION,