Skip to content

Commit

Permalink
group/RP batch delete policies
Browse files Browse the repository at this point in the history
  • Loading branch information
qianwei-yin committed Nov 15, 2024
1 parent eafa23c commit 5f72697
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 10 deletions.
12 changes: 2 additions & 10 deletions ui/src/js/permission-settings/permission-settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ export class PermissionSettings extends TatorPage {

this._getUserData();

// await store.getState().setUserData();

// Project id
// this.projectId = store.getState().projectId;

// This just happens once and unlike getType, it also sets project info
// await store.getState().setProjectData(this.projectId);

// Figure out if something else needs to be shown
this.moveToCurrentHash();

Expand All @@ -79,9 +71,9 @@ export class PermissionSettings extends TatorPage {
// Current User's Group List
await store.getState().getCurrentUserGroupList();
// All Group data
await store.getState().setGroupData();
store.getState().setGroupData();
// Policy data that are associated to this user, this user's groups, this user's organizations
await store.getState().setPolicyData();
store.getState().setPolicyData();

console.log("😇 ~ _init ~ store.getState():", store.getState());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export class PolicyTableViewActions extends TableViewActions {
this._newPolicySearchParams.bind(this)
);

this._delete.addEventListener(
"click",
this._openDeletePoliciesModal.bind(this)
);

this._filterWindow._addConditionButton.addEventListener(
"click",
this._addCondition.bind(this)
Expand Down Expand Up @@ -48,6 +53,121 @@ export class PolicyTableViewActions extends TableViewActions {
this._delete.removeAttribute("disabled");
}
}

setUpWarningDeleteMsg() {
const {
selectedPolicyIds,
Policy: { processedMap },
Group: { groupIdUserIdMap },
} = store.getState();

// If a policy's entity is group, then deleting it can affect the group's members
const affectedUsers = new Map();
selectedPolicyIds.forEach((id) => {
const policy = processedMap.get(id);
if (policy.entityType === "group") {
affectedUsers.set(id, groupIdUserIdMap.get(policy.entityId));
} else {
affectedUsers.set(id, []);
}
});

this._warningDeleteMessage = `
Pressing confirm will delete these policies:<br/><br/><br/>
${selectedPolicyIds
.map((id) => {
return `ID: ${id}, ${processedMap.get(id).entityName} against ${
processedMap.get(id).targetName
}, ${
affectedUsers.get(id).length
? ` This can affect User ${affectedUsers.get(id)}`
: ""
}`;
})
.join("<br/><br/>")}
<br/><br/><br/>
Do you want to continue?
`;
return this._warningDeleteMessage;
}

_openDeletePoliciesModal() {
const button = document.createElement("button");
button.setAttribute("class", "btn btn-clear f1 text-semibold btn-red");

let confirmText = document.createTextNode("Confirm");
button.appendChild(confirmText);

button.addEventListener("click", this._deletePolicies.bind(this));
this.setUpWarningDeleteMsg();

this.modal._confirm({
titleText: `Delete Confirmation`,
mainText: this._warningDeleteMessage,
buttonSave: button,
});
}

async _deletePolicies() {
const { selectedPolicyIds } = store.getState();

this.modal._modalCloseAndClear();
try {
const responses = [];
for (const id of selectedPolicyIds) {
const respData = await store.getState().deletePolicy(id);
responses.push(respData);
}
this.handleResponseList(responses);
} catch (err) {
this.modal._error(err);
}
}

handleResponseList(responses) {
if (responses && Array.isArray(responses)) {
let sCount = 0;
let eCount = 0;
let errors = "";

for (let object of responses) {
if (object.response?.ok) {
sCount++;
} else {
eCount++;
const message = object?.data?.message || "";
errors += `<br/><br/>${message}`;
}
}

if (sCount > 0 && eCount === 0) {
this.modal._success(
`Successfully deleted ${sCount} ${
sCount == 1 ? "policy" : "policies"
}.`
);
store.getState().setPolicyData();
} else if (sCount > 0 && eCount > 0) {
this.modal._complete(
`Successfully deleted ${sCount} ${
sCount == 1 ? "policy" : "policies"
}.<br/><br/>
Error deleting ${eCount} ${
eCount == 1 ? "policy" : "policies"
}.<br/><br/>
Error message${eCount == 1 ? "" : "s"}:<br/><br/>${errors}`
);
store.getState().setPolicyData();
} else {
return this.modal._error(
`Error deleting ${eCount} ${
eCount == 1 ? "policy" : "policies"
}.<br/><br/>
Error message${eCount == 1 ? "" : "s"}:<br/><br/>${errors}`
);
}
}
}
}

customElements.define("policy-table-view-actions", PolicyTableViewActions);

0 comments on commit 5f72697

Please sign in to comment.