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";
|
|
|
|
import { PageSection, Select, SelectOption, SelectVariant } from "@patternfly/react-core";
|
2020-12-18 07:01:35 +00:00
|
|
|
import moment from "moment";
|
2021-05-04 17:58:18 +00:00
|
|
|
import type UserSessionRepresentation from "keycloak-admin/lib/defs/userSessionRepresentation";
|
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-07-05 15:58:19 +00:00
|
|
|
import { FilterIcon } from "@patternfly/react-icons";
|
|
|
|
import "./SessionSection.css";
|
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);
|
|
|
|
const [filterType, setFilterType] = useState("All session types");
|
|
|
|
const [key, setKey] = useState(0);
|
|
|
|
|
|
|
|
const refresh = () => {
|
|
|
|
setKey(new Date().getTime());
|
|
|
|
};
|
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-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 15:58:19 +00:00
|
|
|
|
|
|
|
const options = [
|
|
|
|
<SelectOption
|
|
|
|
key={1}
|
|
|
|
data-testid="all-sessions-option"
|
|
|
|
value={t("sessions:All session types")}
|
|
|
|
isPlaceholder
|
|
|
|
/>,
|
|
|
|
<SelectOption
|
|
|
|
data-testid="regular-sso-option"
|
|
|
|
key={2}
|
|
|
|
value={t("sessions:Regular SSO")}
|
|
|
|
/>,
|
|
|
|
<SelectOption
|
|
|
|
data-testid="offline-option"
|
|
|
|
key={3}
|
|
|
|
value={t("sessions:Offline")}
|
|
|
|
/>,
|
|
|
|
<SelectOption
|
|
|
|
data-testid="direct-grant-option"
|
|
|
|
key={4}
|
|
|
|
value={t("sessions:Direct grant")}
|
|
|
|
/>,
|
|
|
|
<SelectOption
|
|
|
|
data-testid="service-account-option"
|
|
|
|
key={5}
|
|
|
|
value={t("sessions:Service account")}
|
|
|
|
/>,
|
|
|
|
];
|
2020-12-18 07:01:35 +00:00
|
|
|
|
2020-09-18 08:04:55 +00:00
|
|
|
return (
|
|
|
|
<>
|
2020-12-18 07:01:35 +00:00
|
|
|
<ViewHeader titleKey="sessions:title" subKey="sessions:sessionExplain" />
|
2021-03-31 13:16:58 +00:00
|
|
|
<PageSection variant="light" className="pf-u-p-0">
|
2020-12-18 07:01:35 +00:00
|
|
|
<KeycloakDataTable
|
|
|
|
loader={loader}
|
|
|
|
ariaLabelKey="session:title"
|
|
|
|
searchPlaceholderKey="sessions:searchForSession"
|
2021-07-05 15:58:19 +00:00
|
|
|
searchTypeComponent={
|
|
|
|
<Select
|
|
|
|
width={200}
|
|
|
|
data-testid="filter-session-type-select"
|
|
|
|
isOpen={filterDropdownOpen}
|
|
|
|
className="filter-session-type-select"
|
|
|
|
variant={SelectVariant.single}
|
|
|
|
onToggle={() => setFilterDropdownOpen(!filterDropdownOpen)}
|
|
|
|
toggleIcon={<FilterIcon />}
|
|
|
|
onSelect={(_, value) => {
|
|
|
|
setFilterType(value as string);
|
|
|
|
refresh();
|
|
|
|
setFilterDropdownOpen(false);
|
|
|
|
}}
|
|
|
|
selections={filterType}
|
|
|
|
>
|
|
|
|
{options}
|
|
|
|
</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,
|
|
|
|
},
|
|
|
|
]}
|
|
|
|
/>
|
|
|
|
</PageSection>
|
2020-09-18 08:04:55 +00:00
|
|
|
</>
|
|
|
|
);
|
2020-09-09 09:07:17 +00:00
|
|
|
};
|