2021-07-05 15:58:19 +00:00
|
|
|
import React, { useState } from "react";
|
2020-12-18 07:01:35 +00:00
|
|
|
import { Link } from "react-router-dom";
|
2021-07-05 15:58:19 +00:00
|
|
|
import { useTranslation } from "react-i18next";
|
2021-08-11 09:33:51 +00:00
|
|
|
import moment from "moment";
|
2021-07-05 16:13:17 +00:00
|
|
|
import {
|
2021-08-16 19:25:36 +00:00
|
|
|
DropdownItem,
|
2021-07-05 16:13:17 +00:00
|
|
|
PageSection,
|
|
|
|
Select,
|
|
|
|
SelectOption,
|
|
|
|
SelectVariant,
|
|
|
|
} from "@patternfly/react-core";
|
2021-08-11 09:33:51 +00:00
|
|
|
import { FilterIcon } from "@patternfly/react-icons";
|
2020-12-18 07:01:35 +00:00
|
|
|
|
2021-08-11 09:33:51 +00:00
|
|
|
import type UserSessionRepresentation from "keycloak-admin/lib/defs/userSessionRepresentation";
|
|
|
|
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
|
2020-12-18 07:01:35 +00:00
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
|
|
|
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
|
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
2021-08-11 09:33:51 +00:00
|
|
|
|
2021-08-16 19:25:36 +00:00
|
|
|
import { CubesIcon } from "@patternfly/react-icons";
|
2021-07-06 08:17:04 +00:00
|
|
|
import "./SessionsSection.css";
|
2021-08-16 19:25:36 +00:00
|
|
|
import { RevocationModal } from "./RevocationModal";
|
|
|
|
import type ClientRepresentation from "keycloak-admin/lib/defs/clientRepresentation";
|
|
|
|
import { LogoutAllSessionsModal } from "./LogoutAllSessionsModal";
|
2020-12-18 07:01:35 +00:00
|
|
|
|
|
|
|
const Clients = (row: UserSessionRepresentation) => {
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{Object.values(row.clients!).map((client) => (
|
|
|
|
<Link key={client} to="" className="pf-u-mx-sm">
|
|
|
|
{client}
|
|
|
|
</Link>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2020-09-09 09:07:17 +00:00
|
|
|
|
2020-09-10 18:04:03 +00:00
|
|
|
export const SessionsSection = () => {
|
2021-07-05 15:58:19 +00:00
|
|
|
const { t } = useTranslation("sessions");
|
2020-12-18 07:01:35 +00:00
|
|
|
const adminClient = useAdminClient();
|
2021-07-05 15:58:19 +00:00
|
|
|
const [filterDropdownOpen, setFilterDropdownOpen] = useState(false);
|
2021-08-16 19:25:36 +00:00
|
|
|
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);
|
2021-08-16 19:25:36 +00:00
|
|
|
const [noSessions, setNoSessions] = useState(false);
|
2021-07-05 15:58:19 +00:00
|
|
|
|
|
|
|
const refresh = () => {
|
|
|
|
setKey(new Date().getTime());
|
|
|
|
};
|
2020-12-18 07:01:35 +00:00
|
|
|
|
2021-08-16 19:25:36 +00:00
|
|
|
const handleRevocationModalToggle = () => {
|
|
|
|
setRevocationModalOpen(!revocationModalOpen);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleLogoutAllSessionsModalToggle = () => {
|
|
|
|
setLogoutAllSessionsModalOpen(!logoutAllSessionsModalOpen);
|
|
|
|
};
|
|
|
|
|
2020-12-18 07:01:35 +00:00
|
|
|
const loader = async () => {
|
|
|
|
const activeClients = await adminClient.sessions.find();
|
|
|
|
const clientSessions = (
|
|
|
|
await Promise.all(
|
|
|
|
activeClients.map((client) =>
|
|
|
|
adminClient.clients.listSessions({ id: client.id })
|
|
|
|
)
|
|
|
|
)
|
|
|
|
).flat();
|
|
|
|
|
2021-08-16 19:25:36 +00:00
|
|
|
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);
|
|
|
|
|
2021-05-17 13:35:12 +00:00
|
|
|
const userIds = Array.from(
|
|
|
|
new Set(clientSessions.map((session) => session.userId))
|
|
|
|
);
|
2020-12-18 07:01:35 +00:00
|
|
|
const userSessions = (
|
|
|
|
await Promise.all(
|
2021-05-17 13:35:12 +00:00
|
|
|
userIds.map((userId) => adminClient.users.listSessions({ id: userId! }))
|
2020-12-18 07:01:35 +00:00
|
|
|
)
|
|
|
|
).flat();
|
|
|
|
|
|
|
|
return userSessions;
|
|
|
|
};
|
2021-07-05 16:13:17 +00:00
|
|
|
|
2021-08-16 19:25:36 +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>,
|
|
|
|
];
|
|
|
|
|
2020-09-18 08:04:55 +00:00
|
|
|
return (
|
|
|
|
<>
|
2021-08-16 19:25:36 +00:00
|
|
|
<ViewHeader
|
|
|
|
dropdownItems={dropdownItems}
|
|
|
|
titleKey="sessions:title"
|
|
|
|
subKey="sessions:sessionExplain"
|
|
|
|
/>
|
2021-03-31 13:16:58 +00:00
|
|
|
<PageSection variant="light" className="pf-u-p-0">
|
2021-08-16 19:25:36 +00:00
|
|
|
{revocationModalOpen && (
|
|
|
|
<RevocationModal
|
|
|
|
handleModalToggle={handleRevocationModalToggle}
|
|
|
|
activeClients={activeClientDetails}
|
|
|
|
save={() => {
|
|
|
|
handleRevocationModalToggle();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
{logoutAllSessionsModalOpen && (
|
|
|
|
<LogoutAllSessionsModal
|
|
|
|
handleModalToggle={handleLogoutAllSessionsModalToggle}
|
|
|
|
/>
|
|
|
|
)}
|
2020-12-18 07:01:35 +00:00
|
|
|
<KeycloakDataTable
|
2021-07-06 08:17:04 +00:00
|
|
|
key={key}
|
2020-12-18 07:01:35 +00:00
|
|
|
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>
|
|
|
|
}
|
2020-12-18 07:01:35 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
]}
|
2021-08-11 09:33:51 +00:00
|
|
|
emptyState={
|
|
|
|
<ListEmptyState
|
2021-08-16 19:25:36 +00:00
|
|
|
hasIcon
|
|
|
|
icon={CubesIcon}
|
|
|
|
message={t("noSessions")}
|
|
|
|
instructions={t("noSessionsDescription")}
|
2021-08-11 09:33:51 +00:00
|
|
|
/>
|
|
|
|
}
|
2020-12-18 07:01:35 +00:00
|
|
|
/>
|
|
|
|
</PageSection>
|
2020-09-18 08:04:55 +00:00
|
|
|
</>
|
|
|
|
);
|
2020-09-09 09:07:17 +00:00
|
|
|
};
|