2020-12-07 19:23:18 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
Badge,
|
|
|
|
Button,
|
2021-02-24 07:59:12 +00:00
|
|
|
ButtonVariant,
|
2020-12-07 19:23:18 +00:00
|
|
|
PageSection,
|
2021-03-03 07:02:40 +00:00
|
|
|
Tab,
|
|
|
|
TabTitleText,
|
2021-07-21 15:08:40 +00:00
|
|
|
ToolbarItem,
|
2020-12-07 19:23:18 +00:00
|
|
|
} from "@patternfly/react-core";
|
2021-07-21 15:08:40 +00:00
|
|
|
import { cellWidth, TableText } from "@patternfly/react-table";
|
2021-06-24 07:35:13 +00:00
|
|
|
import type ClientRepresentation from "keycloak-admin/lib/defs/clientRepresentation";
|
2021-07-21 15:08:40 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { Link } from "react-router-dom";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
2021-06-24 07:35:13 +00:00
|
|
|
import { formattedLinkTableCell } from "../components/external-link/FormattedLink";
|
2021-07-21 15:08:40 +00:00
|
|
|
import { KeycloakTabs } from "../components/keycloak-tabs/KeycloakTabs";
|
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
2020-09-25 17:29:03 +00:00
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
2020-11-12 12:55:52 +00:00
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
2021-06-24 07:35:13 +00:00
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
2021-07-21 15:08:40 +00:00
|
|
|
import { emptyFormatter, exportClient, getBaseUrl } from "../util";
|
2021-03-04 08:20:37 +00:00
|
|
|
import { InitialAccessTokenList } from "./initial-access/InitialAccessTokenList";
|
2021-07-21 15:08:40 +00:00
|
|
|
import { toAddClient } from "./routes/AddClient";
|
|
|
|
import { toClient } from "./routes/Client";
|
|
|
|
import { toImportClient } from "./routes/ImportClient";
|
2020-09-03 19:25:05 +00:00
|
|
|
|
2020-09-10 18:04:03 +00:00
|
|
|
export const ClientsSection = () => {
|
|
|
|
const { t } = useTranslation("clients");
|
2021-07-28 12:01:42 +00:00
|
|
|
const { addAlert, addError } = useAlerts();
|
2020-09-17 11:37:30 +00:00
|
|
|
|
2020-11-12 12:55:52 +00:00
|
|
|
const adminClient = useAdminClient();
|
2021-03-05 10:18:47 +00:00
|
|
|
const { realm } = useRealm();
|
2021-01-13 19:59:45 +00:00
|
|
|
const baseUrl = getBaseUrl(adminClient);
|
2020-09-03 19:25:05 +00:00
|
|
|
|
2021-02-24 07:59:12 +00:00
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
const refresh = () => setKey(new Date().getTime());
|
|
|
|
const [selectedClient, setSelectedClient] = useState<ClientRepresentation>();
|
|
|
|
|
2020-12-07 19:23:18 +00:00
|
|
|
const loader = async (first?: number, max?: number, search?: string) => {
|
|
|
|
const params: { [name: string]: string | number } = {
|
|
|
|
first: first!,
|
|
|
|
max: max!,
|
|
|
|
};
|
2020-10-05 18:13:23 +00:00
|
|
|
if (search) {
|
|
|
|
params.clientId = search;
|
|
|
|
params.search = "true";
|
|
|
|
}
|
2020-12-07 19:23:18 +00:00
|
|
|
return await adminClient.clients.find({ ...params });
|
|
|
|
};
|
|
|
|
|
2021-02-24 07:59:12 +00:00
|
|
|
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
|
|
|
|
titleKey: t("clientDelete", { clientId: selectedClient?.clientId }),
|
|
|
|
messageKey: "clients:clientDeleteConfirm",
|
|
|
|
continueButtonLabel: "common:delete",
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await adminClient.clients.del({
|
|
|
|
id: selectedClient!.id!,
|
|
|
|
});
|
|
|
|
addAlert(t("clientDeletedSuccess"), AlertVariant.success);
|
2021-04-29 06:28:59 +00:00
|
|
|
refresh();
|
2021-02-24 07:59:12 +00:00
|
|
|
} catch (error) {
|
2021-07-28 12:01:42 +00:00
|
|
|
addError("client:clientDeleteError", error);
|
2021-02-24 07:59:12 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2020-12-07 19:23:18 +00:00
|
|
|
const ClientDetailLink = (client: ClientRepresentation) => (
|
|
|
|
<>
|
2021-07-21 15:08:40 +00:00
|
|
|
<Link
|
|
|
|
key={client.id}
|
|
|
|
to={toClient({ realm, clientId: client.id!, tab: "settings" })}
|
|
|
|
>
|
2020-12-07 19:23:18 +00:00
|
|
|
{client.clientId}
|
2021-02-16 19:18:09 +00:00
|
|
|
{!client.enabled && (
|
2021-03-09 13:59:41 +00:00
|
|
|
<Badge key={`${client.id}-disabled`} isRead className="pf-u-ml-sm">
|
2021-04-21 13:18:45 +00:00
|
|
|
{t("common:disabled")}
|
2021-02-16 19:18:09 +00:00
|
|
|
</Badge>
|
|
|
|
)}
|
2020-12-07 19:23:18 +00:00
|
|
|
</Link>
|
|
|
|
</>
|
|
|
|
);
|
2021-02-25 09:10:28 +00:00
|
|
|
|
2021-06-24 07:35:13 +00:00
|
|
|
const ClientDescription = (client: ClientRepresentation) => (
|
|
|
|
<>
|
|
|
|
<TableText wrapModifier="truncate">
|
|
|
|
{emptyFormatter()(client.description)}
|
|
|
|
</TableText>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
|
2020-09-03 19:25:05 +00:00
|
|
|
return (
|
2020-09-25 17:29:03 +00:00
|
|
|
<>
|
|
|
|
<ViewHeader
|
|
|
|
titleKey="clients:clientList"
|
|
|
|
subKey="clients:clientsExplain"
|
2021-03-03 07:02:40 +00:00
|
|
|
divider={false}
|
2020-09-25 17:29:03 +00:00
|
|
|
/>
|
2021-02-16 19:18:09 +00:00
|
|
|
<PageSection variant="light" className="pf-u-p-0">
|
2021-03-31 13:16:58 +00:00
|
|
|
<KeycloakTabs isBox>
|
2021-03-03 07:02:40 +00:00
|
|
|
<Tab
|
|
|
|
data-testid="list"
|
|
|
|
eventKey="list"
|
|
|
|
title={<TabTitleText>{t("clientsList")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<DeleteConfirm />
|
|
|
|
<KeycloakDataTable
|
|
|
|
key={key}
|
|
|
|
loader={loader}
|
|
|
|
isPaginated
|
|
|
|
ariaLabelKey="clients:clientList"
|
|
|
|
searchPlaceholderKey="clients:searchForClient"
|
|
|
|
toolbarItem={
|
|
|
|
<>
|
2021-03-04 08:48:34 +00:00
|
|
|
<ToolbarItem>
|
2021-07-21 15:08:40 +00:00
|
|
|
{/* @ts-ignore */}
|
|
|
|
<Button component={Link} to={toAddClient({ realm })}>
|
2021-03-04 08:48:34 +00:00
|
|
|
{t("createClient")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
|
|
|
<ToolbarItem>
|
|
|
|
<Button
|
2021-07-21 15:08:40 +00:00
|
|
|
component={Link}
|
|
|
|
// @ts-ignore
|
|
|
|
to={toImportClient({ realm })}
|
2021-03-04 08:48:34 +00:00
|
|
|
variant="link"
|
|
|
|
>
|
|
|
|
{t("importClient")}
|
|
|
|
</Button>
|
|
|
|
</ToolbarItem>
|
2021-03-03 07:02:40 +00:00
|
|
|
</>
|
|
|
|
}
|
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
title: t("common:export"),
|
|
|
|
onRowClick: (client) => {
|
|
|
|
exportClient(client);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: t("common:delete"),
|
|
|
|
onRowClick: (client) => {
|
|
|
|
setSelectedClient(client);
|
|
|
|
toggleDeleteDialog();
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
columns={[
|
|
|
|
{
|
|
|
|
name: "clientId",
|
2021-04-06 17:39:27 +00:00
|
|
|
displayKey: "common:clientId",
|
2021-03-03 07:02:40 +00:00
|
|
|
cellRenderer: ClientDetailLink,
|
|
|
|
},
|
|
|
|
{ name: "protocol", displayKey: "common:type" },
|
|
|
|
{
|
|
|
|
name: "description",
|
|
|
|
displayKey: "common:description",
|
2021-06-24 07:35:13 +00:00
|
|
|
transforms: [cellWidth(20)],
|
|
|
|
cellRenderer: ClientDescription,
|
2021-03-03 07:02:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "baseUrl",
|
|
|
|
displayKey: "clients:homeURL",
|
|
|
|
cellFormatters: [formattedLinkTableCell(), emptyFormatter()],
|
|
|
|
cellRenderer: (client) => {
|
|
|
|
if (client.rootUrl) {
|
|
|
|
if (
|
|
|
|
!client.rootUrl.startsWith("http") ||
|
2021-08-18 09:29:42 +00:00
|
|
|
client.rootUrl.includes("$")
|
2021-03-03 07:02:40 +00:00
|
|
|
) {
|
|
|
|
client.rootUrl =
|
|
|
|
client.rootUrl
|
|
|
|
.replace("${authBaseUrl}", baseUrl)
|
|
|
|
.replace("${authAdminUrl}", baseUrl) +
|
|
|
|
(client.baseUrl ? client.baseUrl.substr(1) : "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return client.rootUrl;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</Tab>
|
|
|
|
<Tab
|
|
|
|
data-testid="initialAccessToken"
|
|
|
|
eventKey="initialAccessToken"
|
|
|
|
title={<TabTitleText>{t("initialAccessToken")}</TabTitleText>}
|
|
|
|
>
|
|
|
|
<InitialAccessTokenList />
|
|
|
|
</Tab>
|
|
|
|
</KeycloakTabs>
|
2020-09-25 17:29:03 +00:00
|
|
|
</PageSection>
|
|
|
|
</>
|
2020-09-03 19:25:05 +00:00
|
|
|
);
|
|
|
|
};
|