Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
284 lines (231 loc) · 9 KB
/
Copy pathscript.js
File metadata and controls
284 lines (231 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
function validateForm() {
var mainID = document.getElementById("mainID").value.toLowerCase();;
var mainDescription = document.getElementById("mainDescription").value;
var domain = document.getElementById("domain").value;
var scope = document.getElementById("scope").value;
if (mainID == "") {
alert("ID is required");
return false;
}
if (mainDescription == "") {
alert("Control Description is required");
return false;
}
if (domain == "") {
alert("Domain is required");
return false;
}
if (scope == "") {
alert("Scope is required");
return false;
}
return true;
}
function showData() {
var controlList;
if (localStorage.getItem("controlList") == null) {
controlList = [{
mainID: "0001",
mainDescription: "Default Control 1: Implement access controls to ensure that only authorized individuals or systems have access to resources, data, and systems.",
domain: "Identity and Access Management (IAM)",
scope: "Yes",
},
{
mainID: "0002",
mainDescription: "Default Control 2: Regularly apply patches and updates to software, operating systems, and applications to address known vulnerabilities and reduce the risk of exploitation.",
domain: "Security Architecture and Engineering",
scope: "Yes",
},
{
mainID: "0003",
mainDescription: "Default Control 3: Enforce a blockchain-based immutable audit trail for all financial transactions.",
domain: "Security Operations",
scope: "No",
},
// Add more default data as needed
];
localStorage.setItem("controlList", JSON.stringify(controlList));
} else {
controlList = JSON.parse(localStorage.getItem("controlList"));
}
var html = "";
controlList.forEach(function (element, index) {
html += "<tr>";
html += "<td>" + element.mainID + "</td>";
html += "<td>" + element.mainDescription + "</td>";
html += "<td>" + element.domain + "</td>";
html += "<td>" + element.scope + "</td>";
html +=
'<td><button onclick="deleteData(' +
index +
')" class="btn btn-danger btn-sm">Delete</button><button onclick="updateData(' +
index +
')" class="btn btn-warning btn-sm m-2">Edit</button></td>';
html += "</tr>";
});
document.querySelector("#crudTable tbody").innerHTML = html;
}
// Use 'window.onload' instead of 'document.onload'
window.addEventListener("load", showData);
function AddData() {
if (validateForm()) {
var mainID = document.getElementById("mainID").value;
var mainDescription = document.getElementById("mainDescription").value;
var domain = document.getElementById("domain").value;
var scope = document.getElementById("scope").value;
var controlList;
if (localStorage.getItem("controlList") == null) {
controlList = [];
} else {
controlList = JSON.parse(localStorage.getItem("controlList"));
}
// Check for duplicate mainID (optional, consider server-side validation for critical applications)
var isDuplicate = false;
for (var i = 0; i < controlList.length; i++) {
if (controlList[i].mainID === mainID) {
isDuplicate = true;
break;
}
}
if (isDuplicate) {
alert("Error: Main Control ID already exists. Please enter a unique ID.");
} else {
controlList.push({
mainID: mainID,
mainDescription: mainDescription,
domain: domain,
scope: scope,
});
localStorage.setItem("controlList", JSON.stringify(controlList));
showData();
// Clear form data after successful submission
document.getElementById("mainID").value = "";
document.getElementById("mainDescription").value = "";
document.getElementById("domain").value = "";
document.getElementById("scope").value = "";
// Display the success alert
alert("Control added successfully!");
}
}
}
function deleteData(index) {
var controlList;
if (localStorage.getItem("controlList") === null) {
controlList = [];
} else {
controlList = JSON.parse(localStorage.getItem("controlList"));
}
// Confirm deletion before proceeding
if (confirm("Are you sure you want to delete this control?")) {
controlList.splice(index, 1);
localStorage.setItem("controlList", JSON.stringify(controlList));
showData();
}
}
function updateData(index) {
document.getElementById("Add Control").style.display = "none";
document.getElementById("Update").style.display = "block";
var highlightedRow = document.querySelector(".edit-highlight");
if (highlightedRow) {
highlightedRow.classList.remove("edit-highlight");
}
// Add highlight to the currently edited row
var rows = document.querySelectorAll("#crudTable tbody tr");
rows[index].classList.add("edit-highlight");
// Select all delete buttons within the table body
var deleteButtons = document.querySelectorAll("#crudTable tbody .btn-danger");
// Disable all delete buttons
deleteButtons.forEach(function (button) {
button.disabled = true;
});
// Confirmation dialog instead of alert
if (confirm("Are you sure you want to update this control?")) {
// Proceed with update logic
var controlList;
if (localStorage.getItem("controlList") === null) {
controlList = [];
} else {
controlList = JSON.parse(localStorage.getItem("controlList"));
}
document.getElementById("mainID").value = controlList[index].mainID;
document.getElementById("mainDescription").value = controlList[index].mainDescription;
document.getElementById("domain").value = controlList[index].domain;
document.getElementById("scope").value = controlList[index].scope;
document.getElementById("Update").onclick = function () {
if (validateForm()) {
var updatedID = document.getElementById("mainID").value.toLowerCase(); // Convert to lowercase
var isDuplicate = controlList.some(function (item, idx) {
return idx !== index && item.mainID.toLowerCase() === updatedID; // Convert to lowercase
});
if (isDuplicate) {
alert("Error: Main Control ID already exists. Please enter a unique ID.");
} else {
controlList[index].mainID = updatedID;
controlList[index].mainDescription = document.getElementById("mainDescription").value;
controlList[index].domain = document.getElementById("domain").value;
controlList[index].scope = document.getElementById("scope").value;
localStorage.setItem("controlList", JSON.stringify(controlList));
showData();
document.getElementById("mainID").value = "";
document.getElementById("mainDescription").value = "";
document.getElementById("domain").value = "";
document.getElementById("scope").value = "";
document.getElementById("Add Control").style.display = "block";
document.getElementById("Update").style.display = "none";
// Enable all delete buttons after updating
deleteButtons.forEach(function (button) {
button.disabled = false;
});
// Display the success alert
alert("Control updated successfully!");
}
}
};
} else {
// User canceled the update
// Optionally, reset UI elements or display a message
// Enable all delete buttons after canceling
deleteButtons.forEach(function (button) {
button.disabled = false;
});
}
}
function downloadCSV() {
// Get the table element
var table = document.getElementById("crudTable");
// Create a string to hold the CSV data
var csvData = "";
// Extract headers from the <thead> element
var headerRow = table.querySelector("thead tr");
if (headerRow) {
for (var i = 0; i < headerRow.cells.length; i++) {
csvData += headerRow.cells[i].textContent + ",";
}
csvData += "\n"; // Add a newline after headers
} else {
console.warn("Warning: Table doesn't contain a header row.");
}
// Loop through all table rows within <tbody> and add data to CSV string
var tableBody = table.querySelector("tbody");
for (var i = 0; i < tableBody.rows.length; i++) {
var row = tableBody.rows[i];
for (var j = 0; j < row.cells.length; j++) {
var cellData = row.cells[j].textContent;
// Escape commas within cell data
cellData = cellData.replace(/,/g, " ");
csvData += cellData + ",";
}
csvData += "\n"; // Add newline after each row
}
// Create a Blob object with the CSV data
var blob = new Blob([csvData], { type: 'text/csv;charset=utf-8' });
// Create a downloadable link
var link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = "Security_Control_Inventory.csv";
link.click();
// Release the object URL (optional)
setTimeout(function() {
URL.revokeObjectURL(link.href);
}, 1000);
}