Fix inability to CRUD projects

This commit is contained in:
Matej Bačo
2026-05-14 17:26:08 +02:00
parent aaa870e656
commit b618bf1353
+54 -3
View File
@@ -1,5 +1,6 @@
<?php
use Ahc\Jwt\JWT;
use Ahc\Jwt\JWTException;
use Appwrite\Auth\Key;
@@ -39,8 +40,10 @@ use Utopia\Database\Adapter\Pool as DatabasePool;
use Utopia\Database\Database;
use Utopia\Database\DateTime as DatabaseDateTime;
use Utopia\Database\Document;
use Utopia\Database\Exception\Query as QueryException;
use Utopia\Database\Query;
use Utopia\Database\Validator\Authorization;
use Utopia\Database\Validator\Queries;
use Utopia\DI\Container;
use Utopia\Domains\Domain;
use Utopia\DSN\DSN;
@@ -1092,7 +1095,7 @@ return function (Container $context): void {
return $key;
}, ['request', 'project', 'servers', 'dbForPlatform', 'authorization']);
$context->set('team', function (Document $project, Database $dbForPlatform, Http $utopia, Request $request, Authorization $authorization) {
$context->set('team', function (Document $project, Database $dbForPlatform, Http $utopia, Request $request, Authorization $authorization, Document $user) {
$teamInternalId = '';
if ($project->getId() !== 'console') {
$teamInternalId = $project->getAttribute('teamInternalId', '');
@@ -1100,7 +1103,55 @@ return function (Container $context): void {
$route = $utopia->match($request);
$path = ! empty($route) ? $route->getPath() : $request->getURI();
$orgHeader = $request->getHeader('x-appwrite-organization', '');
if (str_starts_with($path, '/v1/projects/:projectId')) {
// Backwards compatibility: /v1/organitation/projects acting as /v1/projects
if (\str_starts_with($path, '/v1/organization/projects')) {
// Backwards compatibility: Take from payload param
$teamId = $request->getParam('organizationId', $request->getParam('teamId', ''));
// Backawrds compatibility: Get from URL param project ID
if (empty($teamId)) {
$projectId = \explode('/', $request->getURI())[4] ?? '';
if (!empty($projectId)) {
$urlProject = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $projectId));
$teamId = $urlProject->getAttribute('teamId', '');
}
}
// Backwards compatibility: Get from queries param
if (empty($teamId)) {
$queries = $request->getParam('queries', []);
$queries = \is_array($queries) ? $queries : [$queries];
try {
$queries = Query::parseQueries($queries);
$queries = Query::getByType($queries, [Query::TYPE_EQUAL]);
foreach ($queries as $query) {
if ($query->getAttribute() === 'teamId') {
$teamId = $query->getValues()[0] ?? '';
break;
}
}
} catch (QueryException $e) {
// Ignore, do not parse from queries
}
}
// Backwards compatibility: We cannot have organization project API call without organitation, take first one
if (empty($teamId)) {
if (!$user->isEmpty()) {
$teamId = ($user->getAttribute('memberships', [])[0] ?? new Document())->getAttribute('teamId', '');
}
}
if (!empty($teamId)) {
$team = $authorization->skip(function () use ($dbForPlatform, $teamId) {
return $dbForPlatform->getDocument('teams', $teamId);
});
return $team;
}
} elseif (str_starts_with($path, '/v1/projects/:projectId')) {
$uri = $request->getURI();
$pid = explode('/', $uri)[3];
$p = $authorization->skip(fn () => $dbForPlatform->getDocument('projects', $pid));
@@ -1133,7 +1184,7 @@ return function (Container $context): void {
});
return $team;
}, ['project', 'dbForPlatform', 'utopia', 'request', 'authorization']);
}, ['project', 'dbForPlatform', 'utopia', 'request', 'authorization', 'user']);
$context->set('previewHostname', function (Request $request, ?Key $apiKey) {
$allowed = false;