2021-09-23 13:41:29 +00:00
|
|
|
import React, { useMemo, useState } from "react";
|
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
|
|
|
Divider,
|
|
|
|
Flex,
|
|
|
|
FlexItem,
|
|
|
|
PageSection,
|
|
|
|
Radio,
|
2021-09-29 08:51:47 +00:00
|
|
|
Spinner,
|
2021-09-23 13:41:29 +00:00
|
|
|
Title,
|
|
|
|
ToolbarItem,
|
|
|
|
} from "@patternfly/react-core";
|
2021-09-09 10:45:19 +00:00
|
|
|
|
2021-09-23 13:41:29 +00:00
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2021-09-29 08:51:47 +00:00
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
2021-10-08 18:21:09 +00:00
|
|
|
import { prettyPrintJSON, upperCaseFormatter } from "../util";
|
2021-09-23 13:41:29 +00:00
|
|
|
import { CodeEditor, Language } from "@patternfly/react-code-editor";
|
|
|
|
import { Link } from "react-router-dom";
|
|
|
|
import type ClientPolicyRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientPolicyRepresentation";
|
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2021-09-09 10:45:19 +00:00
|
|
|
|
2021-09-29 08:51:47 +00:00
|
|
|
import "./RealmSettingsSection.css";
|
|
|
|
|
2021-09-09 10:45:19 +00:00
|
|
|
export const PoliciesTab = () => {
|
2021-09-23 13:41:29 +00:00
|
|
|
const { t } = useTranslation("realm-settings");
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
const { addAlert, addError } = useAlerts();
|
|
|
|
|
|
|
|
const [show, setShow] = useState(false);
|
2021-09-29 08:51:47 +00:00
|
|
|
const [policies, setPolicies] = useState<ClientPolicyRepresentation[]>();
|
2021-09-23 13:41:29 +00:00
|
|
|
const [selectedPolicy, setSelectedPolicy] =
|
|
|
|
useState<ClientPolicyRepresentation>();
|
|
|
|
|
2021-09-29 08:51:47 +00:00
|
|
|
useFetch(
|
|
|
|
() => adminClient.clientPolicies.listPolicies(),
|
|
|
|
(policies) => setPolicies(policies.policies),
|
|
|
|
[]
|
|
|
|
);
|
2021-09-23 13:41:29 +00:00
|
|
|
|
2021-09-29 08:51:47 +00:00
|
|
|
const loader = async () => policies ?? [];
|
2021-09-23 13:41:29 +00:00
|
|
|
|
2021-10-08 18:21:09 +00:00
|
|
|
const code = useMemo(() => prettyPrintJSON(policies), [policies]);
|
2021-09-23 13:41:29 +00:00
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: t("deleteClientPolicyConfirmTitle"),
|
|
|
|
messageKey: t("deleteClientPolicyConfirm", {
|
|
|
|
policyName: selectedPolicy?.name,
|
|
|
|
}),
|
|
|
|
continueButtonLabel: t("delete"),
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
// delete client policy here
|
|
|
|
addAlert(t("deleteClientPolicySuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
|
|
|
addError(t("deleteClientPolicyError"), error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-09-29 08:51:47 +00:00
|
|
|
if (!policies) {
|
|
|
|
return (
|
|
|
|
<div className="pf-u-text-align-center">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
2021-09-09 10:45:19 +00:00
|
|
|
return (
|
2021-09-23 13:41:29 +00:00
|
|
|
<>
|
|
|
|
<DeleteConfirm />
|
|
|
|
<PageSection>
|
|
|
|
<Flex className="kc-policies-config-section">
|
|
|
|
<FlexItem>
|
|
|
|
<Title headingLevel="h1" size="md">
|
|
|
|
{t("policiesConfigType")}
|
|
|
|
</Title>
|
|
|
|
</FlexItem>
|
|
|
|
<FlexItem>
|
|
|
|
<Radio
|
|
|
|
isChecked={!show}
|
2021-09-29 08:51:47 +00:00
|
|
|
name="policiesView"
|
2021-09-23 13:41:29 +00:00
|
|
|
onChange={() => setShow(false)}
|
|
|
|
label={t("policiesConfigTypes.formView")}
|
2021-09-29 08:51:47 +00:00
|
|
|
id="formView-policiesView"
|
2021-09-23 13:41:29 +00:00
|
|
|
className="kc-form-radio-btn pf-u-mr-sm pf-u-ml-sm"
|
|
|
|
/>
|
|
|
|
</FlexItem>
|
|
|
|
<FlexItem>
|
|
|
|
<Radio
|
|
|
|
isChecked={show}
|
2021-09-29 08:51:47 +00:00
|
|
|
name="policiesView"
|
2021-09-23 13:41:29 +00:00
|
|
|
onChange={() => setShow(true)}
|
|
|
|
label={t("policiesConfigTypes.jsonEditor")}
|
2021-09-29 08:51:47 +00:00
|
|
|
id="jsonEditor-policiesView"
|
2021-09-23 13:41:29 +00:00
|
|
|
className="kc-editor-radio-btn"
|
|
|
|
/>
|
|
|
|
</FlexItem>
|
|
|
|
</Flex>
|
|
|
|
</PageSection>
|
|
|
|
<Divider />
|
|
|
|
{!show ? (
|
|
|
|
<KeycloakDataTable
|
2021-09-29 08:51:47 +00:00
|
|
|
key={policies.length}
|
2021-09-23 13:41:29 +00:00
|
|
|
emptyState={
|
|
|
|
<ListEmptyState
|
|
|
|
message={t("realm-settings:noClientPolicies")}
|
|
|
|
instructions={t("realm-settings:noClientPoliciesInstructions")}
|
|
|
|
primaryActionText={t("realm-settings:createClientPolicy")}
|
|
|
|
/>
|
|
|
|
}
|
2021-09-29 08:51:47 +00:00
|
|
|
ariaLabelKey="realm-settings:clientPolicies"
|
2021-09-23 13:41:29 +00:00
|
|
|
searchPlaceholderKey="realm-settings:clientPolicySearch"
|
|
|
|
loader={loader}
|
|
|
|
toolbarItem={
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button
|
|
|
|
id="createPolicy"
|
|
|
|
component={Link}
|
|
|
|
data-testid="createPolicy"
|
|
|
|
>
|
|
|
|
{t("createClientPolicy")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
}
|
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
title: t("common:delete"),
|
|
|
|
onRowClick: (item) => {
|
|
|
|
toggleDeleteDialog();
|
|
|
|
setSelectedPolicy(item);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "name",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "enabled",
|
|
|
|
cellFormatters: [upperCaseFormatter()],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description",
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<div className="pf-u-mt-md pf-u-ml-lg">
|
|
|
|
<CodeEditor
|
|
|
|
isLineNumbersVisible
|
|
|
|
isLanguageLabelVisible
|
|
|
|
code={code}
|
|
|
|
language={Language.json}
|
|
|
|
height="30rem"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="pf-u-mt-md">
|
|
|
|
<Button
|
|
|
|
variant={ButtonVariant.primary}
|
|
|
|
className="pf-u-mr-md pf-u-ml-lg"
|
|
|
|
>
|
|
|
|
{t("save")}
|
|
|
|
</Button>
|
|
|
|
<Button variant={ButtonVariant.secondary}> {t("reload")}</Button>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</>
|
2021-09-09 10:45:19 +00:00
|
|
|
);
|
|
|
|
};
|