keycloak-scim/src/sessions/SessionsSection.tsx

217 lines
6.6 KiB
TypeScript
Raw Normal View History

2021-07-05 15:58:19 +00:00
import React, { useState } from "react";
import { Link } from "react-router-dom";
2021-07-05 15:58:19 +00:00
import { useTranslation } from "react-i18next";
import moment from "moment";
2021-07-05 16:13:17 +00:00
import {
DropdownItem,
2021-07-05 16:13:17 +00:00
PageSection,
Select,
SelectOption,
SelectVariant,
} from "@patternfly/react-core";
import { FilterIcon } from "@patternfly/react-icons";
import type UserSessionRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userSessionRepresentation";
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
import { ViewHeader } from "../components/view-header/ViewHeader";
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
import { useAdminClient } from "../context/auth/AdminClient";
import { CubesIcon } from "@patternfly/react-icons";
2021-07-06 08:17:04 +00:00
import "./SessionsSection.css";
import { RevocationModal } from "./RevocationModal";
import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation";
import { LogoutAllSessionsModal } from "./LogoutAllSessionsModal";
const Clients = (row: UserSessionRepresentation) => {
return (
<>
{Object.values(row.clients!).map((client) => (
<Link key={client} to="" className="pf-u-mx-sm">
{client}
</Link>
))}
</>
);
};
export const SessionsSection = () => {
2021-07-05 15:58:19 +00:00
const { t } = useTranslation("sessions");
const adminClient = useAdminClient();
2021-07-05 15:58:19 +00:00
const [filterDropdownOpen, setFilterDropdownOpen] = useState(false);
const [revocationModalOpen, setRevocationModalOpen] = useState(false);
const [logoutAllSessionsModalOpen, setLogoutAllSessionsModalOpen] =
useState(false);
const [activeClientDetails, setActiveClientDetails] = useState<
ClientRepresentation[]
>([]);
2021-07-06 08:17:04 +00:00
const [filterType, setFilterType] = useState(
2021-07-06 08:57:29 +00:00
t("sessionsType.allSessions").toString()
2021-07-06 08:17:04 +00:00
);
2021-07-05 15:58:19 +00:00
const [key, setKey] = useState(0);
const [noSessions, setNoSessions] = useState(false);
2021-07-05 15:58:19 +00:00
const refresh = () => {
setKey(new Date().getTime());
};
const handleRevocationModalToggle = () => {
setRevocationModalOpen(!revocationModalOpen);
};
const handleLogoutAllSessionsModalToggle = () => {
setLogoutAllSessionsModalOpen(!logoutAllSessionsModalOpen);
};
const loader = async () => {
const activeClients = await adminClient.sessions.find();
const clientSessions = (
await Promise.all(
activeClients.map((client) =>
adminClient.clients.listSessions({ id: client.id })
)
)
).flat();
setNoSessions(clientSessions.length === 0);
const allClients = await adminClient.clients.find();
const getActiveClientDetails = allClients.filter((x) =>
activeClients.map((y) => y.id).includes(x.id)
);
setActiveClientDetails(getActiveClientDetails);
const userIds = Array.from(
new Set(clientSessions.map((session) => session.userId))
);
const userSessions = (
await Promise.all(
userIds.map((userId) => adminClient.users.listSessions({ id: userId! }))
)
).flat();
return userSessions;
};
2021-07-05 16:13:17 +00:00
const dropdownItems = [
<DropdownItem
key="toggle-modal"
data-testid="revocation"
component="button"
onClick={() => handleRevocationModalToggle()}
>
{t("revocation")}
</DropdownItem>,
<DropdownItem
key="delete-role"
data-testid="logout-all"
component="button"
isDisabled={noSessions}
onClick={() => handleLogoutAllSessionsModalToggle()}
>
{t("signOutAllActiveSessions")}
</DropdownItem>,
];
return (
<>
<ViewHeader
dropdownItems={dropdownItems}
titleKey="sessions:title"
subKey="sessions:sessionExplain"
/>
<PageSection variant="light" className="pf-u-p-0">
{revocationModalOpen && (
<RevocationModal
handleModalToggle={handleRevocationModalToggle}
activeClients={activeClientDetails}
save={() => {
handleRevocationModalToggle();
}}
/>
)}
{logoutAllSessionsModalOpen && (
<LogoutAllSessionsModal
handleModalToggle={handleLogoutAllSessionsModalToggle}
/>
)}
<KeycloakDataTable
2021-07-06 08:17:04 +00:00
key={key}
loader={loader}
ariaLabelKey="session:title"
searchPlaceholderKey="sessions:searchForSession"
2021-07-05 15:58:19 +00:00
searchTypeComponent={
<Select
data-testid="filter-session-type-select"
isOpen={filterDropdownOpen}
2021-07-20 18:53:36 +00:00
className="kc-filter-session-type-select"
2021-07-05 15:58:19 +00:00
variant={SelectVariant.single}
2021-07-06 08:17:04 +00:00
onToggle={(isExpanded) => setFilterDropdownOpen(isExpanded)}
2021-07-05 15:58:19 +00:00
toggleIcon={<FilterIcon />}
onSelect={(_, value) => {
2021-07-06 08:17:04 +00:00
setFilterType(value.toString());
2021-07-05 15:58:19 +00:00
refresh();
setFilterDropdownOpen(false);
}}
selections={filterType}
>
2021-07-06 08:17:04 +00:00
<SelectOption
data-testid="all-sessions-option"
2021-07-06 08:57:29 +00:00
value={t("sessionsType.allSessions")}
2021-07-06 08:17:04 +00:00
isPlaceholder
/>
<SelectOption
data-testid="regular-sso-option"
2021-07-06 08:57:29 +00:00
value={t("sessionsType.regularSSO")}
2021-07-06 08:17:04 +00:00
/>
<SelectOption
data-testid="offline-option"
2021-07-06 08:57:29 +00:00
value={t("sessionsType.offline")}
2021-07-06 08:17:04 +00:00
/>
<SelectOption
data-testid="direct-grant-option"
2021-07-06 08:57:29 +00:00
value={t("sessionsType.directGrant")}
2021-07-06 08:17:04 +00:00
/>
<SelectOption
data-testid="service-account-option"
2021-07-06 08:57:29 +00:00
value={t("sessionsType.serviceAccount")}
2021-07-06 08:17:04 +00:00
/>
2021-07-05 15:58:19 +00:00
</Select>
}
columns={[
{
name: "username",
displayKey: "sessions:subject",
},
{
name: "lastAccess",
displayKey: "sessions:lastAccess",
cellRenderer: (row) => moment(row.lastAccess).fromNow(),
},
{
name: "start",
displayKey: "sessions:startDate",
cellRenderer: (row) => moment(row.lastAccess).format("LLL"),
},
{
name: "clients",
displayKey: "sessions:accessedClients",
cellRenderer: Clients,
},
]}
emptyState={
<ListEmptyState
hasIcon
icon={CubesIcon}
message={t("noSessions")}
instructions={t("noSessionsDescription")}
/>
}
/>
</PageSection>
</>
);
};