From 8fec25a977ec6758b51b78fdeb61669d2280f237 Mon Sep 17 00:00:00 2001 From: shreddedbacon Date: Fri, 8 Dec 2023 09:39:38 +1100 Subject: [PATCH] fix: return groups user has access to in group payloads --- services/api/src/resources/group/resolvers.ts | 12 ++++++++---- services/api/src/resources/organization/resolvers.ts | 8 +++++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/services/api/src/resources/group/resolvers.ts b/services/api/src/resources/group/resolvers.ts index c9f06bb188..04e4db9f7d 100644 --- a/services/api/src/resources/group/resolvers.ts +++ b/services/api/src/resources/group/resolvers.ts @@ -190,10 +190,10 @@ export const getGroupsByProjectId: ResolverFn = async ( _input, { hasPermission, sqlClientPool, models, keycloakGrant, keycloakGroups, keycloakUsersGroups, adminScopes } ) => { + const projectGroups = await models.GroupModel.loadGroupsByProjectIdFromGroups(pid, keycloakGroups); // use the admin scope check instead of `hasPermission` for speed if (adminScopes.groupViewAll) { try { - const projectGroups = await models.GroupModel.loadGroupsByProjectIdFromGroups(pid, keycloakGroups); return projectGroups; } catch (err) { if (!keycloakGrant) { @@ -202,7 +202,6 @@ export const getGroupsByProjectId: ResolverFn = async ( } } } else { - const projectGroups = await models.GroupModel.loadGroupsByProjectIdFromGroups(pid, keycloakGroups); const user = await models.UserModel.loadUserById( keycloakGrant.access_token.content.sub ); @@ -237,7 +236,13 @@ export const getGroupsByProjectId: ResolverFn = async ( } } } - const userProjectGroups = R.intersection(projectGroups, userGroups); + let userProjectGroups = [] + for (const ug of userGroups) { + const pg = projectGroups.find(i => i.id === ug.id) + if (pg) { + userProjectGroups.push(pg) + } + } return userProjectGroups; } @@ -262,7 +267,6 @@ export const getGroupsByUserId: ResolverFn = async ( } } const currentUserGroups = keycloakUsersGroups; - // const bothUserGroups = R.intersection(queryUserGroups, currentUserGroups); return currentUserGroups; }; diff --git a/services/api/src/resources/organization/resolvers.ts b/services/api/src/resources/organization/resolvers.ts index 10259d662c..9cc23a0411 100644 --- a/services/api/src/resources/organization/resolvers.ts +++ b/services/api/src/resources/organization/resolvers.ts @@ -551,7 +551,13 @@ export const getGroupsByOrganizationsProject: ResolverFn = async ( } } } - const userProjectGroups = R.intersection(orgProjectGroups, userGroups); + let userProjectGroups = [] + for (const ug of userGroups) { + const pg = orgProjectGroups.find(i => i.id === ug.id) + if (pg) { + userProjectGroups.push(pg) + } + } return userProjectGroups; };