2021-10-04 13:39:54 +00:00
|
|
|
import React, { useState } from "react";
|
2021-09-24 10:46:01 +00:00
|
|
|
import { omit } from "lodash";
|
2021-09-20 12:42:07 +00:00
|
|
|
import {
|
2021-10-04 13:39:54 +00:00
|
|
|
ActionGroup,
|
2021-09-20 12:42:07 +00:00
|
|
|
AlertVariant,
|
|
|
|
Button,
|
|
|
|
ButtonVariant,
|
2021-10-04 13:39:54 +00:00
|
|
|
FormGroup,
|
2021-09-20 12:42:07 +00:00
|
|
|
Label,
|
|
|
|
PageSection,
|
2021-09-29 08:51:47 +00:00
|
|
|
Spinner,
|
2021-09-20 12:42:07 +00:00
|
|
|
ToolbarItem,
|
|
|
|
} from "@patternfly/react-core";
|
|
|
|
import { Divider, Flex, FlexItem, Radio, Title } from "@patternfly/react-core";
|
|
|
|
import { CodeEditor, Language } from "@patternfly/react-code-editor";
|
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2021-09-29 08:51:47 +00:00
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
2021-09-20 12:42:07 +00:00
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2021-10-08 18:21:09 +00:00
|
|
|
import { prettyPrintJSON } from "../util";
|
2021-09-20 12:42:07 +00:00
|
|
|
import { Link } from "react-router-dom";
|
2021-10-26 20:16:19 +00:00
|
|
|
import { toAddClientProfile } from "./routes/AddClientProfile";
|
2021-09-24 10:46:01 +00:00
|
|
|
import type ClientProfileRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientProfileRepresentation";
|
2021-09-29 08:51:47 +00:00
|
|
|
import "./RealmSettingsSection.css";
|
2021-10-26 20:16:19 +00:00
|
|
|
import { toClientProfile } from "./routes/ClientProfile";
|
2021-09-29 08:51:47 +00:00
|
|
|
|
2021-09-24 10:46:01 +00:00
|
|
|
type ClientProfile = ClientProfileRepresentation & {
|
2021-09-20 12:42:07 +00:00
|
|
|
global: boolean;
|
|
|
|
};
|
2021-09-09 10:45:19 +00:00
|
|
|
|
|
|
|
export const ProfilesTab = () => {
|
2021-09-20 12:42:07 +00:00
|
|
|
const { t } = useTranslation("realm-settings");
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
const { realm } = useRealm();
|
|
|
|
const { addAlert, addError } = useAlerts();
|
2021-09-24 10:46:01 +00:00
|
|
|
const [tableProfiles, setTableProfiles] = useState<ClientProfile[]>();
|
|
|
|
const [globalProfiles, setGlobalProfiles] =
|
|
|
|
useState<ClientProfileRepresentation[]>();
|
|
|
|
const [selectedProfile, setSelectedProfile] = useState<ClientProfile>();
|
2021-09-20 12:42:07 +00:00
|
|
|
const [show, setShow] = useState(false);
|
2021-10-04 13:39:54 +00:00
|
|
|
const [code, setCode] = useState<string>();
|
2021-09-24 10:46:01 +00:00
|
|
|
const [key, setKey] = useState(0);
|
2021-09-20 12:42:07 +00:00
|
|
|
|
2021-09-29 08:51:47 +00:00
|
|
|
useFetch(
|
|
|
|
() =>
|
|
|
|
adminClient.clientPolicies.listProfiles({
|
|
|
|
includeGlobalProfiles: true,
|
|
|
|
}),
|
|
|
|
(allProfiles) => {
|
|
|
|
setGlobalProfiles(allProfiles.globalProfiles);
|
2021-09-20 12:42:07 +00:00
|
|
|
|
2021-09-29 08:51:47 +00:00
|
|
|
const globalProfiles = allProfiles.globalProfiles?.map(
|
|
|
|
(globalProfiles) => ({
|
|
|
|
...globalProfiles,
|
|
|
|
global: true,
|
|
|
|
})
|
|
|
|
);
|
2021-09-20 12:42:07 +00:00
|
|
|
|
2021-09-29 08:51:47 +00:00
|
|
|
const profiles = allProfiles.profiles?.map((profiles) => ({
|
|
|
|
...profiles,
|
|
|
|
global: false,
|
|
|
|
}));
|
2021-09-20 12:42:07 +00:00
|
|
|
|
2021-09-29 08:51:47 +00:00
|
|
|
const allClientProfiles = globalProfiles?.concat(profiles ?? []);
|
|
|
|
setTableProfiles(allClientProfiles || []);
|
2021-10-04 13:39:54 +00:00
|
|
|
setCode(JSON.stringify(allClientProfiles, null, 2));
|
2021-09-29 08:51:47 +00:00
|
|
|
},
|
|
|
|
[key]
|
|
|
|
);
|
2021-09-20 12:42:07 +00:00
|
|
|
|
2021-09-29 08:51:47 +00:00
|
|
|
const loader = async () => tableProfiles ?? [];
|
2021-09-20 12:42:07 +00:00
|
|
|
|
2021-10-04 13:39:54 +00:00
|
|
|
const normalizeProfile = (
|
|
|
|
profile: ClientProfile
|
|
|
|
): ClientProfileRepresentation => omit(profile, "global");
|
2021-09-20 12:42:07 +00:00
|
|
|
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: t("deleteClientProfileConfirmTitle"),
|
2021-10-26 20:16:19 +00:00
|
|
|
messageKey: t("deleteClientProfileConfirm", {
|
|
|
|
profileName: selectedProfile?.name,
|
|
|
|
}),
|
2021-09-20 12:42:07 +00:00
|
|
|
continueButtonLabel: t("delete"),
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
2021-09-24 10:46:01 +00:00
|
|
|
const updatedProfiles = tableProfiles
|
|
|
|
?.filter(
|
|
|
|
(profile) => profile.name !== selectedProfile?.name && !profile.global
|
|
|
|
)
|
2021-10-04 13:39:54 +00:00
|
|
|
.map<ClientProfileRepresentation>((profile) =>
|
|
|
|
normalizeProfile(profile)
|
|
|
|
);
|
2021-09-24 10:46:01 +00:00
|
|
|
|
2021-09-20 12:42:07 +00:00
|
|
|
try {
|
2021-09-24 10:46:01 +00:00
|
|
|
await adminClient.clientPolicies.createProfiles({
|
|
|
|
profiles: updatedProfiles,
|
|
|
|
globalProfiles,
|
|
|
|
});
|
2021-09-20 12:42:07 +00:00
|
|
|
addAlert(t("deleteClientSuccess"), AlertVariant.success);
|
2021-09-24 10:46:01 +00:00
|
|
|
setKey(key + 1);
|
2021-09-20 12:42:07 +00:00
|
|
|
} catch (error) {
|
|
|
|
addError(t("deleteClientError"), error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const cellFormatter = (row: ClientProfile) => (
|
2021-10-26 20:16:19 +00:00
|
|
|
<Link
|
|
|
|
to={toClientProfile({
|
|
|
|
realm,
|
|
|
|
profileName: row.name!,
|
|
|
|
})}
|
|
|
|
key={row.name}
|
|
|
|
>
|
2021-09-29 08:51:47 +00:00
|
|
|
{row.name} {row.global && <Label color="blue">{t("global")}</Label>}
|
2021-09-20 12:42:07 +00:00
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
|
2021-09-29 08:51:47 +00:00
|
|
|
if (!tableProfiles) {
|
|
|
|
return (
|
|
|
|
<div className="pf-u-text-align-center">
|
|
|
|
<Spinner />
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-10-04 13:39:54 +00:00
|
|
|
const save = async () => {
|
|
|
|
if (!code) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
const obj: ClientProfile[] = JSON.parse(code);
|
|
|
|
const changedProfiles = obj
|
|
|
|
.filter((profile) => !profile.global)
|
|
|
|
.map((profile) => normalizeProfile(profile));
|
|
|
|
|
|
|
|
const changedGlobalProfiles = obj
|
|
|
|
.filter((profile) => profile.global)
|
|
|
|
.map((profile) => normalizeProfile(profile));
|
|
|
|
|
|
|
|
try {
|
|
|
|
await adminClient.clientPolicies.createProfiles({
|
|
|
|
profiles: changedProfiles,
|
|
|
|
globalProfiles: changedGlobalProfiles,
|
|
|
|
});
|
|
|
|
addAlert(
|
|
|
|
t("realm-settings:updateClientProfilesSuccess"),
|
|
|
|
AlertVariant.success
|
|
|
|
);
|
|
|
|
setKey(key + 1);
|
|
|
|
} catch (error) {
|
|
|
|
addError("realm-settings:updateClientProfilesError", error);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.warn("Invalid json, ignoring value using {}");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-09 10:45:19 +00:00
|
|
|
return (
|
2021-09-20 12:42:07 +00:00
|
|
|
<>
|
|
|
|
<DeleteConfirm />
|
|
|
|
<PageSection>
|
|
|
|
<Flex className="kc-profiles-config-section">
|
|
|
|
<FlexItem>
|
|
|
|
<Title headingLevel="h1" size="md">
|
|
|
|
{t("profilesConfigType")}
|
|
|
|
</Title>
|
|
|
|
</FlexItem>
|
|
|
|
<FlexItem>
|
|
|
|
<Radio
|
|
|
|
isChecked={!show}
|
2021-09-29 08:51:47 +00:00
|
|
|
name="profilesView"
|
2021-09-20 12:42:07 +00:00
|
|
|
onChange={() => setShow(false)}
|
|
|
|
label={t("profilesConfigTypes.formView")}
|
2021-09-29 08:51:47 +00:00
|
|
|
id="formView-profilesView"
|
2021-09-20 12:42:07 +00:00
|
|
|
className="kc-form-radio-btn pf-u-mr-sm pf-u-ml-sm"
|
2021-09-29 08:51:47 +00:00
|
|
|
data-testid="formView-profilesView"
|
2021-09-20 12:42:07 +00:00
|
|
|
/>
|
|
|
|
</FlexItem>
|
|
|
|
<FlexItem>
|
|
|
|
<Radio
|
|
|
|
isChecked={show}
|
2021-09-29 08:51:47 +00:00
|
|
|
name="profilesView"
|
2021-09-20 12:42:07 +00:00
|
|
|
onChange={() => setShow(true)}
|
|
|
|
label={t("profilesConfigTypes.jsonEditor")}
|
2021-09-29 08:51:47 +00:00
|
|
|
id="jsonEditor-profilesView"
|
2021-09-20 12:42:07 +00:00
|
|
|
className="kc-editor-radio-btn"
|
2021-09-29 08:51:47 +00:00
|
|
|
data-testid="jsonEditor-profilesView"
|
2021-09-20 12:42:07 +00:00
|
|
|
/>
|
|
|
|
</FlexItem>
|
|
|
|
</Flex>
|
|
|
|
</PageSection>
|
|
|
|
<Divider />
|
|
|
|
{!show ? (
|
|
|
|
<KeycloakDataTable
|
2021-09-29 08:51:47 +00:00
|
|
|
key={tableProfiles.length}
|
|
|
|
ariaLabelKey="realm-settings:profiles"
|
2021-09-20 12:42:07 +00:00
|
|
|
searchPlaceholderKey="realm-settings:clientProfileSearch"
|
|
|
|
loader={loader}
|
|
|
|
toolbarItem={
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button
|
|
|
|
id="createProfile"
|
2021-10-01 10:52:45 +00:00
|
|
|
component={(props) => (
|
2021-10-26 20:16:19 +00:00
|
|
|
<Link {...props} to={toAddClientProfile({ realm })} />
|
2021-10-01 10:52:45 +00:00
|
|
|
)}
|
2021-09-20 12:42:07 +00:00
|
|
|
data-testid="createProfile"
|
|
|
|
>
|
|
|
|
{t("createClientProfile")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
}
|
2021-09-24 10:46:01 +00:00
|
|
|
isRowDisabled={(value) => value.global}
|
2021-09-20 12:42:07 +00:00
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
title: t("common:delete"),
|
2021-09-24 10:46:01 +00:00
|
|
|
onRowClick: (profile) => {
|
|
|
|
setSelectedProfile(profile);
|
2021-09-20 12:42:07 +00:00
|
|
|
toggleDeleteDialog();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "name",
|
2021-10-29 14:19:57 +00:00
|
|
|
displayKey: t("common:name"),
|
2021-09-20 12:42:07 +00:00
|
|
|
cellRenderer: cellFormatter,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "description",
|
|
|
|
displayKey: t("clientProfileDescription"),
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
emptyState={
|
|
|
|
<ListEmptyState
|
|
|
|
message={t("emptyClientProfiles")}
|
|
|
|
instructions={t("emptyClientProfilesInstructions")}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
) : (
|
2021-10-04 13:39:54 +00:00
|
|
|
<FormGroup fieldId={"jsonEditor"}>
|
2021-09-20 12:42:07 +00:00
|
|
|
<div className="pf-u-mt-md pf-u-ml-lg">
|
|
|
|
<CodeEditor
|
|
|
|
isLineNumbersVisible
|
|
|
|
isLanguageLabelVisible
|
2021-10-04 13:39:54 +00:00
|
|
|
isReadOnly={false}
|
2021-09-20 12:42:07 +00:00
|
|
|
code={code}
|
|
|
|
language={Language.json}
|
|
|
|
height="30rem"
|
2021-10-04 13:39:54 +00:00
|
|
|
onChange={(value) => {
|
|
|
|
setCode(value ?? "");
|
|
|
|
}}
|
2021-09-20 12:42:07 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2021-10-04 13:39:54 +00:00
|
|
|
<ActionGroup>
|
|
|
|
<div className="pf-u-mt-md">
|
|
|
|
<Button
|
|
|
|
variant={ButtonVariant.primary}
|
|
|
|
className="pf-u-mr-md pf-u-ml-lg"
|
|
|
|
onClick={save}
|
|
|
|
data-testid="jsonEditor-saveBtn"
|
|
|
|
>
|
|
|
|
{t("save")}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
variant={ButtonVariant.link}
|
|
|
|
onClick={() => {
|
2021-10-08 18:21:09 +00:00
|
|
|
setCode(prettyPrintJSON(tableProfiles));
|
2021-10-04 13:39:54 +00:00
|
|
|
}}
|
|
|
|
data-testid="jsonEditor-reloadBtn"
|
|
|
|
>
|
|
|
|
{t("reload")}
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
</ActionGroup>
|
|
|
|
</FormGroup>
|
2021-09-20 12:42:07 +00:00
|
|
|
)}
|
|
|
|
</>
|
2021-09-09 10:45:19 +00:00
|
|
|
);
|
|
|
|
};
|