From 1b0086f61edcf5c92e15ece907166db4cafe1693 Mon Sep 17 00:00:00 2001 From: Carla Martinez Date: Thu, 30 Nov 2023 09:35:18 +0100 Subject: [PATCH] Add 'Rebuild auto membership' option The 'Rebuild auto membership' option should apply any specified rule to a given user. Signed-off-by: Carla Martinez --- src/components/UserSettings.tsx | 22 ++- .../modals/RebuildAutoMembership.tsx | 125 ++++++++++++++++++ src/services/rpc.ts | 4 +- 3 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 src/components/modals/RebuildAutoMembership.tsx diff --git a/src/components/UserSettings.tsx b/src/components/UserSettings.tsx index 8fab032f..57083102 100644 --- a/src/components/UserSettings.tsx +++ b/src/components/UserSettings.tsx @@ -50,6 +50,7 @@ import useAlerts from "src/hooks/useAlerts"; // Modals import DisableEnableUsers from "./modals/DisableEnableUsers"; import DeleteUsers from "./modals/DeleteUsers"; +import RebuildAutoMembership from "./modals/RebuildAutoMembership"; export interface PropsToUserSettings { originalUser: Partial; @@ -122,6 +123,16 @@ const UserSettings = (props: PropsToUserSettings) => { setIsDeleteModalOpen(false); }; + // 'Rebuild auto membership' option + const [ + isRebuildAutoMembershipModalOpen, + setIsRebuildAutoMembershipModalOpen, + ] = useState(false); + const onCloseRebuildAutoMembershipModal = () => { + setIsRebuildAutoMembershipModalOpen(false); + }; + const userToRebuild = props.user.uid ? [props.user.uid] : []; + // Kebab const [isKebabOpen, setIsKebabOpen] = useState(false); @@ -148,7 +159,10 @@ const UserSettings = (props: PropsToUserSettings) => { Unlock , Add OTP token, - + setIsRebuildAutoMembershipModalOpen(true)} + > Rebuild auto membership , New certificate, @@ -433,6 +447,12 @@ const UserSettings = (props: PropsToUserSettings) => { fromSettings={true} onRefresh={props.onRefresh} /> + ); }; diff --git a/src/components/modals/RebuildAutoMembership.tsx b/src/components/modals/RebuildAutoMembership.tsx new file mode 100644 index 00000000..2f3ff64c --- /dev/null +++ b/src/components/modals/RebuildAutoMembership.tsx @@ -0,0 +1,125 @@ +import React from "react"; +// PatternFly +import { Button } from "@patternfly/react-core"; +// Hooks +import useAlerts from "src/hooks/useAlerts"; +// Modals +import ModalWithFormLayout from "../layouts/ModalWithFormLayout"; +// Components +import TextLayout from "../layouts/TextLayout"; +// RPC +import { + ErrorResult, + useAutoMemberRebuildHostsMutation, + useAutoMemberRebuildUsersMutation, +} from "src/services/rpc"; + +interface PropsToRebuildAutoMembership { + isOpen: boolean; + onClose: () => void; + entriesToRebuild: string[]; + entity: "users" | "hosts" | "groups"; +} + +// TODO: Use this component in those places where the Rebuild auto membership modal is used +const RebuildAutoMembership = (props: PropsToRebuildAutoMembership) => { + // Alerts to show in the UI + const alerts = useAlerts(); + + // RPC hook + const [rebuildAutoMembership] = + props.entity === "users" + ? useAutoMemberRebuildUsersMutation() + : useAutoMemberRebuildHostsMutation(); + // TODO: Adapt this to use it with 'Groups'. + + // [API call] 'Rebuild auto membership' + const onRebuildAutoMembership = () => { + // Task can potentially run for a very long time, give feed back that we + // at least started the task + alerts.addAlert( + "rebuild-automember-start", + "Starting automember rebuild membership task (this may take a long " + + "time to complete) ...", + "info" + ); + + rebuildAutoMembership(props.entriesToRebuild).then((response) => { + if ("data" in response) { + if (response.data.result) { + // Close modal + props.onClose(); + // Set alert: success + alerts.addAlert( + "rebuild-auto-membership-success", + "Automember rebuild membership task completed", + "success" + ); + } else if (response.data.error) { + // Set alert: error + const errorMessage = response.data.error as ErrorResult; + alerts.addAlert( + "rebuild-auto-membership-error", + errorMessage.message, + "danger" + ); + } + } + }); + }; + + // Actions + const membershipModalActions: JSX.Element[] = [ + , + , + ]; + + // 'Rebuild auto membership' modal fields: Confirmation question + const confirmationQuestion = [ + { + id: "question-text", + pfComponent: ( + + Warning In case of a high number of users, hosts or groups, the + rebuild task may require high CPU usage. This can severely impact + server performance. Typically this only needs to be done once after + importing raw data into the server. Are you sure you want to rebuild + the auto memberships? + + ), + }, + ]; + + // Render component + return ( + <> + + + + ); +}; + +export default RebuildAutoMembership; diff --git a/src/services/rpc.ts b/src/services/rpc.ts index c26b7c88..68f70a8d 100644 --- a/src/services/rpc.ts +++ b/src/services/rpc.ts @@ -643,7 +643,7 @@ export const api = createApi({ users.length === 0 ? { type: "group", version: API_VERSION_BACKUP } : { - users: users.map((uid) => uid[0]), + users: users.map((uid) => uid), version: API_VERSION_BACKUP, }; @@ -660,7 +660,7 @@ export const api = createApi({ hosts.length === 0 ? { type: "group", version: API_VERSION_BACKUP } : { - hosts: hosts.map((fqdn) => fqdn[0]), + hosts: hosts.map((fqdn) => fqdn), version: API_VERSION_BACKUP, };