hardcoded version working

This commit is contained in:
mfrances 2021-05-20 11:00:02 -04:00
parent 37ad6bd14a
commit da45bf0e09
3 changed files with 70 additions and 67 deletions

View file

@ -80,6 +80,19 @@ export const AddRoleMappingModal = ({
} }
); );
} }
// 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 { return {
roles, roles,
client, client,

View file

@ -1,66 +1,42 @@
import { AlertVariant, Button, FormGroup, TextInput } from "@patternfly/react-core"; import { Button, FormGroup, TextInput } from "@patternfly/react-core";
import React, { useState } from "react"; import React, { useState } from "react";
import { HelpItem } from "../../../components/help-enabler/HelpItem"; import { HelpItem } from "../../../components/help-enabler/HelpItem";
import type { UseFormMethods } from "react-hook-form"; import type { UseFormMethods } from "react-hook-form";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { useAdminClient } from "../../../context/auth/AdminClient"; import type ClientRepresentation from "keycloak-admin/lib/defs/clientRepresentation";
import { useAlerts } from "../../../components/alert/Alerts"; import type RoleRepresentation from "keycloak-admin/lib/defs/roleRepresentation";
import { RoleMappingPayload } from "keycloak-admin/lib/defs/roleRepresentation"; import { AddRoleMappingModal } from "../../../components/role-mapping/AddRoleMappingModal";
import "../../user-federation.css";
import { AddRoleMappingModal, MappingType } from "../../../components/role-mapping/AddRoleMappingModal";
export type LdapMapperHardcodedLdapRoleProps = { export type LdapMapperHardcodedLdapRoleProps = {
form: UseFormMethods; form: UseFormMethods;
}; };
export type CompositeRole = RoleRepresentation & {
parent: RoleRepresentation;
};
export type Row = {
client?: ClientRepresentation;
role: CompositeRole | RoleRepresentation;
};
export const LdapMapperHardcodedLdapRole = ({ export const LdapMapperHardcodedLdapRole = ({
form, form,
}: LdapMapperHardcodedLdapRoleProps) => { }: LdapMapperHardcodedLdapRoleProps) => {
const { t } = useTranslation("user-federation"); const { t } = useTranslation("user-federation");
const helpText = useTranslation("user-federation-help").t; const helpText = useTranslation("user-federation-help").t;
const adminClient = useAdminClient();
const [showAssign, setShowAssign] = useState(false); const [showAssign, setShowAssign] = useState(false);
const { addAlert } = useAlerts(); const selectRoles = async (rows: Row[]) => {
if (rows[0].client) {
form.setValue(
const assignRoles = async (rows: Row[]) => { "config.role[0]",
try { `${rows[0].client.clientId}.${rows[0].role.name}`
const realmRoles = rows
.filter((row) => row.client === undefined)
.map((row) => row.role as RoleMappingPayload)
.flat();
await adminClient.clientScopes.addRealmScopeMappings(
{
id,
},
realmRoles
);
await Promise.all(
rows
.filter((row) => row.client !== undefined)
.map((row) =>
adminClient.clientScopes.addClientScopeMappings(
{
id,
client: row.client!.id!,
},
[row.role as RoleMappingPayload]
)
)
);
addAlert(t("roleMappingUpdatedSuccess"), AlertVariant.success);
} catch (error) {
addAlert(
t("roleMappingUpdatedError", {
error: error.response?.data?.errorMessage || error,
}),
AlertVariant.danger
); );
} else {
form.setValue("config.role[0]", `${rows[0].role.name}`);
} }
}; };
@ -69,15 +45,16 @@ export const LdapMapperHardcodedLdapRole = ({
{showAssign && ( {showAssign && (
// MF 042921 hardcoded for now, to see modal displayed // MF 042921 hardcoded for now, to see modal displayed
<AddRoleMappingModal <AddRoleMappingModal
id="e2d7fe7c-f7bc-4903-9562-3d079ae8667c" id="1a85c63a-99bd-4d16-9924-b38b8f7cceaf" // this is the ID for client-scopes > marks-client-scope
type="client-scope" type="client-scope"
name="name" name="name"
// id={id} // id={id}
// type={type} // type={type}
// name={name} // name={name}
onAssign={assignRoles} onAssign={selectRoles}
onClose={() => setShowAssign(false)} onClose={() => setShowAssign(false)}
/>)} />
)}
<FormGroup <FormGroup
label={t("common:role")} label={t("common:role")}
@ -91,6 +68,7 @@ export const LdapMapperHardcodedLdapRole = ({
fieldId="kc-role" fieldId="kc-role"
isRequired isRequired
> >
<div className="keycloak__user-federation__assign-role">
<TextInput <TextInput
isRequired isRequired
type="text" type="text"
@ -100,11 +78,13 @@ export const LdapMapperHardcodedLdapRole = ({
ref={form.register} ref={form.register}
/> />
<Button <Button
data-testid="assignRole" className="keycloak__user-federation__assign-role-btn"
data-testid="selectRole"
onClick={() => setShowAssign(true)} onClick={() => setShowAssign(true)}
> >
{t("assignRole")} {t("selectRole")}
</Button> </Button>
</div>
</FormGroup> </FormGroup>
</> </>
); );

View file

@ -9,3 +9,13 @@
.error { .error {
color: red; color: red;
} }
.keycloak__user-federation__assign-role {
display: flex;
flex-direction: row;
align-items: right;
}
.keycloak__user-federation__assign-role-btn {
margin-left: 10px;
}