|
| 1 | +function checkAccessForm(){ |
| 2 | + const allowListArr = getEmailsList($("#allowed-email-list")); |
| 3 | + |
| 4 | + if(!$("#benchmark").val()){ |
| 5 | + showErrorToast("Make sure that you've selected a benchmark"); |
| 6 | + return false; |
| 7 | + } |
| 8 | + |
| 9 | + if(!allowListArr.length){ |
| 10 | + showErrorToast("Make sure that the email allow list is not empty"); |
| 11 | + return false; |
| 12 | + } |
| 13 | + |
| 14 | + return true; |
| 15 | +} |
| 16 | + |
| 17 | +function checkAutoAccessForm(){ |
| 18 | + const allowListArr = getEmailsList($("#allowed-email-list-auto")); |
| 19 | + |
| 20 | + if(!$("#benchmark-auto").val()){ |
| 21 | + showErrorToast("Make sure that you've selected a benchmark"); |
| 22 | + return false; |
| 23 | + } |
| 24 | + |
| 25 | + const interval = $("#interval-auto").val(); |
| 26 | + |
| 27 | + if(!interval || interval < 5 || interval > 60){ |
| 28 | + showErrorToast("Make sure that the time interval is between 5 and 60 (inclusive)"); |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + if(!allowListArr.length){ |
| 33 | + showErrorToast("Make sure that the email allow list is not empty"); |
| 34 | + return false; |
| 35 | + } |
| 36 | + |
| 37 | + return true; |
| 38 | +} |
| 39 | + |
| 40 | +function onGrantAccessSuccess(response){ |
| 41 | + if(response.status === "success"){ |
| 42 | + showReloadModal({ |
| 43 | + title: "Successfully Granted Access to the Selected Users", |
| 44 | + seconds: 3, |
| 45 | + }); |
| 46 | + } |
| 47 | + else{ |
| 48 | + showErrorModal("Failed to Grant Access", response); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +async function grantAccess(grantBtn){ |
| 53 | + addSpinner(grantBtn); |
| 54 | + |
| 55 | + disableElements(".card button, .card input, .card select"); |
| 56 | + |
| 57 | + const allowListArr = getEmailsList($("#allowed-email-list")); |
| 58 | + const formData = new FormData(); |
| 59 | + |
| 60 | + formData.append("benchmark_id", $("#benchmark").val()); |
| 61 | + formData.append("model_id", grantBtn.getAttribute("data-model-id")); |
| 62 | + formData.append("emails", allowListArr.join(" ")); |
| 63 | + |
| 64 | + ajaxRequest( |
| 65 | + `/models/grant_access`, |
| 66 | + "POST", |
| 67 | + formData, |
| 68 | + onGrantAccessSuccess, |
| 69 | + "Failed to grant access" |
| 70 | + ); |
| 71 | + |
| 72 | + showPanel(`Granting Access...`); |
| 73 | + window.runningTaskId = await getTaskId(); |
| 74 | + streamEvents(logPanel, stagesList, currentStageElement); |
| 75 | +} |
| 76 | + |
| 77 | +function startAutoGrant(startBtn){ |
| 78 | + disableElements(".card button, .card input, .card select"); |
| 79 | + |
| 80 | + const allowListArr = getEmailsList($("#allowed-email-list-auto")); |
| 81 | + const formData = new FormData(); |
| 82 | + |
| 83 | + formData.append("benchmark_id", $("#benchmark-auto").val()); |
| 84 | + formData.append("model_id", startBtn.getAttribute("data-model-id")); |
| 85 | + formData.append("interval", $("#interval-auto").val()); |
| 86 | + formData.append("emails", allowListArr.join(" ")); |
| 87 | + |
| 88 | + ajaxRequest( |
| 89 | + `/models/start_auto_access`, |
| 90 | + "POST", |
| 91 | + formData, |
| 92 | + (response) => { |
| 93 | + if(response.status === "success"){ |
| 94 | + showReloadModal({ |
| 95 | + title: "Successfully Started Auto Grant Access", |
| 96 | + seconds: 2, |
| 97 | + }); |
| 98 | + } |
| 99 | + else{ |
| 100 | + showErrorModal("Failed to Start Auto Grant Access", response); |
| 101 | + } |
| 102 | + }, |
| 103 | + "Failed to start auto grant access" |
| 104 | + ); |
| 105 | +} |
| 106 | + |
| 107 | +async function stopAutoGrant(stopBtn){ |
| 108 | + disableElements(".card button, .card input, .card select"); |
| 109 | + |
| 110 | + const formData = new FormData(); |
| 111 | + |
| 112 | + formData.append("model_id", stopBtn.getAttribute("data-model-id")); |
| 113 | + |
| 114 | + ajaxRequest( |
| 115 | + `/models/stop_auto_access`, |
| 116 | + "POST", |
| 117 | + formData, |
| 118 | + (response) => { |
| 119 | + if(response.status === "success"){ |
| 120 | + showReloadModal({ |
| 121 | + title: "Successfully Stopped Auto Grant Access", |
| 122 | + seconds: 2, |
| 123 | + }); |
| 124 | + } |
| 125 | + else{ |
| 126 | + showErrorModal("Failed to Stop Auto Grant Access", response); |
| 127 | + } |
| 128 | + }, |
| 129 | + "Failed to stop auto grant access" |
| 130 | + ); |
| 131 | +} |
| 132 | + |
| 133 | +function onRevokeUserAccessSuccess(response){ |
| 134 | + if(response.status === "success"){ |
| 135 | + showReloadModal({ |
| 136 | + title: "Successfully Revoked User Access", |
| 137 | + seconds: 3, |
| 138 | + }); |
| 139 | + } |
| 140 | + else{ |
| 141 | + showErrorModal("Failed to Revoke User Access", response); |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +async function revokeUserAccess(revokeAccessBtn){ |
| 146 | + addSpinner(revokeAccessBtn); |
| 147 | + |
| 148 | + disableElements(".card button, .card input, .card select"); |
| 149 | + |
| 150 | + const formData = new FormData(); |
| 151 | + |
| 152 | + formData.append("model_id", revokeAccessBtn.getAttribute("data-model-id")); |
| 153 | + formData.append("key_id", revokeAccessBtn.getAttribute("data-key-id")); |
| 154 | + |
| 155 | + ajaxRequest( |
| 156 | + `/models/revoke_user_access`, |
| 157 | + "POST", |
| 158 | + formData, |
| 159 | + onRevokeUserAccessSuccess, |
| 160 | + "Failed to revoke user access" |
| 161 | + ); |
| 162 | + |
| 163 | + window.runningTaskId = await getTaskId(); |
| 164 | + streamEvents(logPanel, stagesList, currentStageElement); |
| 165 | +} |
| 166 | + |
| 167 | +function onDeleteKeysSuccess(response){ |
| 168 | + if(response.status === "success"){ |
| 169 | + showReloadModal({ |
| 170 | + title: "Successfully Deleted Keys", |
| 171 | + seconds: 3, |
| 172 | + }); |
| 173 | + } |
| 174 | + else{ |
| 175 | + showErrorModal("Failed to Delete Keys", response); |
| 176 | + } |
| 177 | +} |
| 178 | + |
| 179 | +async function deleteKeys(deleteKeysBtn){ |
| 180 | + addSpinner(deleteKeysBtn); |
| 181 | + |
| 182 | + disableElements(".card button, .card input, .card select"); |
| 183 | + |
| 184 | + const formData = new FormData(); |
| 185 | + |
| 186 | + formData.append("model_id", deleteKeysBtn.getAttribute("data-model-id")); |
| 187 | + |
| 188 | + ajaxRequest( |
| 189 | + `/models/delete_keys`, |
| 190 | + "POST", |
| 191 | + formData, |
| 192 | + onDeleteKeysSuccess, |
| 193 | + "Failed to delete keys" |
| 194 | + ); |
| 195 | + |
| 196 | + window.runningTaskId = await getTaskId(); |
| 197 | + streamEvents(logPanel, stagesList, currentStageElement); |
| 198 | +} |
| 199 | + |
| 200 | +function showErrorToast(message){ |
| 201 | + showToast("Validation Error", message, "bg-danger text-light"); |
| 202 | +} |
| 203 | + |
| 204 | +function isValidEmail(email) { |
| 205 | + return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email); |
| 206 | +} |
| 207 | + |
| 208 | +function createEmailChip(email, input_element) { |
| 209 | + const $chip = $("<div class='email-chip'></div>").text(email); |
| 210 | + const $remove = $("<span class='remove-btn'>×</span>"); |
| 211 | + |
| 212 | + $remove.on("click", function () { |
| 213 | + $chip.remove(); |
| 214 | + }); |
| 215 | + |
| 216 | + $chip.append($remove); |
| 217 | + input_element.before($chip); |
| 218 | +} |
| 219 | + |
| 220 | +function parseEmails(element){ |
| 221 | + if (!$(element).length || !$(element)) |
| 222 | + return; |
| 223 | + const jsonList = JSON.parse(element.attr("data-allowed-list")); |
| 224 | + const input_element = element.find("input"); |
| 225 | + if(jsonList.length){ |
| 226 | + jsonList.forEach(email => { |
| 227 | + createEmailChip(email, input_element); |
| 228 | + }); |
| 229 | + } |
| 230 | +} |
| 231 | + |
| 232 | +function getEmailsList(element){ |
| 233 | + const emailsArr = []; |
| 234 | + element.find(".email-chip").each(function() { |
| 235 | + const span = $(this).find("span"); |
| 236 | + span.remove(); |
| 237 | + emailsArr.push($(this).text().trim()); |
| 238 | + $(this).append(span); |
| 239 | + }); |
| 240 | + return emailsArr; |
| 241 | +} |
| 242 | + |
| 243 | + |
| 244 | +$(document).ready(() => { |
| 245 | + parseEmails($("#allowed-email-list")); |
| 246 | + |
| 247 | + $(".email-input").on("keydown", function (e) { |
| 248 | + if (e.key === "Enter" || e.key === " " || e.key === ",") { |
| 249 | + e.preventDefault(); |
| 250 | + let email = $(this).val().trim().replace(/,$/, ""); |
| 251 | + if (email && isValidEmail(email)) { |
| 252 | + createEmailChip(email, $(this)); |
| 253 | + $(this).val(""); |
| 254 | + } |
| 255 | + } |
| 256 | + }); |
| 257 | + |
| 258 | + $(".email-input").on("paste", function (e) { |
| 259 | + e.preventDefault(); |
| 260 | + let clipboardData = (e.originalEvent || e).clipboardData.getData("text"); |
| 261 | + let rawEmails = clipboardData.split(/[\s,]+/); |
| 262 | + rawEmails.forEach(email => { |
| 263 | + email = email.trim(); |
| 264 | + if (email && isValidEmail(email)) { |
| 265 | + createEmailChip(email, $(this)); |
| 266 | + } |
| 267 | + }); |
| 268 | + $(this).val(""); |
| 269 | + }); |
| 270 | + |
| 271 | + $("#grant-access-btn").on("click", (e) => { |
| 272 | + if(checkAccessForm()){ |
| 273 | + showConfirmModal(e.currentTarget, grantAccess, "grant access to the emails added?"); |
| 274 | + } |
| 275 | + }); |
| 276 | + |
| 277 | + $("#start-auto-access-btn").on("click", (e) => { |
| 278 | + if(checkAutoAccessForm()){ |
| 279 | + showConfirmModal(e.currentTarget, startAutoGrant, "start automatic grant access for the selected benchmark?"); |
| 280 | + } |
| 281 | + }); |
| 282 | + |
| 283 | + $("#stop-auto-access-btn").on("click", (e) => { |
| 284 | + showConfirmModal(e.currentTarget, stopAutoGrant, "stop automatic grant access?"); |
| 285 | + }); |
| 286 | + |
| 287 | + $(".revoke-access-btn").on("click", (e) => { |
| 288 | + showConfirmModal(e.currentTarget, revokeUserAccess, "revoke access for the selected user?"); |
| 289 | + }); |
| 290 | + |
| 291 | + $("#delete-keys-btn").on("click", (e) => { |
| 292 | + showConfirmModal(e.currentTarget, deleteKeys, "delete all keys?"); |
| 293 | + }); |
| 294 | +}); |
0 commit comments