Skip to content

Commit

Permalink
Provide Delete button functionality
Browse files Browse the repository at this point in the history
The 'Delete' button should delete a list
of members from the Sudo rules list for
a given user.

Signed-off-by: Carla Martinez <[email protected]>
  • Loading branch information
carma12 committed May 23, 2024
1 parent 785cb25 commit 8b5c53b
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 9 deletions.
63 changes: 61 additions & 2 deletions src/components/MemberOf/MemberOfSudoRules.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ import { User, SudoRule } from "src/utils/datatypes/globalDataTypes";
import MemberOfToolbar, { MembershipDirection } from "./MemberOfToolbar";
import MemberOfTableSudoRules from "./MemberOfTableSudoRules";
import MemberOfAddModal, { AvailableItems } from "./MemberOfAddModal";
import MemberOfDeleteModal from "./MemberOfDeleteModal";
// Hooks
import useAlerts from "src/hooks/useAlerts";
// RPC
import {
useGetSudoRulesInfoByNameQuery,
useAddToSudoRulesMutation,
useGettingSudoRulesQuery,
useRemoveFromSudoRulesMutation,
} from "src/services/rpcSudoRules";
// Utils
import { API_VERSION_BACKUP, paginate } from "src/utils/utils";
Expand Down Expand Up @@ -112,6 +114,7 @@ const MemberOfSudoRules = (props: MemberOfSudoRulesProps) => {

// Dialogs and actions
const [showAddModal, setShowAddModal] = React.useState(false);
const [showDeleteModal, setShowDeleteModal] = React.useState(false);

// Buttons functionality
// - Refresh
Expand All @@ -123,6 +126,7 @@ const MemberOfSudoRules = (props: MemberOfSudoRulesProps) => {
// Add new member to 'Sudo rules'
// API calls
const [addMemberToSudoRules] = useAddToSudoRulesMutation();
const [removeMembersFromSudoRules] = useRemoveFromSudoRulesMutation();
const [adderSearchValue, setAdderSearchValue] = React.useState("");
const [availableSudoRules, setAvailableSudoRules] = React.useState<
SudoRule[]
Expand Down Expand Up @@ -208,6 +212,47 @@ const MemberOfSudoRules = (props: MemberOfSudoRulesProps) => {
});
};

// - Delete
const onDeleteSudoRules = () => {
if (props.user.uid) {
removeMembersFromSudoRules([
props.user.uid,
"user",
sudoRulesSelected,
]).then((response) => {
if ("data" in response) {
if (response.data.result) {
// Set alert: success
alerts.addAlert(
"remove-sudo-rules-success",
"Removed Sudo rules from user '" + props.user.uid + "'",
"success"
);
// Update displayed HBAC rules
const newSudoRules = sudoRules.filter(
(sudoRule) => !sudoRulesSelected.includes(sudoRule.cn)
);
setSudoRules(newSudoRules);
// Update data
setSudoRulesSelected([]);
// Close modal
setShowDeleteModal(false);
// Refresh
props.onRefreshUserData();
} else if (response.data.error) {
// Set alert: error
const errorMessage = response.data.error as unknown as ErrorResult;
alerts.addAlert(
"remove-sudo-rules-error",
errorMessage.message,
"danger"
);
}
}
});
}
};

return (
<>
<alerts.ManagedAlerts />
Expand All @@ -219,8 +264,7 @@ const MemberOfSudoRules = (props: MemberOfSudoRulesProps) => {
refreshButtonEnabled={isRefreshButtonEnabled}
onRefreshButtonClick={props.onRefreshUserData}
deleteButtonEnabled={someItemSelected}
// eslint-disable-next-line @typescript-eslint/no-empty-function
onDeleteButtonClick={() => {}}
onDeleteButtonClick={() => setShowDeleteModal(true)}
addButtonEnabled={isAddButtonEnabled}
onAddButtonClick={() => setShowAddModal(true)}
membershipDirectionEnabled={true}
Expand Down Expand Up @@ -260,6 +304,21 @@ const MemberOfSudoRules = (props: MemberOfSudoRulesProps) => {
ariaLabel="Add user of Sudo rule modal"
/>
)}
{showDeleteModal && someItemSelected && (
<MemberOfDeleteModal
showModal={showDeleteModal}
onCloseModal={() => setShowDeleteModal(false)}
title="Delete user from Sudo rules"
onDelete={onDeleteSudoRules}
>
<MemberOfTableSudoRules
sudoRules={sudoRules.filter((sudorule) =>
sudoRulesSelected.includes(sudorule.cn)
)}
showTableRows
/>
</MemberOfDeleteModal>
)}
</>
);
};
Expand Down
56 changes: 49 additions & 7 deletions src/services/rpcSudoRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,16 @@ import { API_VERSION_BACKUP } from "../utils/utils";
import { SudoRule } from "../utils/datatypes/globalDataTypes";

/**
* Sudo rules-related endpoints: getSudoRulesInfoByName, addToSudoRules
* Sudo rules-related endpoints: getSudoRulesInfoByName, addToSudoRules, removeFromSudoRules
*
* API commands:
* - sudorule_show: https://freeipa.readthedocs.io/en/latest/api/hbacrule_show.html
* - sudorule_add_user: https://freeipa.readthedocs.io/en/latest/api/hbacrule_add_user.html
* - sudorule_add_host: https://freeipa.readthedocs.io/en/latest/api/hbacrule_add_host.html
* - sudorule_add_option: https://freeipa.readthedocs.io/en/latest/api/hbacrule_add_option.html
* - sudorule_show: https://freeipa.readthedocs.io/en/latest/api/sudorule_show.html
* - sudorule_add_user: https://freeipa.readthedocs.io/en/latest/api/sudorule_add_user.html
* - sudorule_add_host: https://freeipa.readthedocs.io/en/latest/api/sudorule_add_host.html
* - sudorule_add_option: https://freeipa.readthedocs.io/en/latest/api/sudorule_add_option.html
* - sudorule_remove_user: https://freeipa.readthedocs.io/en/latest/api/sudorule_remove_user.html
* - sudorule_remove_host: https://freeipa.readthedocs.io/en/latest/api/sudorule_remove_host.html
* - sudorule_remove_option: https://freeipa.readthedocs.io/en/latest/api/sudorule_remove_option.html
*
*/

Expand Down Expand Up @@ -112,6 +115,42 @@ const extendedApi = api.injectEndpoints({
return getBatchCommand(membersToAdd, API_VERSION_BACKUP);
},
}),
/**
* Delete entity from Sudo rules
* @param {string} memberId - ID of the entity to remove from Sudo rules
* @param {string} memberType - Type of the entity
* Available types: user | host | option
* @param {string[]} listOfSudoRules - List of members to remove from Sudo rules
*/
removeFromSudoRules: build.mutation<
BatchRPCResponse,
[string, string, string[]]
>({
query: (payload) => {
const memberId = payload[0];
const memberType = payload[1];
const listOfSudoRules = payload[2];

let methodType = "";
if (memberType === "user") {
methodType = "sudorule_remove_user";
} else if (memberType === "host") {
methodType = "sudorule_remove_host";
} else if (memberType === "option") {
methodType = "sudorule_remove_option";
}

const membersToRemove: Command[] = [];
listOfSudoRules.map((hbacrule) => {
const payloadItem: Command = {
method: methodType,
params: [[hbacrule], { [memberType]: memberId }],
};
membersToRemove.push(payloadItem);
});
return getBatchCommand(membersToRemove, API_VERSION_BACKUP);
},
}),
}),
overrideExisting: false,
});
Expand All @@ -122,5 +161,8 @@ export const useGettingSudoRulesQuery = (payloadData) => {
return useGettingGenericQuery(payloadData);
};

export const { useGetSudoRulesInfoByNameQuery, useAddToSudoRulesMutation } =
extendedApi;
export const {
useGetSudoRulesInfoByNameQuery,
useAddToSudoRulesMutation,
useRemoveFromSudoRulesMutation,
} = extendedApi;

0 comments on commit 8b5c53b

Please sign in to comment.