keycloak-scim/apps/admin-ui/src/authentication/RequiredActions.tsx

174 lines
5.1 KiB
TypeScript
Raw Normal View History

import { useState } from "react";
2021-10-19 15:30:57 +00:00
import { useTranslation } from "react-i18next";
import { AlertVariant, Switch } from "@patternfly/react-core";
2021-10-19 15:30:57 +00:00
import type RequiredActionProviderRepresentation from "@keycloak/keycloak-admin-client/lib/defs/requiredActionProviderRepresentation";
import type RequiredActionProviderSimpleRepresentation from "@keycloak/keycloak-admin-client/lib/defs/requiredActionProviderSimpleRepresentation";
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
import { DraggableTable } from "./components/DraggableTable";
import { useAlerts } from "../components/alert/Alerts";
import { KeycloakSpinner } from "../components/keycloak-spinner/KeycloakSpinner";
2022-06-24 11:44:15 +00:00
import { toKey } from "../util";
2021-10-19 15:30:57 +00:00
type DataType = RequiredActionProviderRepresentation &
RequiredActionProviderSimpleRepresentation;
type Row = {
name: string;
enabled: boolean;
defaultAction: boolean;
data: DataType;
};
export const RequiredActions = () => {
const { t } = useTranslation("authentication");
const { adminClient } = useAdminClient();
2021-10-19 15:30:57 +00:00
const { addAlert, addError } = useAlerts();
const [actions, setActions] = useState<Row[]>();
const [key, setKey] = useState(0);
const refresh = () => setKey(key + 1);
useFetch(
async () => {
const [requiredActions, unregisteredRequiredActions] = await Promise.all([
adminClient.authenticationManagement.getRequiredActions(),
adminClient.authenticationManagement.getUnregisteredRequiredActions(),
]);
return [
...requiredActions.map((a) => ({
name: a.name!,
enabled: a.enabled!,
defaultAction: a.defaultAction!,
data: a,
})),
...unregisteredRequiredActions.map((a) => ({
name: a.name!,
enabled: false,
defaultAction: false,
data: a,
})),
];
},
(actions) => setActions(actions),
[key]
);
const isUnregisteredAction = (data: DataType): boolean => {
return !("alias" in data);
};
const updateAction = async (
action: DataType,
field: "enabled" | "defaultAction"
) => {
try {
if (field in action) {
action[field] = !action[field];
await adminClient.authenticationManagement.updateRequiredAction(
{ alias: action.alias! },
action
);
} else if (isUnregisteredAction(action)) {
await adminClient.authenticationManagement.registerRequiredAction({
name: action.name,
providerId: action.providerId,
});
}
refresh();
addAlert(t("updatedRequiredActionSuccess"), AlertVariant.success);
} catch (error) {
addError("authentication:updatedRequiredActionError", error);
}
};
const executeMove = async (
action: RequiredActionProviderRepresentation,
times: number
) => {
try {
const alias = action.alias!;
for (let index = 0; index < Math.abs(times); index++) {
if (times > 0) {
await adminClient.authenticationManagement.lowerRequiredActionPriority(
{
alias,
}
);
} else {
await adminClient.authenticationManagement.raiseRequiredActionPriority(
{
alias,
}
);
}
}
refresh();
addAlert(t("updatedRequiredActionSuccess"), AlertVariant.success);
} catch (error) {
addError("authentication:updatedRequiredActionError", error);
}
};
if (!actions) {
return <KeycloakSpinner />;
2021-10-19 15:30:57 +00:00
}
return (
<DraggableTable
keyField="name"
onDragFinish={async (nameDragged, items) => {
const keys = actions.map((e) => e.name);
const newIndex = items.indexOf(nameDragged);
const oldIndex = keys.indexOf(nameDragged);
const dragged = actions[oldIndex].data;
if (!dragged.alias) return;
const times = newIndex - oldIndex;
executeMove(dragged, times);
}}
columns={[
{
name: "name",
displayKey: "authentication:requiredActions",
},
{
name: "enabled",
displayKey: "common:enabled",
cellRenderer: (row) => (
<Switch
2022-06-24 11:44:15 +00:00
id={`enable-${toKey(row.name)}`}
2021-10-19 15:30:57 +00:00
label={t("common:on")}
labelOff={t("common:off")}
isChecked={row.enabled}
onChange={() => {
updateAction(row.data, "enabled");
}}
aria-label={toKey(row.name)}
2021-10-19 15:30:57 +00:00
/>
),
},
{
name: "default",
displayKey: "authentication:setAsDefaultAction",
cellRenderer: (row) => (
<Switch
2022-06-24 11:44:15 +00:00
id={`default-${toKey(row.name)}`}
2021-10-19 15:30:57 +00:00
label={t("common:on")}
isDisabled={!row.enabled}
labelOff={!row.enabled ? t("disabledOff") : t("common:off")}
2021-10-19 15:30:57 +00:00
isChecked={row.defaultAction}
onChange={() => {
updateAction(row.data, "defaultAction");
}}
aria-label={toKey(row.name)}
2021-10-19 15:30:57 +00:00
/>
),
},
]}
data={actions}
/>
);
};