keycloak-scim/js/apps/admin-ui/src/user/UserConsents.tsx

142 lines
4.3 KiB
TypeScript
Raw Normal View History

import type UserConsentRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userConsentRepresentation";
2021-06-09 18:01:14 +00:00
import {
AlertVariant,
ButtonVariant,
Chip,
ChipGroup,
} from "@patternfly/react-core";
import { CubesIcon } from "@patternfly/react-icons";
import { cellWidth } from "@patternfly/react-table";
import { sortBy } from "lodash-es";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { adminClient } from "../admin-client";
2021-06-09 18:01:14 +00:00
import { useAlerts } from "../components/alert/Alerts";
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
import {
Action,
KeycloakDataTable,
} from "../components/table-toolbar/KeycloakDataTable";
import { emptyFormatter } from "../util";
import useFormatDate from "../utils/useFormatDate";
import { useParams } from "../utils/useParams";
export const UserConsents = () => {
const [selectedClient, setSelectedClient] =
useState<UserConsentRepresentation>();
const { t } = useTranslation("roles");
const { addAlert, addError } = useAlerts();
const formatDate = useFormatDate();
2021-06-09 18:01:14 +00:00
const [key, setKey] = useState(0);
const { id } = useParams<{ id: string }>();
const alphabetize = (consentsList: UserConsentRepresentation[]) => {
return sortBy(consentsList, (client) => client.clientId?.toUpperCase());
};
2021-06-09 18:01:14 +00:00
const refresh = () => setKey(new Date().getTime());
const loader = async () => {
2021-06-09 13:35:35 +00:00
const getConsents = await adminClient.users.listConsents({ id });
2021-06-09 13:35:35 +00:00
return alphabetize(getConsents);
};
const clientScopesRenderer = ({
grantedClientScopes,
}: UserConsentRepresentation) => {
2021-06-09 13:35:35 +00:00
return (
<ChipGroup className="kc-consents-chip-group">
{grantedClientScopes!.map((currentChip) => (
<Chip key={currentChip} isReadOnly className="kc-consents-chip">
2021-06-09 13:35:35 +00:00
{currentChip}
</Chip>
))}
</ChipGroup>
);
};
2021-06-09 18:01:14 +00:00
const [toggleDeleteDialog, DeleteConfirm] = useConfirmDialog({
titleKey: "users:revokeClientScopesTitle",
2021-06-10 15:08:11 +00:00
messageKey: t("users:revokeClientScopes", {
clientId: selectedClient?.clientId,
}),
continueButtonLabel: "common:revoke",
2021-06-09 18:01:14 +00:00
continueButtonVariant: ButtonVariant.danger,
onConfirm: async () => {
try {
await adminClient.users.revokeConsent({
id,
clientId: selectedClient!.clientId!,
});
refresh();
addAlert(t("deleteGrantsSuccess"), AlertVariant.success);
} catch (error) {
addError("roles:deleteGrantsError", error);
2021-06-09 18:01:14 +00:00
}
},
});
return (
<>
2021-06-09 18:01:14 +00:00
<DeleteConfirm />
2021-06-08 13:23:32 +00:00
<KeycloakDataTable
loader={loader}
2021-06-09 18:01:14 +00:00
key={key}
2021-06-08 13:23:32 +00:00
ariaLabelKey="roles:roleList"
searchPlaceholderKey=" "
columns={[
{
name: "clientId",
displayKey: "clients:Client",
cellFormatters: [emptyFormatter()],
transforms: [cellWidth(20)],
},
{
name: "grantedClientScopes",
displayKey: "client-scopes:grantedClientScopes",
cellFormatters: [emptyFormatter()],
cellRenderer: clientScopesRenderer,
transforms: [cellWidth(30)],
},
{
name: "createDate",
2021-06-08 13:23:32 +00:00
displayKey: "clients:created",
transforms: [cellWidth(20)],
cellRenderer: ({ createDate }) =>
createDate ? formatDate(new Date(createDate)) : "—",
2021-06-08 13:23:32 +00:00
},
{
name: "lastUpdatedDate",
displayKey: "clients:lastUpdated",
2021-06-09 18:01:14 +00:00
transforms: [cellWidth(10)],
cellRenderer: ({ lastUpdatedDate }) =>
lastUpdatedDate ? formatDate(new Date(lastUpdatedDate)) : "—",
2021-06-09 18:01:14 +00:00
},
]}
actions={[
{
title: t("users:revoke"),
onRowClick: (client) => {
setSelectedClient(client);
toggleDeleteDialog();
},
} as Action<UserConsentRepresentation>,
2021-06-08 13:23:32 +00:00
]}
emptyState={
<ListEmptyState
hasIcon={true}
icon={CubesIcon}
message={t("users:noConsents")}
instructions={t("users:noConsentsText")}
/>
}
/>
</>
);
};