Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: strip existing project when adding it to an organization #3726

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 2 additions & 25 deletions services/api/src/models/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,34 +828,11 @@ export const Group = (clients: {
groups: Group[]
): Promise<void> => {
for (const g in groups) {
const group = groups[g]
const groupProjectIds = getProjectIdsFromGroup(group)
const newGroupProjects = R.pipe(
R.without([projectId]),
R.uniq,
R.join(',')
// @ts-ignore
)(groupProjectIds);

try {
await keycloakAdminClient.groups.update(
{
id: group.id
},
{
name: group.name,
attributes: {
...group.attributes,
'lagoon-projects': [newGroupProjects],
'group-lagoon-project-ids': [`{${JSON.stringify(group.name)}:[${newGroupProjects}]}`]
}
}
);
// purge the caches to ensure current data
await purgeGroupCache(group)
await removeProjectFromGroup(projectId, groups[g])
} catch (err) {
throw new Error(
`Error setting projects for group ${group.name}: ${err.message}`
`Error setting projects for group ${groups[g].name}: ${err.message}`
);
}
}
Expand Down
54 changes: 44 additions & 10 deletions services/api/src/resources/organization/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -704,27 +704,60 @@ export const removeProjectFromOrganization: ResolverFn = async (
}

// add existing project to an organization
// this removes all notifications and groups from the project and resets all the access to the project to only
// the default project group and the default-user of the project
export const addExistingProjectToOrganization: ResolverFn = async (
root,
{ input },
{ sqlClientPool, hasPermission, userActivityLogger, models }
) => {

let pid = input.project;
let oid = input.organization;

// platform admin only as it potentially reveals information about projects/orgs/groups
await hasPermission('organization', 'add');

const groupProjectIds = []
const projectInOtherOrgs = []
const projectGroupNames = []
const otherOrgs = []
let pid = input.project;
let oid = input.organization;
const project = await projectHelpers(sqlClientPool).getProjectById(pid)

// get all the groups the requested project is in
const projectGroups = await groupHelpers(sqlClientPool).selectGroupsByProjectId(models, pid)
await checkProjectGroupAssociation(oid, projectGroups, projectGroupNames, otherOrgs, groupProjectIds, projectInOtherOrgs, sqlClientPool)
try {
const projectGroups = await groupHelpers(sqlClientPool).selectGroupsByProjectId(models, pid)

let removeGroups = []
for (const g in projectGroups) {
if (projectGroups[g].attributes["type"] == "project-default-group") {
// remove all users from the project default group except the `default-user@project`
await models.GroupModel.removeNonProjectDefaultUsersFromGroup(projectGroups[g], project.name)
// update group
await models.GroupModel.updateGroup({
id: projectGroups[g].id,
name: projectGroups[g].name,
attributes: {
...projectGroups[g].attributes,
"lagoon-organization": [""]
}
});
} else {
removeGroups.push(projectGroups[g])
}
}
// remove groups from project
await models.GroupModel.removeProjectFromGroups(pid, removeGroups);
} catch (err) {
throw new Error(
`Unable to remove all groups from the project`
)
}
try {
// remove all notifications from project
await notificationHelpers(sqlClientPool).removeAllNotificationsFromProject({project: pid})
} catch (err) {
throw new Error(
`Unable to remove all notifications from the project`
)
}

const projectGroups = await groupHelpers(sqlClientPool).selectGroupsByProjectId(models, pid)
// check if project.organization is already set?
// get all groups the project is in
for (const group of projectGroups) {
Expand All @@ -737,6 +770,7 @@ export const addExistingProjectToOrganization: ResolverFn = async (
"lagoon-organization": [input.organization]
}
});
await groupHelpers(sqlClientPool).addOrganizationToGroup(input.organization, group.id)

// log this activity
userActivityLogger(`User added a group to organization`, {
Expand Down Expand Up @@ -765,7 +799,7 @@ export const addExistingProjectToOrganization: ResolverFn = async (
);

// log this activity
userActivityLogger(`User added a project to organization`, {
userActivityLogger(`User added an existing project to organization`, {
project: '',
organization: input.organization,
event: 'api:addExistingProjectToOrganization',
Expand Down