2020-12-15 07:21:17 +00:00
|
|
|
import React, { useEffect, useState } from "react";
|
|
|
|
import { useHistory, useParams } from "react-router-dom";
|
|
|
|
import {
|
|
|
|
AlertVariant,
|
2020-12-04 20:37:29 +00:00
|
|
|
ButtonVariant,
|
|
|
|
DropdownItem,
|
|
|
|
PageSection,
|
2020-12-15 07:21:17 +00:00
|
|
|
Tab,
|
|
|
|
TabTitleText,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2020-12-04 20:37:29 +00:00
|
|
|
import { useForm } from "react-hook-form";
|
2020-12-15 07:21:17 +00:00
|
|
|
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2020-12-04 20:37:29 +00:00
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
2020-12-15 07:21:17 +00:00
|
|
|
import RoleRepresentation from "keycloak-admin/lib/defs/roleRepresentation";
|
2020-12-04 20:37:29 +00:00
|
|
|
import { KeyValueType, RoleAttributes } from "./RoleAttributes";
|
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
|
|
|
import { RealmRoleForm } from "./RealmRoleForm";
|
2021-01-05 19:49:33 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2021-01-13 20:47:40 +00:00
|
|
|
import { KeycloakTabs } from "../components/keycloak-tabs/KeycloakTabs";
|
2020-12-15 07:21:17 +00:00
|
|
|
|
2020-12-04 20:37:29 +00:00
|
|
|
const arrayToAttributes = (attributeArray: KeyValueType[]) => {
|
|
|
|
const initValue: { [index: string]: string[] } = {};
|
|
|
|
return attributeArray.reduce((acc, attribute) => {
|
|
|
|
acc[attribute.key] = [attribute.value];
|
|
|
|
return acc;
|
|
|
|
}, initValue);
|
|
|
|
};
|
|
|
|
|
|
|
|
const attributesToArray = (attributes: { [key: string]: string }): any => {
|
|
|
|
if (!attributes || Object.keys(attributes).length == 0) {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
key: "",
|
|
|
|
value: "",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return Object.keys(attributes).map((key) => ({
|
|
|
|
key: key,
|
|
|
|
value: attributes[key],
|
|
|
|
}));
|
|
|
|
};
|
|
|
|
|
|
|
|
export const RealmRoleTabs = () => {
|
2020-12-15 07:21:17 +00:00
|
|
|
const { t } = useTranslation("roles");
|
2020-12-04 20:37:29 +00:00
|
|
|
const form = useForm<RoleRepresentation>({ mode: "onChange" });
|
2020-12-15 07:21:17 +00:00
|
|
|
const history = useHistory();
|
|
|
|
const [name, setName] = useState("");
|
|
|
|
const adminClient = useAdminClient();
|
2021-01-05 19:49:33 +00:00
|
|
|
const { realm } = useRealm();
|
2021-01-19 19:30:52 +00:00
|
|
|
const [role, setRole] = useState<RoleRepresentation>();
|
2020-12-15 07:21:17 +00:00
|
|
|
|
|
|
|
const { id } = useParams<{ id: string }>();
|
|
|
|
|
|
|
|
const { addAlert } = useAlerts();
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-12-04 20:37:29 +00:00
|
|
|
(async () => {
|
|
|
|
if (id) {
|
|
|
|
const fetchedRole = await adminClient.roles.findOneById({ id });
|
2021-01-05 13:39:27 +00:00
|
|
|
setName(fetchedRole.name!);
|
|
|
|
setupForm(fetchedRole);
|
2021-01-19 19:30:52 +00:00
|
|
|
setRole(fetchedRole);
|
2020-12-04 20:37:29 +00:00
|
|
|
} else {
|
|
|
|
setName(t("createRole"));
|
2021-01-05 13:39:27 +00:00
|
|
|
}
|
2020-12-04 20:37:29 +00:00
|
|
|
})();
|
2020-12-15 07:21:17 +00:00
|
|
|
}, []);
|
|
|
|
|
|
|
|
const setupForm = (role: RoleRepresentation) => {
|
|
|
|
Object.entries(role).map((entry) => {
|
2020-12-04 20:37:29 +00:00
|
|
|
if (entry[0] === "attributes") {
|
|
|
|
form.setValue(entry[0], attributesToArray(entry[1]));
|
|
|
|
} else {
|
|
|
|
form.setValue(entry[0], entry[1]);
|
|
|
|
}
|
2020-12-15 07:21:17 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-01-19 19:30:52 +00:00
|
|
|
// reset form to default values
|
|
|
|
const reset = () => {
|
|
|
|
setupForm(role!);
|
|
|
|
};
|
|
|
|
|
2020-12-15 07:21:17 +00:00
|
|
|
const save = async (role: RoleRepresentation) => {
|
|
|
|
try {
|
2020-12-04 20:37:29 +00:00
|
|
|
if (id) {
|
|
|
|
if (role.attributes) {
|
|
|
|
// react-hook-form will use `KeyValueType[]` here we convert it back into an indexed property of string[]
|
|
|
|
role.attributes = arrayToAttributes(
|
|
|
|
(role.attributes as unknown) as KeyValueType[]
|
|
|
|
);
|
|
|
|
}
|
2021-01-19 19:30:52 +00:00
|
|
|
setRole(role!);
|
|
|
|
setupForm(role!);
|
2020-12-04 20:37:29 +00:00
|
|
|
await adminClient.roles.updateById({ id }, role);
|
|
|
|
} else {
|
|
|
|
await adminClient.roles.create(role);
|
|
|
|
const createdRole = await adminClient.roles.findOneByName({
|
|
|
|
name: role.name!,
|
|
|
|
});
|
|
|
|
history.push(`/${realm}/roles/${createdRole.id}`);
|
|
|
|
}
|
|
|
|
addAlert(t(id ? "roleSaveSuccess" : "roleCreated"), AlertVariant.success);
|
2020-12-15 07:21:17 +00:00
|
|
|
} catch (error) {
|
2020-12-04 20:37:29 +00:00
|
|
|
addAlert(
|
|
|
|
t((id ? "roleSave" : "roleCreate") + "Error", {
|
|
|
|
error: error.response.data?.errorMessage || error,
|
|
|
|
}),
|
|
|
|
AlertVariant.danger
|
|
|
|
);
|
2020-12-15 07:21:17 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-04 20:37:29 +00:00
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "roles:roleDeleteConfirm",
|
|
|
|
messageKey: t("roles:roleDeleteConfirmDialog", { name }),
|
|
|
|
continueButtonLabel: "common:delete",
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await adminClient.roles.delById({ id });
|
|
|
|
addAlert(t("roleDeletedSuccess"), AlertVariant.success);
|
|
|
|
history.replace(`/${realm}/roles`);
|
|
|
|
} catch (error) {
|
|
|
|
addAlert(`${t("roleDeleteError")} ${error}`, AlertVariant.danger);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2020-12-15 07:21:17 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2020-12-04 20:37:29 +00:00
|
|
|
<DeleteConfirm />
|
|
|
|
<ViewHeader
|
|
|
|
titleKey={name}
|
|
|
|
subKey={id ? "" : "roles:roleCreateExplain"}
|
|
|
|
dropdownItems={
|
|
|
|
id
|
|
|
|
? [
|
|
|
|
<DropdownItem
|
|
|
|
key="action"
|
|
|
|
component="button"
|
|
|
|
onClick={() => toggleDeleteDialog()}
|
|
|
|
>
|
|
|
|
{t("deleteRole")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]
|
|
|
|
: undefined
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
<PageSection variant="light">
|
|
|
|
{id && (
|
2021-01-13 20:47:40 +00:00
|
|
|
<KeycloakTabs isBox>
|
2020-12-04 20:37:29 +00:00
|
|
|
<Tab
|
2021-01-13 20:47:40 +00:00
|
|
|
eventKey="details"
|
2020-12-04 20:37:29 +00:00
|
|
|
title={<TabTitleText>{t("details")}</TabTitleText>}
|
2020-12-15 07:21:17 +00:00
|
|
|
>
|
2021-01-19 19:30:52 +00:00
|
|
|
<RealmRoleForm
|
|
|
|
reset={reset}
|
|
|
|
form={form}
|
|
|
|
save={save}
|
|
|
|
editMode={true}
|
|
|
|
/>
|
2020-12-04 20:37:29 +00:00
|
|
|
</Tab>
|
|
|
|
<Tab
|
2021-01-13 20:47:40 +00:00
|
|
|
eventKey="attributes"
|
2020-12-04 20:37:29 +00:00
|
|
|
title={<TabTitleText>{t("attributes")}</TabTitleText>}
|
2021-01-05 19:49:33 +00:00
|
|
|
>
|
2021-01-19 19:30:52 +00:00
|
|
|
<RoleAttributes form={form} save={save} reset={reset} />
|
2020-12-04 20:37:29 +00:00
|
|
|
</Tab>
|
2021-01-13 20:47:40 +00:00
|
|
|
</KeycloakTabs>
|
2020-12-04 20:37:29 +00:00
|
|
|
)}
|
2021-01-19 19:30:52 +00:00
|
|
|
{!id && (
|
|
|
|
<RealmRoleForm
|
|
|
|
reset={reset}
|
|
|
|
form={form}
|
|
|
|
save={save}
|
|
|
|
editMode={false}
|
|
|
|
/>
|
|
|
|
)}
|
2020-12-04 20:37:29 +00:00
|
|
|
</PageSection>
|
2020-12-15 07:21:17 +00:00
|
|
|
</>
|
|
|
|
);
|
2021-01-11 21:28:15 +00:00
|
|
|
};
|