2020-10-15 05:50:47 +00:00
|
|
|
import {
|
2020-11-10 20:26:20 +00:00
|
|
|
AlertVariant,
|
|
|
|
ButtonVariant,
|
2020-10-15 05:50:47 +00:00
|
|
|
Card,
|
|
|
|
CardTitle,
|
2020-11-10 20:26:20 +00:00
|
|
|
DropdownItem,
|
2020-10-15 05:50:47 +00:00
|
|
|
Gallery,
|
2020-11-10 20:26:20 +00:00
|
|
|
GalleryItem,
|
2020-10-15 05:50:47 +00:00
|
|
|
PageSection,
|
|
|
|
Split,
|
|
|
|
SplitItem,
|
|
|
|
Text,
|
|
|
|
TextContent,
|
|
|
|
TextVariants,
|
|
|
|
} from "@patternfly/react-core";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { DatabaseIcon } from "@patternfly/react-icons";
|
2021-08-26 08:39:35 +00:00
|
|
|
import type ComponentRepresentation from "@keycloak/keycloak-admin-client/lib/defs/componentRepresentation";
|
2022-08-03 12:12:07 +00:00
|
|
|
import { useMemo, useState } from "react";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2022-08-16 13:09:14 +00:00
|
|
|
import { useNavigate } from "react-router-dom-v5-compat";
|
2020-11-10 20:26:20 +00:00
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
2022-02-09 09:16:10 +00:00
|
|
|
import { ManagePriorityDialog } from "./ManagePriorityDialog";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { KeycloakCard } from "../components/keycloak-card/KeycloakCard";
|
2020-11-10 20:26:20 +00:00
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
2021-04-29 06:28:59 +00:00
|
|
|
import { useAdminClient, useFetch } from "../context/auth/AdminClient";
|
2021-07-21 09:30:18 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2022-01-05 17:06:53 +00:00
|
|
|
import { useServerInfo } from "../context/server-info/ServerInfoProvider";
|
|
|
|
import { toUpperCase } from "../util";
|
|
|
|
import { toProvider } from "./routes/NewProvider";
|
|
|
|
|
2021-02-23 08:52:40 +00:00
|
|
|
import "./user-federation.css";
|
2021-12-21 15:32:53 +00:00
|
|
|
import helpUrls from "../help-urls";
|
2021-02-09 18:01:23 +00:00
|
|
|
|
2021-10-29 16:11:06 +00:00
|
|
|
export default function UserFederationSection() {
|
2021-07-05 11:24:10 +00:00
|
|
|
const [userFederations, setUserFederations] =
|
|
|
|
useState<ComponentRepresentation[]>();
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2020-11-12 12:55:52 +00:00
|
|
|
const { t } = useTranslation("user-federation");
|
2021-07-21 09:30:18 +00:00
|
|
|
const { realm } = useRealm();
|
2022-07-14 13:02:28 +00:00
|
|
|
const { adminClient } = useAdminClient();
|
2021-01-05 13:39:27 +00:00
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
const refresh = () => setKey(new Date().getTime());
|
2020-11-10 20:26:20 +00:00
|
|
|
|
2022-08-16 13:09:14 +00:00
|
|
|
const navigate = useNavigate();
|
2021-02-09 18:01:23 +00:00
|
|
|
|
2022-02-09 09:16:10 +00:00
|
|
|
const [manageDisplayDialog, setManageDisplayDialog] = useState(false);
|
|
|
|
|
2022-01-05 17:06:53 +00:00
|
|
|
const providers =
|
|
|
|
useServerInfo().componentTypes?.[
|
|
|
|
"org.keycloak.storage.UserStorageProvider"
|
|
|
|
] || [];
|
|
|
|
|
2021-04-29 06:28:59 +00:00
|
|
|
useFetch(
|
2021-08-24 10:39:16 +00:00
|
|
|
async () => {
|
|
|
|
const realmModel = await adminClient.realms.findOne({ realm });
|
2021-04-29 06:28:59 +00:00
|
|
|
const testParams: { [name: string]: string | number } = {
|
2021-09-30 08:58:48 +00:00
|
|
|
parentId: realmModel!.id!,
|
2021-04-29 06:28:59 +00:00
|
|
|
type: "org.keycloak.storage.UserStorageProvider",
|
|
|
|
};
|
|
|
|
return adminClient.components.find(testParams);
|
|
|
|
},
|
|
|
|
(userFederations) => {
|
|
|
|
setUserFederations(userFederations);
|
|
|
|
},
|
|
|
|
[key]
|
|
|
|
);
|
2020-11-10 20:26:20 +00:00
|
|
|
|
2022-01-05 17:06:53 +00:00
|
|
|
const ufAddProviderDropdownItems = useMemo(
|
|
|
|
() =>
|
|
|
|
providers.map((p) => (
|
|
|
|
<DropdownItem
|
|
|
|
key={p.id}
|
|
|
|
onClick={() =>
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toProvider({ realm, providerId: p.id!, id: "new" }))
|
2022-01-05 17:06:53 +00:00
|
|
|
}
|
|
|
|
>
|
2022-01-08 09:27:57 +00:00
|
|
|
{p.id.toUpperCase() == "LDAP"
|
|
|
|
? p.id.toUpperCase()
|
|
|
|
: toUpperCase(p.id)}
|
2022-01-05 17:06:53 +00:00
|
|
|
</DropdownItem>
|
|
|
|
)),
|
|
|
|
[]
|
|
|
|
);
|
2020-11-10 20:26:20 +00:00
|
|
|
|
2022-02-09 09:16:10 +00:00
|
|
|
const lowerButtonProps = {
|
|
|
|
variant: "link",
|
|
|
|
onClick: () => setManageDisplayDialog(true),
|
|
|
|
lowerButtonTitle: t("managePriorities"),
|
|
|
|
};
|
2020-10-15 05:50:47 +00:00
|
|
|
|
2020-11-10 20:26:20 +00:00
|
|
|
let cards;
|
|
|
|
|
|
|
|
const [currentCard, setCurrentCard] = useState("");
|
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: t("userFedDeleteConfirmTitle"),
|
|
|
|
messageKey: t("userFedDeleteConfirm"),
|
|
|
|
continueButtonLabel: "common:delete",
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
2020-11-12 12:55:52 +00:00
|
|
|
await adminClient.components.del({ id: currentCard });
|
2021-01-05 13:39:27 +00:00
|
|
|
refresh();
|
2020-11-10 20:26:20 +00:00
|
|
|
addAlert(t("userFedDeletedSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("user-federation:userFedDeleteError", error);
|
2020-11-10 20:26:20 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const toggleDeleteForCard = (id: string) => {
|
|
|
|
setCurrentCard(id);
|
|
|
|
toggleDeleteDialog();
|
|
|
|
};
|
|
|
|
|
2022-02-09 09:16:10 +00:00
|
|
|
const cardSorter = (card1: any, card2: any) => {
|
|
|
|
const a = `${card1.name}`;
|
|
|
|
const b = `${card2.name}`;
|
|
|
|
return a < b ? -1 : 1;
|
|
|
|
};
|
|
|
|
|
2020-11-10 20:26:20 +00:00
|
|
|
if (userFederations) {
|
2022-08-31 22:12:29 +00:00
|
|
|
cards = userFederations.sort(cardSorter).map((userFederation, index) => (
|
|
|
|
<GalleryItem
|
|
|
|
key={index}
|
|
|
|
className="keycloak-admin--user-federation__gallery-item"
|
|
|
|
>
|
|
|
|
<KeycloakCard
|
|
|
|
id={userFederation.id!}
|
|
|
|
dropdownItems={[
|
|
|
|
<DropdownItem
|
|
|
|
key={`${index}-cardDelete`}
|
|
|
|
onClick={() => {
|
|
|
|
toggleDeleteForCard(userFederation.id!);
|
|
|
|
}}
|
|
|
|
data-testid="card-delete"
|
|
|
|
>
|
|
|
|
{t("common:delete")}
|
|
|
|
</DropdownItem>,
|
|
|
|
]}
|
|
|
|
providerId={userFederation.providerId!}
|
|
|
|
title={userFederation.name!}
|
|
|
|
footerText={toUpperCase(userFederation.providerId!)}
|
|
|
|
labelText={
|
|
|
|
userFederation.config?.["enabled"]?.[0] !== "false"
|
|
|
|
? `${t("common:enabled")}`
|
|
|
|
: `${t("common:disabled")}`
|
|
|
|
}
|
|
|
|
labelColor={
|
|
|
|
userFederation.config?.["enabled"]?.[0] !== "false"
|
|
|
|
? "blue"
|
|
|
|
: "gray"
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
</GalleryItem>
|
|
|
|
));
|
2020-11-10 20:26:20 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 05:50:47 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-02-09 09:16:10 +00:00
|
|
|
<DeleteConfirm />
|
|
|
|
{manageDisplayDialog && userFederations && (
|
|
|
|
<ManagePriorityDialog
|
|
|
|
onClose={() => setManageDisplayDialog(false)}
|
|
|
|
components={userFederations.filter((p) => p.config?.enabled)}
|
|
|
|
/>
|
|
|
|
)}
|
2020-10-15 05:50:47 +00:00
|
|
|
<ViewHeader
|
2020-11-10 20:26:20 +00:00
|
|
|
titleKey="userFederation"
|
2021-12-21 15:32:53 +00:00
|
|
|
subKey="user-federation:userFederationExplain"
|
|
|
|
helpUrl={helpUrls.userFederationUrl}
|
2020-11-10 20:26:20 +00:00
|
|
|
{...(userFederations && userFederations.length > 0
|
|
|
|
? {
|
2020-11-16 19:09:39 +00:00
|
|
|
lowerDropdownItems: ufAddProviderDropdownItems,
|
2020-11-10 20:26:20 +00:00
|
|
|
lowerDropdownMenuTitle: "user-federation:addNewProvider",
|
2022-02-09 09:16:10 +00:00
|
|
|
lowerButton: lowerButtonProps,
|
2020-11-10 20:26:20 +00:00
|
|
|
}
|
|
|
|
: {})}
|
2020-10-15 05:50:47 +00:00
|
|
|
/>
|
|
|
|
<PageSection>
|
2020-11-10 20:26:20 +00:00
|
|
|
{userFederations && userFederations.length > 0 ? (
|
2022-02-09 09:16:10 +00:00
|
|
|
<Gallery hasGutter>{cards}</Gallery>
|
2020-11-10 20:26:20 +00:00
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<TextContent>
|
|
|
|
<Text component={TextVariants.p}>{t("getStarted")}</Text>
|
|
|
|
</TextContent>
|
|
|
|
<TextContent>
|
|
|
|
<Text className="pf-u-mt-lg" component={TextVariants.h2}>
|
|
|
|
{t("providers")}
|
|
|
|
</Text>
|
|
|
|
</TextContent>
|
|
|
|
<hr className="pf-u-mb-lg" />
|
|
|
|
<Gallery hasGutter>
|
2022-01-05 17:06:53 +00:00
|
|
|
{providers.map((p) => (
|
|
|
|
<Card
|
|
|
|
key={p.id}
|
|
|
|
className="keycloak-empty-state-card"
|
2022-07-27 15:17:44 +00:00
|
|
|
role="button"
|
2022-01-05 17:06:53 +00:00
|
|
|
isHoverable
|
|
|
|
onClick={() =>
|
2022-08-16 13:09:14 +00:00
|
|
|
navigate(toProvider({ realm, providerId: p.id! }))
|
2022-01-05 17:06:53 +00:00
|
|
|
}
|
|
|
|
data-testid={`${p.id}-card`}
|
|
|
|
>
|
|
|
|
<CardTitle>
|
|
|
|
<Split hasGutter>
|
|
|
|
<SplitItem>
|
|
|
|
<DatabaseIcon size="lg" />
|
|
|
|
</SplitItem>
|
|
|
|
<SplitItem isFilled>
|
|
|
|
{t("addProvider", {
|
|
|
|
provider: toUpperCase(p.id!),
|
|
|
|
count: 4,
|
|
|
|
})}
|
|
|
|
</SplitItem>
|
|
|
|
</Split>
|
|
|
|
</CardTitle>
|
|
|
|
</Card>
|
|
|
|
))}
|
2020-11-10 20:26:20 +00:00
|
|
|
</Gallery>
|
|
|
|
</>
|
|
|
|
)}
|
2020-10-15 05:50:47 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
|
|
|
);
|
2021-10-29 16:11:06 +00:00
|
|
|
}
|