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
This commit is contained in:
Erik Jan de Wit 2020-11-17 22:39:28 +01:00 committed by GitHub
parent ba761c0526
commit 49ef2bd665
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 21 deletions

View file

@ -1,5 +1,5 @@
import React, { useContext, useEffect, useState } from "react"; 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 { useTranslation } from "react-i18next";
import { Controller, useForm } from "react-hook-form"; import { Controller, useForm } from "react-hook-form";
import { import {
@ -29,11 +29,7 @@ import { ViewHeader } from "../../components/view-header/ViewHeader";
import { HelpItem } from "../../components/help-enabler/HelpItem"; import { HelpItem } from "../../components/help-enabler/HelpItem";
import { ProtocolMapperRepresentation } from "../models/client-scope"; import { ProtocolMapperRepresentation } from "../models/client-scope";
export type RoleMappingFormProps = { export const RoleMappingForm = () => {
clientScopeId: string;
};
export const RoleMappingForm = ({ clientScopeId }: RoleMappingFormProps) => {
const { realm } = useContext(RealmContext); const { realm } = useContext(RealmContext);
const adminClient = useAdminClient(); const adminClient = useAdminClient();
const history = useHistory(); const history = useHistory();
@ -41,35 +37,48 @@ export const RoleMappingForm = ({ clientScopeId }: RoleMappingFormProps) => {
const { t } = useTranslation("client-scopes"); const { t } = useTranslation("client-scopes");
const { register, handleSubmit, control, errors } = useForm(); const { register, handleSubmit, control, errors } = useForm();
const { clientScopeId } = useParams<{ clientScopeId: string }>();
const [roleOpen, setRoleOpen] = useState(false); const [roleOpen, setRoleOpen] = useState(false);
const [roles, setRoles] = useState<RoleRepresentation[]>([]);
const [clientsOpen, setClientsOpen] = useState(false); const [clientsOpen, setClientsOpen] = useState(false);
const [clients, setClients] = useState<ClientRepresentation[]>([]); const [clients, setClients] = useState<ClientRepresentation[]>([]);
const [selectedClient, setSelectedClient] = useState<ClientRepresentation>(); const [selectedClient, setSelectedClient] = useState<ClientRepresentation>();
const [clientRoles, setClientRoles] = useState<RoleRepresentation[]>(); const [clientRoles, setClientRoles] = useState<RoleRepresentation[]>([]);
useEffect(() => { useEffect(() => {
(async () => { (async () => {
const roles = await adminClient.roles.find();
setRoles(roles);
const clients = await adminClient.clients.find(); const clients = await adminClient.clients.find();
clients.map(
const asyncFilter = async (
predicate: (client: ClientRepresentation) => Promise<boolean>
) => {
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) =>
(client.toString = function () { (client.toString = function () {
return this.name!; return this.clientId!;
}) })
); );
setClients(clients); setClients(filteredClients);
})(); })();
}); }, []);
useEffect(() => { useEffect(() => {
(async () => { (async () => {
const client = selectedClient as ClientRepresentation; const client = selectedClient as ClientRepresentation;
if (client && client.name !== "realmRoles") { if (client && client.name !== "realmRoles") {
setClientRoles(await adminClient.clients.listRoles({ id: client.id! })); setClientRoles(await adminClient.clients.listRoles({ id: client.id! }));
} else {
setClientRoles(await adminClient.roles.find());
} }
})(); })();
}, [selectedClient]); }, [selectedClient]);
@ -105,7 +114,7 @@ export const RoleMappingForm = ({ clientScopeId }: RoleMappingFormProps) => {
<SelectGroup key="group" label={t("clientGroup")}> <SelectGroup key="group" label={t("clientGroup")}>
{clients.map((client) => ( {clients.map((client) => (
<SelectOption key={client.id} value={client}> <SelectOption key={client.id} value={client}>
{client.name} {client.clientId}
</SelectOption> </SelectOption>
))} ))}
</SelectGroup>, </SelectGroup>,
@ -118,11 +127,7 @@ export const RoleMappingForm = ({ clientScopeId }: RoleMappingFormProps) => {
{role.name} {role.name}
</SelectOption> </SelectOption>
); );
if (clientRoles) {
return clientRoles.map((role) => createItem(role)); return clientRoles.map((role) => createItem(role));
} else {
return roles.map((role) => createItem(role));
}
}; };
return ( return (

View file

@ -20,6 +20,7 @@ import { UserFederationSection } from "./user-federation/UserFederationSection";
import { UsersSection } from "./user/UsersSection"; import { UsersSection } from "./user/UsersSection";
import { MappingDetails } from "./client-scopes/details/MappingDetails"; import { MappingDetails } from "./client-scopes/details/MappingDetails";
import { ClientDetails } from "./clients/ClientDetails"; import { ClientDetails } from "./clients/ClientDetails";
import { RoleMappingForm } from "./client-scopes/add/RoleMappingForm";
export type RouteDef = { export type RouteDef = {
path: string; path: string;
@ -73,6 +74,12 @@ export const routes: RoutesFn = (t: TFunction) => [
breadcrumb: t("client-scopes:clientScopeDetails"), breadcrumb: t("client-scopes:clientScopeDetails"),
access: "view-clients", 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", path: "/client-scopes/:scopeId/:id",
component: MappingDetails, component: MappingDetails,

View file

@ -33,7 +33,7 @@ export const RoleMappingFormExample = () => (
} }
> >
<Page> <Page>
<RoleMappingForm clientScopeId="dummy" /> <RoleMappingForm />
</Page> </Page>
</AdminClient.Provider> </AdminClient.Provider>
</ServerInfoContext.Provider> </ServerInfoContext.Provider>