From 7edc76a079109b92e7381fa9b744cb6a5bf6dc3f Mon Sep 17 00:00:00 2001 From: mfrances Date: Wed, 2 Jun 2021 17:20:38 -0400 Subject: [PATCH] role mapping working --- .../role-mapping/AddRoleMappingModal.tsx | 109 ++++++++++-------- src/components/role-mapping/RoleMapping.tsx | 80 ++++++------- .../mappers/LdapMapperHardcodedLdapRole.tsx | 9 +- 3 files changed, 106 insertions(+), 92 deletions(-) diff --git a/src/components/role-mapping/AddRoleMappingModal.tsx b/src/components/role-mapping/AddRoleMappingModal.tsx index 5385a5e4ba..aa7223f619 100644 --- a/src/components/role-mapping/AddRoleMappingModal.tsx +++ b/src/components/role-mapping/AddRoleMappingModal.tsx @@ -23,12 +23,12 @@ import { FilterIcon } from "@patternfly/react-icons"; import { Row, ServiceRole } from "./RoleMapping"; import type RoleRepresentation from "keycloak-admin/lib/defs/roleRepresentation"; -export type MappingType = "service-account" | "client-scope"; +export type MappingType = "service-account" | "client-scope" | "user-fed"; type AddRoleMappingModalProps = { id: string; type: MappingType; - name: string; + name?: string; isRadio?: boolean; onAssign: (rows: Row[]) => void; onClose: () => void; @@ -69,32 +69,29 @@ export const AddRoleMappingModal = ({ await Promise.all( clients.map(async (client) => { let roles: RoleRepresentation[] = []; - if (type === "service-account") { - roles = await adminClient.users.listAvailableClientRoleMappings({ - id: id, - clientUniqueId: client.id!, - }); - } else if (type === "client-scope") { - roles = await adminClient.clientScopes.listAvailableClientScopeMappings( - { - id, - client: client.id!, - } - ); + + switch (type) { + case "service-account": + roles = await adminClient.users.listAvailableClientRoleMappings( + { + id: id, + clientUniqueId: client.id!, + } + ); + break; + + case "client-scope": + roles = await adminClient.clientScopes.listAvailableClientScopeMappings( + { + id, + client: client.id!, + } + ); + break; + case "user-fed": + roles = await adminClient.roles.find(); + break; } - // MF 052021 TODOs: - // change if/elses to switches - // add a type for user-federation that pulls in all roles - // make id optional - - // adminClient.roles.find - - // roles = await adminClient.clients.listRoles( - // { - // - // id: client.id! - // } - return { roles, client, @@ -133,15 +130,25 @@ export const AddRoleMappingModal = ({ } let availableRoles: RoleRepresentation[] = []; - if (type === "service-account") { - availableRoles = await adminClient.users.listAvailableRealmRoleMappings({ - id, - }); - } else if (type === "client-scope") { - availableRoles = await adminClient.clientScopes.listAvailableRealmScopeMappings( - { id } - ); + + switch (type) { + case "service-account": + availableRoles = await adminClient.users.listAvailableRealmRoleMappings( + { + id, + } + ); + break; + case "client-scope": + availableRoles = await adminClient.clientScopes.listAvailableRealmScopeMappings( + { id } + ); + break; + case "user-fed": + availableRoles = await adminClient.roles.find(); + break; } + const realmRoles = availableRoles.map((role) => { return { role, @@ -158,18 +165,28 @@ export const AddRoleMappingModal = ({ await Promise.all( allClients.map(async (client) => { let clientAvailableRoles: RoleRepresentation[] = []; - if (type === "service-account") { - clientAvailableRoles = await adminClient.users.listAvailableClientRoleMappings( - { - id, - clientUniqueId: client.id!, - } - ); - } else if (type === "client-scope") { - clientAvailableRoles = await adminClient.clientScopes.listAvailableClientScopeMappings( - { id, client: client.id! } - ); + + switch (type) { + case "service-account": + clientAvailableRoles = await adminClient.users.listAvailableClientRoleMappings( + { + id, + clientUniqueId: client.id!, + } + ); + break; + case "client-scope": + clientAvailableRoles = await adminClient.clientScopes.listAvailableClientScopeMappings( + { id, client: client.id! } + ); + break; + case "user-fed": + clientAvailableRoles = await adminClient.clients.listRoles({ + id: client.id!, + }); + break; } + return clientAvailableRoles.map((role) => { return { role, diff --git a/src/components/role-mapping/RoleMapping.tsx b/src/components/role-mapping/RoleMapping.tsx index 3a1c410459..435a32da01 100644 --- a/src/components/role-mapping/RoleMapping.tsx +++ b/src/components/role-mapping/RoleMapping.tsx @@ -19,7 +19,6 @@ import "./role-mapping.css"; import { useConfirmDialog } from "../confirm-dialog/ConfirmDialog"; import { useAdminClient } from "../../context/auth/AdminClient"; import { useAlerts } from "../alert/Alerts"; -import _ from "lodash"; export type CompositeRole = RoleRepresentation & { parent: RoleRepresentation; @@ -85,46 +84,49 @@ export const RoleMapping = ({ continueButtonVariant: ButtonVariant.danger, onConfirm: async () => { try { - if (type === "service-account") { - await Promise.all( - selected.map((row) => { - const role = { id: row.role.id!, name: row.role.name! }; - if (row.client) { - return adminClient.users.delClientRoleMappings({ - id, - clientUniqueId: row.client!.id!, - roles: [role], - }); - } else { - return adminClient.users.delRealmRoleMappings({ - id, - roles: [role], - }); - } - }) - ); - } else if (type === "client-scope") { - await Promise.all( - selected.map((row) => { - const role = { id: row.role.id!, name: row.role.name! }; - if (row.client) { - return adminClient.clientScopes.delClientScopeMappings( - { + switch (type) { + case "service-account": + await Promise.all( + selected.map((row) => { + const role = { id: row.role.id!, name: row.role.name! }; + if (row.client) { + return adminClient.users.delClientRoleMappings({ id, - client: row.client!.id!, - }, - [role] - ); - } else { - return adminClient.clientScopes.delRealmScopeMappings( - { + clientUniqueId: row.client!.id!, + roles: [role], + }); + } else { + return adminClient.users.delRealmRoleMappings({ id, - }, - [role] - ); - } - }) - ); + roles: [role], + }); + } + }) + ); + break; + case "client-scope": + await Promise.all( + selected.map((row) => { + const role = { id: row.role.id!, name: row.role.name! }; + if (row.client) { + return adminClient.clientScopes.delClientScopeMappings( + { + id, + client: row.client!.id!, + }, + [role] + ); + } else { + return adminClient.clientScopes.delRealmScopeMappings( + { + id, + }, + [role] + ); + } + }) + ); + break; } addAlert(t("clientScopeRemoveSuccess"), AlertVariant.success); refresh(); diff --git a/src/user-federation/ldap/mappers/LdapMapperHardcodedLdapRole.tsx b/src/user-federation/ldap/mappers/LdapMapperHardcodedLdapRole.tsx index 61bff491c4..a56feadf0b 100644 --- a/src/user-federation/ldap/mappers/LdapMapperHardcodedLdapRole.tsx +++ b/src/user-federation/ldap/mappers/LdapMapperHardcodedLdapRole.tsx @@ -43,14 +43,9 @@ export const LdapMapperHardcodedLdapRole = ({ return ( <> {showAssign && ( - // MF 042921 hardcoded for now, to see modal displayed marks-client-scope - type="client-scope" - name="name" - // id={id} - // type={type} - // name={name} + id="" + type="user-fed" onAssign={selectRoles} isRadio={true} onClose={() => setShowAssign(false)}