diff --git a/src/client-scopes/add/RoleMappingForm.tsx b/src/client-scopes/add/RoleMappingForm.tsx index e77ab99efa..9ab1cb8259 100644 --- a/src/client-scopes/add/RoleMappingForm.tsx +++ b/src/client-scopes/add/RoleMappingForm.tsx @@ -31,16 +31,21 @@ export const RoleMappingForm = () => { const { realm } = useRealm(); const adminClient = useAdminClient(); const history = useHistory(); - const { addAlert } = useAlerts(); + const { addAlert, addError } = useAlerts(); const { t } = useTranslation("client-scopes"); - const { register, handleSubmit, control, errors } = useForm(); + const { register, handleSubmit, control, errors } = + useForm({ + defaultValues: { + protocolMapper: "oidc-role-name-mapper", + }, + }); const { id } = useParams<{ id: string }>(); const [roleOpen, setRoleOpen] = useState(false); const [clientsOpen, setClientsOpen] = useState(false); - const [clients, setClients] = useState([]); + const [clients, setClients] = useState(); const [selectedClient, setSelectedClient] = useState(); const [clientRoles, setClientRoles] = useState([]); @@ -74,10 +79,9 @@ export const RoleMappingForm = () => { useFetch( async () => { - const client = selectedClient as ClientRepresentation; - if (client && client.name !== "realmRoles") { + if (selectedClient && selectedClient.name !== "realmRoles") { const clientRoles = await adminClient.clients.listRoles({ - id: client.id!, + id: selectedClient.id!, }); return clientRoles; } else { @@ -90,10 +94,20 @@ export const RoleMappingForm = () => { const save = async (mapping: ProtocolMapperRepresentation) => { try { - await adminClient.clientScopes.addProtocolMapper({ id }, mapping); + await adminClient.clientScopes.addProtocolMapper( + { id }, + { + ...mapping, + protocol: "openid-connect", + config: { + ...mapping.config, + role: `${selectedClient?.clientId}.${mapping.config?.role.name}`, + }, + } + ); addAlert(t("mapperCreateSuccess")); } catch (error: any) { - addAlert(t("mapperCreateError", error)); + addError("client-scopes:mapperCreateError", error); } }; @@ -189,87 +203,90 @@ export const RoleMappingForm = () => { } /> - - } - validated={errors["config.role"] ? "error" : "default"} - helperTextInvalid={t("common:required")} - fieldId="role" - > - - - - - - ( - - )} + {clients && ( + - - - + } + validated={errors.config?.role ? "error" : "default"} + helperTextInvalid={t("common:required")} + fieldId="role" + > + + + + + + ( + + )} + /> + + + + )}