Skip to content

Commit

Permalink
feat: update manageUsers
Browse files Browse the repository at this point in the history
  • Loading branch information
ferruhcihan committed Oct 9, 2024
1 parent 2479e86 commit 4c3fd7f
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 18 deletions.
13 changes: 4 additions & 9 deletions src/operator/keycloak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,13 +750,8 @@ export async function addUserGroups(
}

async function createUpdateUser(api: any, user: any) {
const { email, firstName, lastName, isPlatformAdmin, isTeamAdmin, teams, initialPassword } = user
const teamGroups = [
...(isPlatformAdmin ? ['platform-admin'] : []),
...(isTeamAdmin ? ['team-admin'] : []),
...(teams.length > 0 ? teams.map((team) => `team-${team}`) : []),
]
const userConf = createTeamUser(email, firstName, lastName, isPlatformAdmin, isTeamAdmin, teamGroups, initialPassword)
const { email, firstName, lastName, groups, initialPassword } = user
const userConf = createTeamUser(email, firstName, lastName, groups, initialPassword)
const existingUsersByUserEmail = (await doApiCall([], `Getting users`, () =>
api.users.realmUsersGet(keycloakRealm, false, `${email}`),
)) as UserRepresentation[]
Expand All @@ -771,8 +766,8 @@ async function createUpdateUser(api: any, user: any) {
await doApiCall(errors, `Updating user ${email}`, async () =>
api.users.realmUsersIdPut(keycloakRealm, existingUser.id as string, updatedUserConf),
)
await removeUserGroups(api, existingUser, teamGroups)
await addUserGroups(api, existingUser, teamGroups)
await removeUserGroups(api, existingUser, groups)
await addUserGroups(api, existingUser, groups)
} else {
await doApiCall(errors, `Creating user ${email}`, () => api.users.realmUsersPost(keycloakRealm, userConf))
}
Expand Down
8 changes: 3 additions & 5 deletions src/tasks/keycloak/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,7 @@ export const teamUserCfgTpl = (
email: string,
firstName: string,
lastName: string,
isPlatformAdmin: boolean,
isTeamAdmin: boolean,
teamGroups: string[],
groups: string[],
initialPassword: string,
): Record<string, unknown> => ({
username: email,
Expand All @@ -85,8 +83,8 @@ export const teamUserCfgTpl = (
emailVerified: true,
firstName,
lastName,
realmRoles: [...(isPlatformAdmin ? ['platformAdmin'] : []), ...(isTeamAdmin ? ['teamAdmin'] : []), 'teamMember'],
groups: teamGroups,
realmRoles: ['teamMember'],
groups,
credentials: [
{
type: 'password',
Expand Down
6 changes: 2 additions & 4 deletions src/tasks/keycloak/realm-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,14 +123,12 @@ export function createTeamUser(
email: string,
firstName: string,
lastName: string,
isPlatformAdmin: boolean,
isTeamAdmin: boolean,
teamGroups: string[],
groups: string[],
initialPassword: string,
): UserRepresentation {
const userRepresentation = defaultsDeep(
new UserRepresentation(),
teamUserCfgTpl(email, firstName, lastName, isPlatformAdmin, isTeamAdmin, teamGroups, initialPassword),
teamUserCfgTpl(email, firstName, lastName, groups, initialPassword),
)
return userRepresentation
}
Expand Down

0 comments on commit 4c3fd7f

Please sign in to comment.