2021-06-09 18:01:14 +00:00
|
|
|
import React, { useState } from "react";
|
2021-04-14 18:39:21 +00:00
|
|
|
import { useParams } from "react-router-dom";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2021-06-09 18:01:14 +00:00
|
|
|
import {
|
|
|
|
AlertVariant,
|
|
|
|
ButtonVariant,
|
|
|
|
Chip,
|
|
|
|
ChipGroup,
|
|
|
|
} from "@patternfly/react-core";
|
2021-04-14 18:39:21 +00:00
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
|
|
|
import { emptyFormatter } from "../util";
|
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
|
|
|
import { cellWidth } from "@patternfly/react-table";
|
|
|
|
import _ from "lodash";
|
2021-05-04 17:58:18 +00:00
|
|
|
import type UserConsentRepresentation from "keycloak-admin/lib/defs/userConsentRepresentation";
|
2021-04-14 18:39:21 +00:00
|
|
|
import { CubesIcon } from "@patternfly/react-icons";
|
|
|
|
import moment from "moment";
|
2021-06-09 18:01:14 +00:00
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
2021-04-14 18:39:21 +00:00
|
|
|
|
|
|
|
export const UserConsents = () => {
|
2021-07-05 09:22:29 +00:00
|
|
|
const [
|
|
|
|
selectedClient,
|
|
|
|
setSelectedClient,
|
|
|
|
] = useState<UserConsentRepresentation>();
|
2021-04-14 18:39:21 +00:00
|
|
|
const { t } = useTranslation("roles");
|
2021-06-09 18:01:14 +00:00
|
|
|
const { addAlert } = useAlerts();
|
|
|
|
const [key, setKey] = useState(0);
|
2021-04-14 18:39:21 +00:00
|
|
|
|
|
|
|
const adminClient = useAdminClient();
|
|
|
|
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());
|
|
|
|
|
2021-04-14 18:39:21 +00:00
|
|
|
const loader = async () => {
|
2021-06-09 13:35:35 +00:00
|
|
|
const getConsents = await adminClient.users.listConsents({ id });
|
2021-04-14 18:39:21 +00:00
|
|
|
|
2021-06-09 13:35:35 +00:00
|
|
|
return alphabetize(getConsents);
|
2021-04-14 18:39:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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"
|
|
|
|
id="consents-chip-text"
|
2021-06-08 13:23:32 +00:00
|
|
|
>
|
2021-06-09 13:35:35 +00:00
|
|
|
{currentChip}
|
|
|
|
</Chip>
|
|
|
|
))}
|
|
|
|
</ChipGroup>
|
|
|
|
);
|
2021-04-14 18:39:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const createdRenderer = ({ createDate }: UserConsentRepresentation) => {
|
|
|
|
return <>{moment(createDate).format("MM/DD/YY hh:MM A")}</>;
|
|
|
|
};
|
|
|
|
|
|
|
|
const lastUpdatedRenderer = ({
|
|
|
|
lastUpdatedDate,
|
|
|
|
}: UserConsentRepresentation) => {
|
|
|
|
return <>{moment(lastUpdatedDate).format("MM/DD/YY hh:MM A")}</>;
|
|
|
|
};
|
|
|
|
|
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,
|
|
|
|
}),
|
2021-06-09 18:01:14 +00:00
|
|
|
continueButtonLabel: "common:delete",
|
|
|
|
continueButtonVariant: ButtonVariant.danger,
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await adminClient.users.revokeConsent({
|
|
|
|
id,
|
|
|
|
clientId: selectedClient!.clientId!,
|
|
|
|
});
|
|
|
|
|
|
|
|
refresh();
|
|
|
|
|
|
|
|
addAlert(t("deleteGrantsSuccess"), AlertVariant.success);
|
|
|
|
} catch (error) {
|
|
|
|
addAlert(t("deleteGrantsError", { error }), AlertVariant.danger);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-04-14 18:39:21 +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: "createdDate",
|
|
|
|
displayKey: "clients:created",
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
cellRenderer: createdRenderer,
|
|
|
|
transforms: [cellWidth(20)],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "lastUpdatedDate",
|
|
|
|
displayKey: "clients:lastUpdated",
|
|
|
|
cellFormatters: [emptyFormatter()],
|
|
|
|
cellRenderer: lastUpdatedRenderer,
|
2021-06-09 18:01:14 +00:00
|
|
|
transforms: [cellWidth(10)],
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
actions={[
|
|
|
|
{
|
|
|
|
title: t("users:revoke"),
|
|
|
|
onRowClick: (client) => {
|
|
|
|
setSelectedClient(client);
|
|
|
|
toggleDeleteDialog();
|
|
|
|
},
|
2021-06-08 13:23:32 +00:00
|
|
|
},
|
|
|
|
]}
|
|
|
|
emptyState={
|
|
|
|
<ListEmptyState
|
|
|
|
hasIcon={true}
|
|
|
|
icon={CubesIcon}
|
|
|
|
message={t("users:noConsents")}
|
|
|
|
instructions={t("users:noConsentsText")}
|
|
|
|
onPrimaryAction={() => {}}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
/>
|
2021-04-14 18:39:21 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|