2022-05-18 09:16:20 +00:00
|
|
|
import type ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation";
|
|
|
|
import { DropdownItem, PageSection } from "@patternfly/react-core";
|
2021-07-05 15:58:19 +00:00
|
|
|
import React, { useState } from "react";
|
|
|
|
import { useTranslation } from "react-i18next";
|
2020-12-18 07:01:35 +00:00
|
|
|
|
|
|
|
import { ViewHeader } from "../components/view-header/ViewHeader";
|
|
|
|
import { useAdminClient } from "../context/auth/AdminClient";
|
2021-12-21 15:32:53 +00:00
|
|
|
import helpUrls from "../help-urls";
|
2022-05-18 09:16:20 +00:00
|
|
|
import { LogoutAllSessionsModal } from "./LogoutAllSessionsModal";
|
|
|
|
import { RevocationModal } from "./RevocationModal";
|
|
|
|
import SessionsTable from "./SessionsTable";
|
2020-12-18 07:01:35 +00:00
|
|
|
|
2022-02-22 13:30:18 +00:00
|
|
|
import "./SessionsSection.css";
|
|
|
|
|
2021-10-29 16:11:06 +00:00
|
|
|
export default function SessionsSection() {
|
2020-12-18 07:01:35 +00:00
|
|
|
const adminClient = useAdminClient();
|
2022-05-18 09:16:20 +00:00
|
|
|
const { t } = useTranslation("sessions");
|
2021-08-16 19:25:36 +00:00
|
|
|
const [revocationModalOpen, setRevocationModalOpen] = useState(false);
|
|
|
|
const [logoutAllSessionsModalOpen, setLogoutAllSessionsModalOpen] =
|
|
|
|
useState(false);
|
|
|
|
const [activeClientDetails, setActiveClientDetails] = useState<
|
|
|
|
ClientRepresentation[]
|
|
|
|
>([]);
|
|
|
|
const [noSessions, setNoSessions] = useState(false);
|
2021-07-05 15:58:19 +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-12-21 15:32:53 +00:00
|
|
|
helpUrl={helpUrls.sessionsUrl}
|
2021-08-16 19:25:36 +00:00
|
|
|
/>
|
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}
|
|
|
|
/>
|
|
|
|
)}
|
2022-05-18 09:16:20 +00:00
|
|
|
<SessionsTable loader={loader} />
|
2020-12-18 07:01:35 +00:00
|
|
|
</PageSection>
|
2020-09-18 08:04:55 +00:00
|
|
|
</>
|
|
|
|
);
|
2021-10-29 16:11:06 +00:00
|
|
|
}
|