Skip to content

Commit

Permalink
Add 'Rebuild auto membership' option
Browse files Browse the repository at this point in the history
The 'Rebuild auto membership' option
should apply any specified rule to
a given user.

Signed-off-by: Carla Martinez <[email protected]>
  • Loading branch information
carma12 committed Dec 18, 2023
1 parent d6f59a1 commit 1b0086f
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 3 deletions.
22 changes: 21 additions & 1 deletion src/components/UserSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<User>;
Expand Down Expand Up @@ -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);

Expand All @@ -148,7 +159,10 @@ const UserSettings = (props: PropsToUserSettings) => {
Unlock
</DropdownItem>,
<DropdownItem key="add otp token">Add OTP token</DropdownItem>,
<DropdownItem key="rebuild auto membership">
<DropdownItem
key="rebuild auto membership"
onClick={() => setIsRebuildAutoMembershipModalOpen(true)}
>
Rebuild auto membership
</DropdownItem>,
<DropdownItem key="new certificate">New certificate</DropdownItem>,
Expand Down Expand Up @@ -433,6 +447,12 @@ const UserSettings = (props: PropsToUserSettings) => {
fromSettings={true}
onRefresh={props.onRefresh}
/>
<RebuildAutoMembership
isOpen={isRebuildAutoMembershipModalOpen}
onClose={onCloseRebuildAutoMembershipModal}
entriesToRebuild={userToRebuild}
entity="users"
/>
</>
);
};
Expand Down
125 changes: 125 additions & 0 deletions src/components/modals/RebuildAutoMembership.tsx
Original file line number Diff line number Diff line change
@@ -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[] = [
<Button
key="rebuild-auto-membership"
variant="primary"
onClick={onRebuildAutoMembership}
form="rebuild-auto-membership-modal"
>
OK
</Button>,
<Button
key="cancel-rebuild-auto-membership"
variant="link"
onClick={props.onClose}
>
Cancel
</Button>,
];

// 'Rebuild auto membership' modal fields: Confirmation question
const confirmationQuestion = [
{
id: "question-text",
pfComponent: (
<TextLayout component="p">
<b>Warning</b> 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?
</TextLayout>
),
},
];

// Render component
return (
<>
<alerts.ManagedAlerts />
<ModalWithFormLayout
variantType="medium"
modalPosition="top"
offPosition="76px"
title="Confirmation"
formId="rebuild-auto-membership-modal"
fields={confirmationQuestion}
show={props.isOpen}
onClose={props.onClose}
actions={membershipModalActions}
/>
</>
);
};

export default RebuildAutoMembership;
4 changes: 2 additions & 2 deletions src/services/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand All @@ -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,
};

Expand Down

0 comments on commit 1b0086f

Please sign in to comment.