From 49ef2bd6656dfad1ac7a9c79db42a409b519af40 Mon Sep 17 00:00:00 2001 From: Erik Jan de Wit Date: Tue, 17 Nov 2020 22:39:28 +0100 Subject: [PATCH] Add role mapping into the flow and filter the clients that don't have roles (#222) * addd role mapping into the flow * added filter for clients that don't have roles * fixed types --- src/client-scopes/add/RoleMappingForm.tsx | 45 +++++++++++++---------- src/route-config.ts | 7 ++++ src/stories/RoleMappingForm.stories.tsx | 2 +- 3 files changed, 33 insertions(+), 21 deletions(-) diff --git a/src/client-scopes/add/RoleMappingForm.tsx b/src/client-scopes/add/RoleMappingForm.tsx index cd6c50f693..540c4bbf5b 100644 --- a/src/client-scopes/add/RoleMappingForm.tsx +++ b/src/client-scopes/add/RoleMappingForm.tsx @@ -1,5 +1,5 @@ import React, { useContext, useEffect, useState } from "react"; -import { useHistory } from "react-router-dom"; +import { useHistory, useParams } from "react-router-dom"; import { useTranslation } from "react-i18next"; import { Controller, useForm } from "react-hook-form"; import { @@ -29,11 +29,7 @@ import { ViewHeader } from "../../components/view-header/ViewHeader"; import { HelpItem } from "../../components/help-enabler/HelpItem"; import { ProtocolMapperRepresentation } from "../models/client-scope"; -export type RoleMappingFormProps = { - clientScopeId: string; -}; - -export const RoleMappingForm = ({ clientScopeId }: RoleMappingFormProps) => { +export const RoleMappingForm = () => { const { realm } = useContext(RealmContext); const adminClient = useAdminClient(); const history = useHistory(); @@ -41,35 +37,48 @@ export const RoleMappingForm = ({ clientScopeId }: RoleMappingFormProps) => { const { t } = useTranslation("client-scopes"); const { register, handleSubmit, control, errors } = useForm(); + const { clientScopeId } = useParams<{ clientScopeId: string }>(); const [roleOpen, setRoleOpen] = useState(false); - const [roles, setRoles] = useState([]); const [clientsOpen, setClientsOpen] = useState(false); const [clients, setClients] = useState([]); const [selectedClient, setSelectedClient] = useState(); - const [clientRoles, setClientRoles] = useState(); + const [clientRoles, setClientRoles] = useState([]); useEffect(() => { (async () => { - const roles = await adminClient.roles.find(); - setRoles(roles); const clients = await adminClient.clients.find(); - clients.map( + + const asyncFilter = async ( + predicate: (client: ClientRepresentation) => Promise + ) => { + const results = await Promise.all(clients.map(predicate)); + return clients.filter((_, index) => results[index]); + }; + + const filteredClients = await asyncFilter( + async (client) => + (await adminClient.clients.listRoles({ id: client.id! })).length > 0 + ); + + filteredClients.map( (client) => (client.toString = function () { - return this.name!; + return this.clientId!; }) ); - setClients(clients); + setClients(filteredClients); })(); - }); + }, []); useEffect(() => { (async () => { const client = selectedClient as ClientRepresentation; if (client && client.name !== "realmRoles") { setClientRoles(await adminClient.clients.listRoles({ id: client.id! })); + } else { + setClientRoles(await adminClient.roles.find()); } })(); }, [selectedClient]); @@ -105,7 +114,7 @@ export const RoleMappingForm = ({ clientScopeId }: RoleMappingFormProps) => { {clients.map((client) => ( - {client.name} + {client.clientId} ))} , @@ -118,11 +127,7 @@ export const RoleMappingForm = ({ clientScopeId }: RoleMappingFormProps) => { {role.name} ); - if (clientRoles) { - return clientRoles.map((role) => createItem(role)); - } else { - return roles.map((role) => createItem(role)); - } + return clientRoles.map((role) => createItem(role)); }; return ( diff --git a/src/route-config.ts b/src/route-config.ts index 7dab1e15d6..5b3cd0d0ec 100644 --- a/src/route-config.ts +++ b/src/route-config.ts @@ -20,6 +20,7 @@ import { UserFederationSection } from "./user-federation/UserFederationSection"; import { UsersSection } from "./user/UsersSection"; import { MappingDetails } from "./client-scopes/details/MappingDetails"; import { ClientDetails } from "./clients/ClientDetails"; +import { RoleMappingForm } from "./client-scopes/add/RoleMappingForm"; export type RouteDef = { path: string; @@ -73,6 +74,12 @@ export const routes: RoutesFn = (t: TFunction) => [ breadcrumb: t("client-scopes:clientScopeDetails"), access: "view-clients", }, + { + path: "/client-scopes/:scopeId/oidc-role-name-mapper", + component: RoleMappingForm, + breadcrumb: t("client-scopes:mappingDetails"), + access: "view-clients", + }, { path: "/client-scopes/:scopeId/:id", component: MappingDetails, diff --git a/src/stories/RoleMappingForm.stories.tsx b/src/stories/RoleMappingForm.stories.tsx index 79c1eabc0c..bf7a1fa728 100644 --- a/src/stories/RoleMappingForm.stories.tsx +++ b/src/stories/RoleMappingForm.stories.tsx @@ -33,7 +33,7 @@ export const RoleMappingFormExample = () => ( } > - +