diff --git a/app/init/resources/request.php b/app/init/resources/request.php index 68f5968519..68a8af3605 100644 --- a/app/init/resources/request.php +++ b/app/init/resources/request.php @@ -1,5 +1,6 @@ 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;