diff --git a/app/controllers/api/databases.php b/app/controllers/api/databases.php index 4c80bfe645..b97c3f77c3 100644 --- a/app/controllers/api/databases.php +++ b/app/controllers/api/databases.php @@ -2457,7 +2457,7 @@ App::delete('/v1/databases/:databaseId/collections/:collectionId/documents/:docu App::get('/v1/databases/usage') ->desc('Get usage stats for the database') - ->groups(['api', 'database']) + ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'databases') @@ -2471,9 +2471,6 @@ App::get('/v1/databases/usage') ->action(function (string $range, Response $response, Database $dbForProject) { $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ @@ -2578,7 +2575,7 @@ App::get('/v1/databases/usage') App::get('/v1/databases/:databaseId/usage') ->desc('Get usage stats for the database') - ->groups(['api', 'database']) + ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'databases') @@ -2593,9 +2590,6 @@ App::get('/v1/databases/:databaseId/usage') ->action(function (string $databaseId, string $range, Response $response, Database $dbForProject) { $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ @@ -2691,7 +2685,7 @@ App::get('/v1/databases/:databaseId/usage') App::get('/v1/databases/:databaseId/collections/:collectionId/usage') ->alias('/v1/database/:collectionId/usage', ['databaseId' => 'default']) ->desc('Get usage stats for a collection') - ->groups(['api', 'database']) + ->groups(['api', 'database', 'usage']) ->label('scope', 'collections.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'databases') @@ -2716,9 +2710,6 @@ App::get('/v1/databases/:databaseId/collections/:collectionId/usage') } $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index f91784bb22..1a7f9bdb2e 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -213,7 +213,7 @@ App::get('/v1/functions/:functionId') App::get('/v1/functions/:functionId/usage') ->desc('Get Function Usage') - ->groups(['api', 'functions']) + ->groups(['api', 'functions', 'usage']) ->label('scope', 'functions.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'functions') @@ -234,9 +234,6 @@ App::get('/v1/functions/:functionId/usage') } $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ @@ -326,7 +323,7 @@ App::get('/v1/functions/:functionId/usage') App::get('/v1/functions/usage') ->desc('Get Functions Usage') - ->groups(['api', 'functions']) + ->groups(['api', 'functions', 'usage']) ->label('scope', 'functions.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'functions') @@ -340,9 +337,6 @@ App::get('/v1/functions/usage') ->action(function (string $range, Response $response, Database $dbForProject) { $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index dd703be67b..55b3ef2bb8 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -245,7 +245,7 @@ App::get('/v1/projects/:projectId') App::get('/v1/projects/:projectId/usage') ->desc('Get usage stats for a project') - ->groups(['api', 'projects']) + ->groups(['api', 'projects', 'usage']) ->label('scope', 'projects.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'projects') @@ -268,9 +268,6 @@ App::get('/v1/projects/:projectId/usage') } $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index ff8dd3ca2c..0c20ffec3c 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -1447,7 +1447,7 @@ App::delete('/v1/storage/buckets/:bucketId/files/:fileId') App::get('/v1/storage/usage') ->desc('Get usage stats for storage') - ->groups(['api', 'storage']) + ->groups(['api', 'storage', 'usage']) ->label('scope', 'files.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'storage') @@ -1461,9 +1461,6 @@ App::get('/v1/storage/usage') ->action(function (string $range, Response $response, Database $dbForProject) { $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') === 'enabled') { $periods = [ '24h' => [ @@ -1560,7 +1557,7 @@ App::get('/v1/storage/usage') App::get('/v1/storage/:bucketId/usage') ->desc('Get usage stats for a storage bucket') - ->groups(['api', 'storage']) + ->groups(['api', 'storage', 'usage']) ->label('scope', 'files.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'storage') @@ -1581,9 +1578,6 @@ App::get('/v1/storage/:bucketId/usage') } $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') === 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/api/users.php b/app/controllers/api/users.php index 96d6017848..930862994b 100644 --- a/app/controllers/api/users.php +++ b/app/controllers/api/users.php @@ -1098,7 +1098,7 @@ App::delete('/v1/users/:userId') App::get('/v1/users/usage') ->desc('Get usage stats for the users API') - ->groups(['api', 'users']) + ->groups(['api', 'users', 'usage']) ->label('scope', 'users.read') ->label('sdk.auth', [APP_AUTH_TYPE_ADMIN]) ->label('sdk.namespace', 'users') @@ -1114,9 +1114,6 @@ App::get('/v1/users/usage') ->action(function (string $range, string $provider, Response $response, Database $dbForProject) { $usage = []; - if (App::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { - throw new Exception(Exception::GENERAL_USAGE_DISABLED); - } if (App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled') { $periods = [ '24h' => [ diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 5655874c65..ae1501a5ad 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -554,3 +554,12 @@ App::shutdown() ->submit(); } }); + +App::init() + ->groups(['usage']) + ->inject('utopia') + ->action(function (App $utopia) { + if ($utopia::getEnv('_APP_USAGE_STATS', 'enabled') != 'enabled') { + throw new Exception(Exception::GENERAL_USAGE_DISABLED); + } + });