Skip to content

Commit 1b9b626

Browse files
Copilotphrocker
andcommitted
Fix missing editRule function and chart.js module loading error (#256)
* Initial plan * Implement editRule function to fix missing function error Co-authored-by: phrocker <[email protected]> * Fix chart.js import error by using UMD version only Co-authored-by: phrocker <[email protected]> * Improve editRule function with JSDoc and refactored URL mapping Co-authored-by: phrocker <[email protected]> * Extract shared URL mapping function to reduce code duplication Co-authored-by: phrocker <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: phrocker <[email protected]>
1 parent 50b6729 commit 1b9b626

File tree

2 files changed

+63
-21
lines changed

2 files changed

+63
-21
lines changed

api/src/main/resources/static/js/rules.js

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,60 @@ function assignRule(ruleId) {
5757
modal.show();
5858
}
5959

60+
/**
61+
* Gets the configuration URL for a rule based on its rule class.
62+
*
63+
* @param {string} ruleClass - The rule class name
64+
* @param {string} ruleName - The rule name
65+
* @returns {string|null} The configuration URL with rule name parameter, or null if unsupported
66+
*/
67+
function getRuleConfigurationUrl(ruleClass, ruleName) {
68+
// Map rule classes to their configuration pages
69+
const ruleClassToUrl = {
70+
'CommandEvaluator': '/sso/v1/zerotrust/rules/config/forbidden_commands_rule',
71+
'AllowedCommandsRule': '/sso/v1/zerotrust/rules/config/allowed_commands_rule',
72+
'PluggableRuleEvaluator': '/sso/v1/zerotrust/rules/config/pluggable_rule'
73+
};
74+
75+
// Find the matching configuration URL using Array.find() for idiomatic JavaScript
76+
const matchingKey = Object.keys(ruleClassToUrl).find(key => ruleClass.includes(key));
77+
78+
if (matchingKey) {
79+
return ruleClassToUrl[matchingKey] + "?ruleName=" + encodeURIComponent(ruleName);
80+
}
81+
82+
return null;
83+
}
84+
85+
/**
86+
* Edits an existing rule by redirecting to the appropriate configuration page.
87+
* Fetches the rule details from the API and determines the correct configuration
88+
* page based on the rule class.
89+
*
90+
* @param {number} ruleId - The ID of the rule to edit
91+
*/
92+
function editRule(ruleId) {
93+
console.log("Editing rule:", ruleId);
94+
95+
// Fetch rule details to determine the rule class
96+
fetchRule(ruleId).then((rule) => {
97+
console.log("Fetched rule for editing:", rule);
98+
99+
const url = getRuleConfigurationUrl(rule.ruleClass, rule.ruleName);
100+
101+
if (url) {
102+
// Redirect to the configuration page
103+
window.location.href = url;
104+
} else {
105+
// Handle unsupported rule types
106+
alert("This rule type does not support editing through the UI.");
107+
}
108+
}).catch((error) => {
109+
console.error("Error fetching rule:", error);
110+
alert("Failed to load rule details for editing.");
111+
});
112+
}
113+
60114
function deleteRule(ruleId) {
61115
const csrfToken = document.getElementById("assignCsrf").value;
62116
var url = '/api/v1/zerotrust/rules/delete/' + ruleId
@@ -110,7 +164,7 @@ $(document).ready(function () {
110164

111165
if (row.canEdit) {
112166
buttons +=
113-
`<button class="btn btn-secondary spacer spacer-middle" data-bs-toggle="modal" data-bs-target="#edit_dialog_${data}" onclick="editRule(${data})">Edit</button><button id="role_btn_${data}" onclick="assignRule(${data})" class="btn btn-secondary assign_btn spacer spacer-right">Assign Host Enclaves</button>
167+
`<button class="btn btn-secondary spacer spacer-middle" onclick="editRule(${data})">Edit</button><button id="role_btn_${data}" onclick="assignRule(${data})" class="btn btn-secondary assign_btn spacer spacer-right">Assign Host Enclaves</button>
114168
`;
115169
}
116170

@@ -184,17 +238,12 @@ $(document).ready(function () {
184238
// Get the value from the ruleName input field
185239
const ruleName = document.getElementById('ruleName').value;
186240

187-
188-
// Construct the URL based on the selected rule class and rule name
189-
let url = "";
190-
if (ruleClass.includes("CommandEvaluator")) {
191-
url = "/sso/v1/zerotrust/rules/config/forbidden_commands_rule?ruleName=" + encodeURIComponent(ruleName);
192-
} else if (ruleClass.includes("AllowedCommandsRule")) {
193-
url = "/sso/v1/zerotrust/rules/config/allowed_commands_rule?ruleName=" + encodeURIComponent(ruleName);
194-
} else if (ruleClass.includes("PluggableRuleEvaluator")) {
195-
// Redirect to pluggable rule configuration form
196-
url = "/sso/v1/zerotrust/rules/config/pluggable_rule?ruleName=" + encodeURIComponent(ruleName);
197-
window.location.href = url;
241+
// Try to get standard configuration URL
242+
const configUrl = getRuleConfigurationUrl(ruleClass, ruleName);
243+
244+
if (configUrl) {
245+
// Redirect to the configuration page for standard rule types
246+
window.location.href = configUrl;
198247
return;
199248
} else if (ruleClass.includes("DeletePrevention")) {
200249

@@ -244,15 +293,9 @@ $(document).ready(function () {
244293
})();
245294
return;
246295
}
247-
248-
// Redirect to the constructed URL if ruleName and ruleClass are valid
249-
if (ruleClass && ruleName) {
250-
window.location.href = url;
251-
} else {
252-
alert("Please select a Rule Class and enter a Rule Name.");
253-
}
254296
});
255297
});
256298

257299
window.assignRule = assignRule;
258-
window.deleteRule = deleteRule;
300+
window.deleteRule = deleteRule;
301+
window.editRule = editRule;

api/src/main/resources/templates/fragments/header.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
<script th:src="@{/node/js/xterm-addon-search.js}"></script>
5858
<script th:src="@{/node/js/bootstrap.min.js}"></script>
5959
<script th:src="@{/node/js/gridstack/gridstack-all.js}"></script>
60-
<script th:src="@{/node/js/chart.js/chart.js}"></script>
6160
<script th:src="@{/node/js/chart.js/chart.umd.js}"></script>
6261
<!--
6362
<script th:src="@{/node/js/tags.js}">

0 commit comments

Comments
 (0)