Skip to content
Closed
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
16 changes: 8 additions & 8 deletions plugins/keycloak/src/functions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AdminRole, Project, StepCall, UserEmail, ZoneObject, ProjectMemberPayload } from '@cpn-console/hooks'
import { ENABLED, type ProjectRole } from '@cpn-console/shared'
import { generateRandomPassword, parseError, PluginResultBuilder } from '@cpn-console/hooks'
import type { ProjectRole } from '@cpn-console/shared'
import { generateRandomPassword, parseError, PluginResultBuilder, specificallyEnabled } from '@cpn-console/hooks'
import type GroupRepresentation from '@keycloak/keycloak-admin-client/lib/defs/groupRepresentation.js'
import type ClientRepresentation from '@keycloak/keycloak-admin-client/lib/defs/clientRepresentation.js'
import type { CustomGroup } from './group.js'
Expand Down Expand Up @@ -65,15 +65,15 @@ export const upsertProject: StepCall<Project> = async ({ args: project, config }
try {
const kcClient = await getkcClient()
const projectName = project.slug
const purgeEnabled = config.keycloak?.purge === ENABLED
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

praise: Oui, bien vu 👍

const purge = config.keycloak?.purge
const projectGroup = await getOrCreateProjectGroup(kcClient, projectName)

const groupMembers = await kcClient.groups.listMembers({ id: projectGroup.id })

await Promise.all([
...groupMembers.map((member) => {
if (!project.users.some(({ id }) => id === member.id)) {
if (purgeEnabled) {
if (specificallyEnabled(purge)) {
return kcClient.users.delFromGroup({
// @ts-ignore id is present on user, bad typing in lib
id: member.id,
Expand Down Expand Up @@ -231,7 +231,7 @@ export const deleteZone: StepCall<ZoneObject> = async ({ args: zone }) => {
export const upsertAdminRole: StepCall<AdminRole> = async ({ args: role, config }) => {
if (!role.oidcGroup) return { status: { result: 'OK', message: 'No OIDC Group defined' } }
const pluginResult = new PluginResultBuilder('Up-to-date')
const purgeEnabled = config.keycloak?.purge === ENABLED
const purge = config.keycloak?.purge
try {
const kcClient = await getkcClient()
const group = await getOrCreateGroupByPath(kcClient, role.oidcGroup)
Expand All @@ -240,7 +240,7 @@ export const upsertAdminRole: StepCall<AdminRole> = async ({ args: role, config
await Promise.all([
...groupMembers.map((member) => {
if (member.id && !role.members.some(({ id }) => id === member.id)) {
if (purgeEnabled) {
if (specificallyEnabled(purge)) {
return kcClient.users.delFromGroup({
id: member.id,
groupId: group!.id!,
Expand Down Expand Up @@ -388,7 +388,7 @@ export const deleteProjectRole: StepCall<ProjectRole> = async ({ args: role }) =

export const upsertProjectMember: StepCall<ProjectMemberPayload> = async ({ args: member, config }) => {
const pluginResult = new PluginResultBuilder('Synced')
const purgeEnabled = config.keycloak?.purge === ENABLED
const purge = config.keycloak?.purge
try {
const kcClient = await getkcClient()

Expand All @@ -410,7 +410,7 @@ export const upsertProjectMember: StepCall<ProjectMemberPayload> = async ({ args
if (shouldBeMember && !isMember) {
await kcClient.users.addToGroup({ id: member.userId, groupId: roleGroup.id })
} else if (!shouldBeMember && isMember) {
if (purgeEnabled) {
if (specificallyEnabled(purge)) {
await kcClient.users.delFromGroup({ id: member.userId, groupId: roleGroup.id })
} else {
console.warn(`User ${member.email} is not in project ${member.project.slug} anymore, but purge is disabled`)
Expand Down
Loading