-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmanagesids.js
86 lines (73 loc) · 2.86 KB
/
managesids.js
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
'use strict';
/**
* Assign a sid to a role.
*
* @param roleType {('agent' | 'global' | 'folder')} the type of the role
* @param index index of the role in its parent container
*/
function assignSid(roleType, index) {
if (!['agent', 'global', 'folder'].includes(roleType)) {
throw new Error('Unknown Role Type');
}
const formData = new FormData();
formData.append('sid', document.getElementById(`assign-sid-${roleType}-${index}`).value);
formData.append('roleName', document.getElementById(`${roleType}RoleContainer`).children[index].getAttribute('data-role-name'));
const url = `${rootURL}/folder-auth/assignSidTo${roleType[0].toUpperCase()}${roleType.substring(1)}Role`;
const request = new XMLHttpRequest();
request.open('POST', url);
request.onload = () => {
if (request.status === 200) {
alert('Sid added successfully.');
location.reload();
} else {
alert('Unable to assign sid to the role.' + request.responseText);
}
};
request.onerror = () => {
alert('Unable to add the sid to the role: ' + request.responseText);
};
// see addRole.js
request.setRequestHeader('Jenkins-Crumb', crumb.value);
request.send(formData);
}
/**
* Removes a sid from a role.
*
* @param roleType {('agent' | 'global' | 'folder')} the type of the role
* @param index index of the role in its parent container
*/
function removeSid(roleType, index) {
if (!['agent', 'global', 'folder'].includes(roleType)) {
throw new Error('Unknown Role Type');
}
const formData = new FormData();
formData.append('sid', document.getElementById(`assign-sid-${roleType}-${index}`).value);
formData.append('roleName', document.getElementById(`${roleType}RoleContainer`).children[index].getAttribute('data-role-name'));
const url = `${rootURL}/folder-auth/removeSidFrom${roleType[0].toUpperCase()}${roleType.substring(1)}Role`;
const request = new XMLHttpRequest();
request.open('POST', url);
request.onload = () => {
if (request.status === 200) {
alert('Sid removed successfully.');
location.reload();
} else {
alert('Unable to remove the sid.' + request.responseText);
}
};
request.onerror = () => {
alert('Unable to remove the sid from the role: ' + request.responseText);
};
// see addRole.js
request.setRequestHeader('Jenkins-Crumb', crumb.value);
request.send(formData);
}
document.addEventListener("DOMContentLoaded", () => {
document.querySelectorAll(".sid-action-button").forEach((button) => {
button.addEventListener("click", (event) => {
const data = event.target.dataset;
const index = parseInt(data.index);
const { roleType, action } = data;
window[action](roleType, index);
});
})
});