From 3bb22c38cc39a15480787a4983fc63f5700d43c7 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Thu, 12 Oct 2023 17:03:45 -0700 Subject: [PATCH 1/2] Update teamInternalId when updating project team This is important because when an organization is deleted, projects are fetched by the teamInternalId and if the teamInternalId is not updated when the project team is updated, the project will be deleted if the previous organization is deleted. --- app/controllers/api/projects.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index a01138225f..9da873eb53 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -483,6 +483,7 @@ App::patch('/v1/projects/:projectId/team') $project = $dbForConsole->updateDocument('projects', $project->getId(), $project ->setAttribute('teamId', $teamId) + ->setAttribute('teamInternalId', $team->getInternalId()) ->setAttribute('$permissions', [ Permission::read(Role::team(ID::custom($teamId))), Permission::update(Role::team(ID::custom($teamId), 'owner')), From 3f2f6ab43a324fc813bde758c5ec9f6ad46f3ee9 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Thu, 12 Oct 2023 17:34:53 -0700 Subject: [PATCH 2/2] Ensure permissions are updated when a project moves orgs Since the following collections also have permissions set to the team, the team should be updated for consistency: - installations - repositories - vcsComments --- app/controllers/api/projects.php | 43 ++++++++++++++++++++++++++------ 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 9da873eb53..7f349cb2d2 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -481,16 +481,43 @@ App::patch('/v1/projects/:projectId/team') throw new Exception(Exception::TEAM_NOT_FOUND); } - $project = $dbForConsole->updateDocument('projects', $project->getId(), $project + $permissions = [ + Permission::read(Role::team(ID::custom($teamId))), + Permission::update(Role::team(ID::custom($teamId), 'owner')), + Permission::update(Role::team(ID::custom($teamId), 'developer')), + Permission::delete(Role::team(ID::custom($teamId), 'owner')), + Permission::delete(Role::team(ID::custom($teamId), 'developer')), + ]; + + $project ->setAttribute('teamId', $teamId) ->setAttribute('teamInternalId', $team->getInternalId()) - ->setAttribute('$permissions', [ - Permission::read(Role::team(ID::custom($teamId))), - Permission::update(Role::team(ID::custom($teamId), 'owner')), - Permission::update(Role::team(ID::custom($teamId), 'developer')), - Permission::delete(Role::team(ID::custom($teamId), 'owner')), - Permission::delete(Role::team(ID::custom($teamId), 'developer')), - ])); + ->setAttribute('$permissions', $permissions); + $project = $dbForConsole->updateDocument('projects', $project->getId(), $project); + + $installations = $dbForConsole->find('installations', [ + Query::equal('projectInternalId', [$project->getInternalId()]), + ]); + foreach ($installations as $installation) { + $installation->getAttribute('$permissions', $permissions); + $dbForConsole->updateDocument('installations', $installation->getId(), $installation); + } + + $repositories = $dbForConsole->find('repositories', [ + Query::equal('projectInternalId', [$project->getInternalId()]), + ]); + foreach ($repositories as $repository) { + $repository->getAttribute('$permissions', $permissions); + $dbForConsole->updateDocument('repositories', $repository->getId(), $repository); + } + + $vcsComments = $dbForConsole->find('vcsComments', [ + Query::equal('projectInternalId', [$project->getInternalId()]), + ]); + foreach ($vcsComments as $vcsComment) { + $vcsComment->getAttribute('$permissions', $permissions); + $dbForConsole->updateDocument('vcsComments', $vcsComment->getId(), $vcsComment); + } $response->dynamic($project, Response::MODEL_PROJECT); });