keycloak-scim/src/events/AdminEvents.tsx

608 lines
19 KiB
TypeScript
Raw Normal View History

2020-12-11 14:48:39 +00:00
import {
ActionGroup,
2020-12-11 14:48:39 +00:00
Button,
Chip,
ChipGroup,
Dropdown,
DropdownToggle,
Flex,
FlexItem,
Form,
FormGroup,
2020-12-11 14:48:39 +00:00
Modal,
ModalVariant,
Select,
SelectOption,
SelectVariant,
2020-12-11 14:48:39 +00:00
} from "@patternfly/react-core";
import {
cellWidth,
2020-12-11 14:48:39 +00:00
Table,
TableBody,
TableHeader,
TableVariant,
} from "@patternfly/react-table";
import { CodeEditor, Language } from "@patternfly/react-code-editor";
import type AdminEventRepresentation from "@keycloak/keycloak-admin-client/lib/defs/adminEventRepresentation";
import moment from "moment";
import React, { FunctionComponent, useMemo, useState } from "react";
import { Controller, useForm } from "react-hook-form";
import { useTranslation } from "react-i18next";
import { pickBy } from "lodash-es";
2020-12-11 14:48:39 +00:00
import { ListEmptyState } from "../components/list-empty-state/ListEmptyState";
import { KeycloakDataTable } from "../components/table-toolbar/KeycloakDataTable";
import { KeycloakTextInput } from "../components/keycloak-text-input/KeycloakTextInput";
import { useAdminClient } from "../context/auth/AdminClient";
import { useRealm } from "../context/realm-context/RealmContext";
import { useServerInfo } from "../context/server-info/ServerInfoProvider";
import { prettyPrintJSON } from "../util";
import { CellResourceLinkRenderer } from "./ResourceLinks";
import "./events.css";
2020-12-11 14:48:39 +00:00
type DisplayDialogProps = {
titleKey: string;
onClose: () => void;
};
type AdminEventSearchForm = {
resourceTypes: string[];
operationTypes: string[];
resourcePath: string;
dateFrom: string;
dateTo: string;
authClient: string;
authUser: string;
authRealm: string;
authIpAddress: string;
};
const defaultValues: AdminEventSearchForm = {
resourceTypes: [],
operationTypes: [],
resourcePath: "",
dateFrom: "",
dateTo: "",
authClient: "",
authUser: "",
authRealm: "",
authIpAddress: "",
};
const DisplayDialog: FunctionComponent<DisplayDialogProps> = ({
titleKey,
onClose,
children,
}) => {
2020-12-11 14:48:39 +00:00
const { t } = useTranslation("events");
return (
<Modal
variant={ModalVariant.medium}
title={t(titleKey)}
isOpen={true}
onClose={onClose}
>
{children}
</Modal>
);
};
export const AdminEvents = () => {
const { t } = useTranslation("events");
const adminClient = useAdminClient();
const { realm } = useRealm();
const serverInfo = useServerInfo();
const resourceTypes = serverInfo.enums?.["resourceType"];
const operationTypes = serverInfo.enums?.["operationType"];
2020-12-11 14:48:39 +00:00
const [key, setKey] = useState(0);
const [searchDropdownOpen, setSearchDropdownOpen] = useState(false);
const [selectResourceTypesOpen, setSelectResourceTypesOpen] = useState(false);
const [selectOperationTypesOpen, setSelectOperationTypesOpen] =
useState(false);
const [activeFilters, setActiveFilters] = useState<
Partial<AdminEventSearchForm>
>({});
2020-12-11 14:48:39 +00:00
const [authEvent, setAuthEvent] = useState<AdminEventRepresentation>();
const [representationEvent, setRepresentationEvent] =
useState<AdminEventRepresentation>();
2020-12-11 14:48:39 +00:00
const filterLabels: Record<keyof AdminEventSearchForm, string> = {
resourceTypes: t("resourceTypes"),
operationTypes: t("operationTypes"),
resourcePath: t("resourcePath"),
dateFrom: t("dateFrom"),
dateTo: t("dateTo"),
authClient: t("client"),
authUser: t("userId"),
authRealm: t("realm"),
authIpAddress: t("ipAddress"),
};
const {
getValues,
register,
reset,
formState: { isDirty },
control,
} = useForm<AdminEventSearchForm>({
shouldUnregister: false,
mode: "onChange",
defaultValues,
});
function loader(first?: number, max?: number) {
return adminClient.realms.findAdminEvents({
// The admin client wants 'dateFrom' and 'dateTo' to be Date objects, however it cannot actually handle them so we need to cast to any.
...(activeFilters as any),
2020-12-11 14:48:39 +00:00
realm,
first,
max,
});
}
function submitSearch() {
setSearchDropdownOpen(false);
commitFilters();
}
function resetSearch() {
reset();
commitFilters();
}
function removeFilter(key: keyof AdminEventSearchForm) {
const formValues: AdminEventSearchForm = { ...getValues() };
delete formValues[key];
reset({ ...defaultValues, ...formValues });
commitFilters();
}
function removeFilterValue(
key: keyof AdminEventSearchForm,
valueToRemove: string
) {
const formValues = getValues();
const fieldValue = formValues[key];
const newFieldValue = Array.isArray(fieldValue)
? fieldValue.filter((val) => val !== valueToRemove)
: fieldValue;
reset({ ...formValues, [key]: newFieldValue });
commitFilters();
}
function commitFilters() {
const newFilters: Partial<AdminEventSearchForm> = pickBy(
getValues(),
(value) => value !== "" || (Array.isArray(value) && value.length > 0)
);
setActiveFilters(newFilters);
setKey(key + 1);
}
function refresh() {
commitFilters();
}
2020-12-11 14:48:39 +00:00
const adminEventSearchFormDisplay = () => {
return (
2021-08-26 12:15:28 +00:00
<Flex
direction={{ default: "column" }}
spaceItems={{ default: "spaceItemsNone" }}
>
<FlexItem>
<Dropdown
id="admin-events-search-select"
data-testid="AdminEventsSearchSelector"
className="pf-u-ml-md"
toggle={
<DropdownToggle
data-testid="adminEventsSearchSelectorToggle"
onToggle={(isOpen) => setSearchDropdownOpen(isOpen)}
className="keycloak__events_search_selector_dropdown__toggle"
>
{t("searchForAdminEvent")}
</DropdownToggle>
}
isOpen={searchDropdownOpen}
>
<Form
isHorizontal
className="keycloak__events_search__form"
2021-08-26 12:15:28 +00:00
data-testid="searchForm"
>
2021-08-26 12:15:28 +00:00
<FormGroup
label={t("resourceTypes")}
fieldId="kc-resourceTypes"
className="keycloak__events_search__form_label"
>
<Controller
name="resourceTypes"
control={control}
render={({
onChange,
value,
}: {
onChange: (newValue: string[]) => void;
value: string[];
}) => (
<Select
className="keycloak__events_search__type_select"
name="resourceTypes"
data-testid="resource-types-searchField"
chipGroupProps={{
numChips: 1,
Profile view update (#1357) * added routing for viewing client profile * added add executors form template * added add executors form template * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * added adding excutors to profiles * added displaying executors - wip * added displaying executors - wip * added navigation to client profile edit on executor creation * replaced table with list for listing executors added * added support for editing client profile * added logic making executors with config links only * added read only values for edit/view client temporarily * added helpText for added executors listed in executors list * added helpText for added executors listed in executors list * added deleting executor from client profile * fixed deleting client profile and fixed messages for delete modals * fixed message for delete clinet profile modal * combined delete dialogs for client profile and executor * displaying global executors for global profiles, hiding add executor button * fixed eslint issue * fixed executors list * added back button to global profile view/edit view * fixed test * added global batche and hid actions dropdown for global profiles * fixed switch on/off labels * fixed hide/display items for global and non-global profiles * added isDirty * feedback fixes * feedback fixes * feedback fixes * feedback fixes * feedback fixes * feedback fix * small refactor * added name and removed unused state * fixed executor creation * fixed executor creation * added saving edited client profile * added saving edited client profile * improved trash icon styles * test fix * Some code suggestions * Some more code suggestions * feedback fixes * feedback fixes * use find instead of filter * feedback fixes * added defaultValues for executors * removed defaultValues for executors * final feedback fixes * minor fixes Co-authored-by: Agnieszka Gancarczyk <agancarc@redhat.com> Co-authored-by: Erik Jan de Wit <erikjan.dewit@gmail.com> Co-authored-by: Jon Koops <jonkoops@gmail.com>
2021-10-26 20:16:19 +00:00
expandedText: t("common:hide"),
collapsedText: t("common:showRemaining"),
}}
variant={SelectVariant.typeaheadMulti}
typeAheadAriaLabel="Select"
onToggle={(isOpen) => setSelectResourceTypesOpen(isOpen)}
selections={value}
onSelect={(_, selectedValue) => {
const option = selectedValue.toString();
const changedValue = value.includes(option)
? value.filter((item) => item !== option)
: [...value, option];
onChange(changedValue);
}}
onClear={(resource) => {
resource.stopPropagation();
onChange([]);
}}
isOpen={selectResourceTypesOpen}
aria-labelledby={"resourceTypes"}
chipGroupComponent={
<ChipGroup>
{value.map((chip) => (
<Chip
key={chip}
onClick={(resource) => {
resource.stopPropagation();
onChange(value.filter((val) => val !== chip));
}}
>
{chip}
</Chip>
))}
</ChipGroup>
}
>
{resourceTypes?.map((option) => (
<SelectOption key={option} value={option} />
))}
</Select>
)}
/>
2021-08-26 12:15:28 +00:00
</FormGroup>
<FormGroup
label={t("operationTypes")}
fieldId="kc-operationTypes"
className="keycloak__events_search__form_label"
2021-08-26 12:15:28 +00:00
>
<Controller
name="operationTypes"
control={control}
render={({
onChange,
value,
}: {
onChange: (newValue: string[]) => void;
value: string[];
}) => (
<Select
className="keycloak__events_search__type_select"
name="operationTypes"
data-testid="operation-types-searchField"
chipGroupProps={{
numChips: 1,
Profile view update (#1357) * added routing for viewing client profile * added add executors form template * added add executors form template * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * add executor: wip * added adding excutors to profiles * added displaying executors - wip * added displaying executors - wip * added navigation to client profile edit on executor creation * replaced table with list for listing executors added * added support for editing client profile * added logic making executors with config links only * added read only values for edit/view client temporarily * added helpText for added executors listed in executors list * added helpText for added executors listed in executors list * added deleting executor from client profile * fixed deleting client profile and fixed messages for delete modals * fixed message for delete clinet profile modal * combined delete dialogs for client profile and executor * displaying global executors for global profiles, hiding add executor button * fixed eslint issue * fixed executors list * added back button to global profile view/edit view * fixed test * added global batche and hid actions dropdown for global profiles * fixed switch on/off labels * fixed hide/display items for global and non-global profiles * added isDirty * feedback fixes * feedback fixes * feedback fixes * feedback fixes * feedback fixes * feedback fix * small refactor * added name and removed unused state * fixed executor creation * fixed executor creation * added saving edited client profile * added saving edited client profile * improved trash icon styles * test fix * Some code suggestions * Some more code suggestions * feedback fixes * feedback fixes * use find instead of filter * feedback fixes * added defaultValues for executors * removed defaultValues for executors * final feedback fixes * minor fixes Co-authored-by: Agnieszka Gancarczyk <agancarc@redhat.com> Co-authored-by: Erik Jan de Wit <erikjan.dewit@gmail.com> Co-authored-by: Jon Koops <jonkoops@gmail.com>
2021-10-26 20:16:19 +00:00
expandedText: t("common:hide"),
collapsedText: t("common:showRemaining"),
}}
variant={SelectVariant.typeaheadMulti}
typeAheadAriaLabel="Select"
onToggle={(isOpen) => setSelectOperationTypesOpen(isOpen)}
selections={value}
onSelect={(_, selectedValue) => {
const option = selectedValue.toString();
const changedValue = value.includes(option)
? value.filter((item) => item !== option)
: [...value, option];
onChange(changedValue);
}}
onClear={(operation) => {
operation.stopPropagation();
onChange([]);
}}
isOpen={selectOperationTypesOpen}
aria-labelledby={"operationTypes"}
chipGroupComponent={
<ChipGroup>
{value.map((chip) => (
<Chip
key={chip}
onClick={(operation) => {
operation.stopPropagation();
onChange(value.filter((val) => val !== chip));
}}
>
{chip}
</Chip>
))}
</ChipGroup>
}
>
{operationTypes?.map((option) => (
<SelectOption key={option} value={option} />
))}
</Select>
)}
/>
2021-08-26 12:15:28 +00:00
</FormGroup>
<FormGroup
label={t("resourcePath")}
fieldId="kc-resourcePath"
2021-08-26 12:15:28 +00:00
className="keycloak__events_search__form_label"
>
<KeycloakTextInput
2021-08-26 12:15:28 +00:00
ref={register()}
type="text"
id="kc-resourcePath"
name="resourcePath"
data-testid="resourcePath-searchField"
2021-08-26 12:15:28 +00:00
/>
</FormGroup>
<FormGroup
label={t("realm")}
fieldId="kc-realm"
className="keycloak__events_search__form_label"
>
<KeycloakTextInput
ref={register()}
type="text"
id="kc-realm"
name="authRealm"
data-testid="realm-searchField"
/>
</FormGroup>
<FormGroup
label={t("client")}
fieldId="kc-client"
className="keycloak__events_search__form_label"
>
<KeycloakTextInput
ref={register()}
type="text"
id="kc-client"
name="authClient"
data-testid="client-searchField"
/>
</FormGroup>
<FormGroup
label={t("user")}
fieldId="kc-user"
className="keycloak__events_search__form_label"
>
<KeycloakTextInput
ref={register()}
type="text"
id="kc-user"
name="authUser"
data-testid="user-searchField"
/>
2021-08-26 12:15:28 +00:00
</FormGroup>
<FormGroup
label={t("ipAddress")}
fieldId="kc-ipAddress"
className="keycloak__events_search__form_label"
>
<KeycloakTextInput
2021-08-26 12:15:28 +00:00
ref={register()}
type="text"
id="kc-ipAddress"
name="authIpAddress"
2021-08-26 12:15:28 +00:00
data-testid="ipAddress-searchField"
/>
</FormGroup>
<FormGroup
label={t("dateFrom")}
fieldId="kc-dateFrom"
className="keycloak__events_search__form_label"
>
<KeycloakTextInput
2021-08-26 12:15:28 +00:00
ref={register()}
type="text"
id="kc-dateFrom"
name="dateFrom"
className="pf-c-form-control pf-m-icon pf-m-calendar"
placeholder="yyyy-MM-dd"
data-testid="dateFrom-searchField"
/>
</FormGroup>
<FormGroup
label={t("dateTo")}
fieldId="kc-dateTo"
className="keycloak__events_search__form_label"
>
<KeycloakTextInput
2021-08-26 12:15:28 +00:00
ref={register()}
type="text"
id="kc-dateTo"
name="dateTo"
className="pf-c-form-control pf-m-icon pf-m-calendar"
placeholder="yyyy-MM-dd"
data-testid="dateTo-searchField"
/>
</FormGroup>
<ActionGroup>
<Button
variant={"primary"}
onClick={submitSearch}
2021-08-26 12:15:28 +00:00
data-testid="search-events-btn"
isDisabled={!isDirty}
>
2021-08-26 12:15:28 +00:00
{t("searchAdminEventsBtn")}
</Button>
<Button
variant="secondary"
onClick={resetSearch}
isDisabled={!isDirty}
>
{t("resetBtn")}
</Button>
2021-08-26 12:15:28 +00:00
</ActionGroup>
</Form>
</Dropdown>
<Button
className="pf-u-ml-md"
onClick={refresh}
data-testid="refresh-btn"
>
{t("refresh")}
</Button>
</FlexItem>
<FlexItem>
{Object.entries(activeFilters).length > 0 && (
<div className="keycloak__searchChips pf-u-ml-md">
{Object.entries(activeFilters).map((filter) => {
const [key, value] = filter as [
keyof AdminEventSearchForm,
string | string[]
];
return (
<ChipGroup
className="pf-u-mt-md pf-u-mr-md"
key={key}
categoryName={filterLabels[key]}
isClosable
onClick={() => removeFilter(key)}
>
{typeof value === "string" ? (
<Chip isReadOnly>{value}</Chip>
) : (
value.map((entry) => (
<Chip
key={entry}
onClick={() => removeFilterValue(key, entry)}
>
{entry}
</Chip>
))
)}
</ChipGroup>
);
})}
</div>
)}
</FlexItem>
2021-08-26 12:15:28 +00:00
</Flex>
);
};
const rows = [
[t("realm"), authEvent?.authDetails?.realmId],
[t("client"), authEvent?.authDetails?.clientId],
[t("user"), authEvent?.authDetails?.userId],
[t("ipAddress"), authEvent?.authDetails?.ipAddress],
];
const code = useMemo(
() =>
representationEvent?.representation
? prettyPrintJSON(JSON.parse(representationEvent.representation))
: "",
[representationEvent?.representation]
);
2020-12-11 14:48:39 +00:00
return (
<>
{authEvent && (
<DisplayDialog titleKey="auth" onClose={() => setAuthEvent(undefined)}>
<Table
aria-label="authData"
data-testid="auth-dialog"
2020-12-11 14:48:39 +00:00
variant={TableVariant.compact}
cells={[t("attribute"), t("value")]}
rows={rows}
2020-12-11 14:48:39 +00:00
>
<TableHeader />
<TableBody />
</Table>
</DisplayDialog>
)}
{representationEvent && (
<DisplayDialog
titleKey="representation"
data-testid="representation-dialog"
2020-12-11 14:48:39 +00:00
onClose={() => setRepresentationEvent(undefined)}
>
<CodeEditor
isLineNumbersVisible
isReadOnly
code={code}
language={Language.json}
Realm setting profiles (#1149) * admin-events-list: added PF code editor * realm-settings: added template for client policies tab * realm-settings: fixed space between text and q icon * admin-events-list: hardcoded representation dialog * realm-settings: css improvement * realm-settings: feedback applied * realm-settings: feedback applied * admin-events-list: fixed representation dialog * admin-events-list: fixed representation dialog * Update src/events/AdminEvents.tsx Co-authored-by: Jon Koops <jonkoops@gmail.com> * admin-events-list: fixed representation dialog * real-settings-profile: added subtabs * realm-settings-list: added table displaying client profiles and delete dialog * realm-settings-list: updated column name * realm-settings-list: implemented radio button format change * realm-settings-list: added buttons for JSON editor * realm-settings-list: added isPaginated to profiles table * realm-settings-list: impoved css for json editor * realm-settings-list: fix for loader * realm-settings-list: feedback fixes * realm-settings-list: feedback fixes * realm-settings-list: feedback fixes * realm-settings-list: feedback fixes * realm-settings-list: feedback fixes * realm-settings-list: feedback fixes * realm-settings-list: disbaled row when profile is global * realm-settings-new-profile: added template for new profile - wip * realm-settings-list: feedback fixes * Add some @ts-ignore comments for the links * Split routes into multiple files * realm-settings: temporarily hardcoded path for cancelling new profile btn, bumped admin-client dep * realm-settings: reverted back to Link Co-authored-by: Agnieszka Gancarczyk <agancarc@redhat.com> Co-authored-by: Jon Koops <jonkoops@gmail.com>
2021-09-20 12:42:07 +00:00
height="8rem"
/>
2020-12-11 14:48:39 +00:00
</DisplayDialog>
)}
<KeycloakDataTable
2020-12-11 14:48:39 +00:00
key={key}
loader={loader}
isPaginated
2020-12-11 17:25:54 +00:00
ariaLabelKey="events:adminEvents"
toolbarItem={adminEventSearchFormDisplay()}
2020-12-11 14:48:39 +00:00
actions={[
{
title: t("auth"),
onRowClick: (event) => setAuthEvent(event),
},
{
title: t("representation"),
onRowClick: (event) => setRepresentationEvent(event),
},
]}
columns={[
{
name: "time",
displayKey: "events:time",
cellRenderer: (row) => moment(row.time).format("LLL"),
2020-12-11 14:48:39 +00:00
},
{
name: "resourcePath",
displayKey: "events:resourcePath",
cellRenderer: CellResourceLinkRenderer,
2020-12-11 14:48:39 +00:00
},
{
name: "resourceType",
displayKey: "events:resourceType",
},
{
name: "operationType",
displayKey: "events:operationType",
transforms: [cellWidth(10)],
2020-12-11 14:48:39 +00:00
},
{
name: "",
displayKey: "events:user",
cellRenderer: (event) => event.authDetails?.userId,
},
]}
emptyState={
<ListEmptyState
message={t("emptyEvents")}
instructions={t("emptyEventsInstructions")}
/>
}
isSearching={Object.keys(activeFilters).length > 0}
2020-12-11 14:48:39 +00:00
/>
</>
);
};