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";
|
2022-08-03 12:12:07 +00:00
|
|
|
import { useState } from "react";
|
2021-07-05 15:58:19 +00:00
|
|
|
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 { RevocationModal } from "./RevocationModal";
|
|
|
|
import SessionsTable from "./SessionsTable";
|
2022-06-01 09:12:30 +00:00
|
|
|
import { useConfirmDialog } from "../components/confirm-dialog/ConfirmDialog";
|
|
|
|
import { useAlerts } from "../components/alert/Alerts";
|
|
|
|
import { useRealm } from "../context/realm-context/RealmContext";
|
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() {
|
2022-05-18 09:16:20 +00:00
|
|
|
const { t } = useTranslation("sessions");
|
2022-06-01 09:12:30 +00:00
|
|
|
|
2022-07-14 13:02:28 +00:00
|
|
|
const { keycloak, adminClient } = useAdminClient();
|
2022-06-01 09:12:30 +00:00
|
|
|
const { addError } = useAlerts();
|
|
|
|
const { realm } = useRealm();
|
|
|
|
|
2021-08-16 19:25:36 +00:00
|
|
|
const [revocationModalOpen, setRevocationModalOpen] = 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);
|
|
|
|
};
|
|
|
|
|
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
|
|
|
|
2022-06-01 09:12:30 +00:00
|
|
|
const [toggleLogoutDialog, LogoutConfirm] = useConfirmDialog({
|
|
|
|
titleKey: "sessions:logoutAllSessions",
|
|
|
|
messageKey: "sessions:logoutAllDescription",
|
|
|
|
continueButtonLabel: "common:confirm",
|
|
|
|
onConfirm: async () => {
|
|
|
|
try {
|
|
|
|
await adminClient.realms.logoutAll({ realm });
|
2022-07-14 13:02:28 +00:00
|
|
|
keycloak.logout({ redirectUri: "" });
|
2022-06-01 09:12:30 +00:00
|
|
|
} catch (error) {
|
|
|
|
addError("sessions:logoutAllSessionsError", error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
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}
|
2022-06-01 09:12:30 +00:00
|
|
|
onClick={toggleLogoutDialog}
|
2021-08-16 19:25:36 +00:00
|
|
|
>
|
|
|
|
{t("signOutAllActiveSessions")}
|
|
|
|
</DropdownItem>,
|
|
|
|
];
|
|
|
|
|
2020-09-18 08:04:55 +00:00
|
|
|
return (
|
|
|
|
<>
|
2022-06-01 09:12:30 +00:00
|
|
|
<LogoutConfirm />
|
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();
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
)}
|
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
|
|
|
}
|