forked from jenkinsci/matrix-auth-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.js
More file actions
265 lines (234 loc) · 10.4 KB
/
table.js
File metadata and controls
265 lines (234 loc) · 10.4 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
/* global Behaviour, dialog, FormChecker, findElementsBySelector */
function matrixAuthEscapeHtml(html) {
return html.replace(/'/g, "'").replace(/"/g, """).replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
}
/*
* This handles the addition of new users/groups to the list.
*/
Behaviour.specify(".matrix-auth-add-button", "GlobalMatrixAuthorizationStrategy", 0, function (e) {
e.onclick = function (e) {
const dataReference = e.target;
const master = document.getElementById(dataReference.getAttribute("data-table-id"));
const table = master.parentNode;
const type = dataReference.getAttribute("data-type");
const typeLabel = dataReference.getAttribute("data-type-label");
dialog
.prompt(dataReference.getAttribute("data-message-title"), {
message: dataReference.getAttribute("data-message-prompt"),
})
.then(
(name) => {
if (
findElementsBySelector(table, "TR").find(function (n) {
return n.getAttribute("name") === "[" + type + ":" + name + "]";
}) != null
) {
dialog.alert(dataReference.getAttribute("data-message-error"));
return;
}
const copy = document.importNode(master, true);
copy.removeAttribute("id");
copy.removeAttribute("style");
copy.firstChild.innerHTML = matrixAuthEscapeHtml(name); // TODO consider setting innerText
copy.setAttribute("name", "[" + type + ":" + name + "]");
for (let child = copy.firstChild; child !== null; child = child.nextSibling) {
if (child.hasAttribute("data-permission-id")) {
child.setAttribute("data-tooltip-enabled", child.getAttribute("data-tooltip-enabled").replace("__SID__", name).replace("__TYPE__", typeLabel));
child.setAttribute("data-tooltip-disabled", child.getAttribute("data-tooltip-disabled").replace("__SID__", name).replace("__TYPE__", typeLabel));
}
}
const tooltipAttributeName = "data-html-tooltip";
findElementsBySelector(copy, ".stop a").forEach(function (item) {
// TODO Clean this up, `title` should be long obsolete.
let oldTitle = item.getAttribute("title");
if (oldTitle !== null) {
item.setAttribute("title", oldTitle.replace("__SID__", name).replace("__TYPE__", typeLabel));
}
item.setAttribute(tooltipAttributeName, item.getAttribute(tooltipAttributeName).replace("__SID__", name).replace("__TYPE__", typeLabel));
});
findElementsBySelector(copy, "input[type=checkbox]").forEach(function (item) {
const tooltip = item.nextSibling.getAttribute(tooltipAttributeName);
if (tooltip) {
item.nextSibling.setAttribute(tooltipAttributeName, tooltip.replace("__SID__", name).replace("__TYPE__", typeLabel));
} else {
// TODO Clean this up, `title` should be long obsolete.
item.nextSibling.setAttribute("title", item.getAttribute("title").replace("__SID__", name).replace("__TYPE__", typeLabel));
}
});
table.appendChild(copy);
Behaviour.applySubtree(table.closest("TABLE"), true);
},
() => {},
);
};
});
/*
* Behavior for the element removing a permission assignment row for a user/group
*/
Behaviour.specify(".global-matrix-authorization-strategy-table TD.stop A.remove", "GlobalMatrixAuthorizationStrategy", 0, function (e) {
e.onclick = function () {
// Run ambiguity warning removal code: If all ambiguous rows are deleted, the warning needs to go as well
// Order of operations: Find table ancestor, remove row, iterate over leftover rows
const table = this.closest("TABLE");
const tr = this.closest("TR");
tr.parentNode.removeChild(tr);
const tableRows = table.getElementsByTagName("tr");
let hasAmbiguousRows = false;
for (let i = 0; i < tableRows.length; i++) {
if (tableRows[i].getAttribute("name") !== null && tableRows[i].getAttribute("name").startsWith("[EITHER")) {
hasAmbiguousRows = true;
}
}
if (!hasAmbiguousRows) {
const alertElements = document.getElementsByClassName("alert");
for (let i = 0; i < alertElements.length; i++) {
if (alertElements[i].hasAttribute("data-table-id") && alertElements[i].getAttribute("data-table-id") === table.getAttribute("data-table-id")) {
alertElements[i].style.display = "none"; // TODO animate this?
}
}
}
return false;
};
});
/*
* Behavior for 'Select all' element that exists for each row of permissions checkboxes
*/
Behaviour.specify(".global-matrix-authorization-strategy-table TD.stop A.selectall", "GlobalMatrixAuthorizationStrategy", 0, function (e) {
e.onclick = function () {
const tr = this.closest("TR");
const inputs = tr.getElementsByTagName("INPUT");
for (let i = 0; i < inputs.length; i++) {
if (inputs[i].type === "checkbox") {
inputs[i].checked = true;
}
}
Behaviour.applySubtree(this.closest("TABLE"), true);
return false;
};
});
/*
* Behavior for 'Unselect all' element that exists for each row of permissions checkboxes
*/
Behaviour.specify(".global-matrix-authorization-strategy-table TD.stop A.unselectall", "GlobalMatrixAuthorizationStrategy", 0, function (e) {
e.onclick = function () {
const tr = this.closest("TR");
const inputs = tr.getElementsByTagName("INPUT");
for (let i = 0; i < inputs.length; i++) {
if (inputs[i].type === "checkbox") {
inputs[i].checked = false;
}
}
Behaviour.applySubtree(this.closest("TABLE"), true);
return false;
};
});
/*
* Behavior for 'Migrate to user' element that exists for each ambiguous row
*/
Behaviour.specify(".global-matrix-authorization-strategy-table TD.stop A.migrate", "GlobalMatrixAuthorizationStrategy", 0, function (e) {
e.onclick = function () {
const tr = this.closest("TR");
const name = tr.getAttribute("name");
let newName = name.replace("[EITHER:", "[USER:"); // migrate_user behavior
if (this.classList.contains("migrate_group")) {
newName = name.replace("[EITHER:", "[GROUP:");
}
const table = this.closest("TABLE");
const tableRows = table.getElementsByTagName("tr");
let newNameElement = null;
for (let i = 0; i < tableRows.length; i++) {
if (tableRows[i].getAttribute("name") === newName) {
newNameElement = tableRows[i];
break;
}
}
if (newNameElement === tr) {
// uh-oh, we shouldn't be able to find ourselves, so just do nothing
return false;
}
if (newNameElement == null) {
// no row for this name exists yet, so transform the ambiguous row to unambiguous
tr.setAttribute("name", newName);
tr.removeAttribute("data-checked");
// remove migration buttons from updated row
const buttonContainer = this.closest("DIV");
const migrateButtons = buttonContainer.getElementsByClassName("migrate");
for (let i = migrateButtons.length - 1; i >= 0; i--) {
buttonContainer.removeChild(migrateButtons[i]);
}
} else {
// there's already a row for the migrated name (unusual but OK), so merge them
// migrate permissions from this row
const ambiguousPermissionInputs = tr.getElementsByTagName("INPUT");
const unambiguousPermissionInputs = newNameElement.getElementsByTagName("INPUT");
for (let i = 0; i < ambiguousPermissionInputs.length; i++) {
if (ambiguousPermissionInputs[i].type === "checkbox") {
unambiguousPermissionInputs[i].checked |= ambiguousPermissionInputs[i].checked;
}
newNameElement.classList.add("highlight-entry");
}
// remove this row
tr.parentNode.removeChild(tr);
}
Behaviour.applySubtree(table, true);
let hasAmbiguousRows = false;
for (let i = 0; i < tableRows.length; i++) {
if (tableRows[i].getAttribute("name") !== null && tableRows[i].getAttribute("name").startsWith("[EITHER")) {
hasAmbiguousRows = true;
}
}
if (!hasAmbiguousRows) {
let alertElements = document.getElementsByClassName("alert");
for (let i = 0; i < alertElements.length; i++) {
if (alertElements[i].hasAttribute("data-table-id") && alertElements[i].getAttribute("data-table-id") === table.getAttribute("data-table-id")) {
alertElements[i].style.display = "none"; // TODO animate this?
}
}
}
return false;
};
});
/*
* Whenever permission assignments change, this ensures that implied permissions get their checkboxes disabled.
*/
Behaviour.specify(".global-matrix-authorization-strategy-table td input", "GlobalMatrixAuthorizationStrategy", 0, function (e) {
const table = e.closest("TABLE");
if (table.classList.contains("read-only")) {
// if this is a read-only UI (ExtendedRead / SystemRead), do not enable checkboxes
return;
}
const tooltipAttributeName = "data-html-tooltip";
const impliedByString = e.closest("TD").getAttribute("data-implied-by-list");
const impliedByList = impliedByString.split(" ");
const tr = e.closest("TR");
e.disabled = false;
let tooltip = matrixAuthEscapeHtml(e.closest("TD").getAttribute("data-tooltip-enabled"));
e.nextSibling.setAttribute(tooltipAttributeName, tooltip);
for (let i = 0; i < impliedByList.length; i++) {
let permissionId = impliedByList[i];
let reference = tr.querySelector("td[data-permission-id='" + permissionId + "'] input");
if (reference !== null) {
if (reference.checked) {
e.disabled = true;
let tooltip = matrixAuthEscapeHtml(e.closest("TD").getAttribute("data-tooltip-disabled"));
e.nextSibling.setAttribute(tooltipAttributeName, tooltip);
}
}
}
e.onchange = function () {
Behaviour.applySubtree(this.closest("TABLE"), true);
return true;
};
});
/*
* Each newly added row needs to have the name checked. Triggered by explicit Behaviour#applySubtree calls elsewhere.
*/
Behaviour.specify(".global-matrix-authorization-strategy-table TR.permission-row", "GlobalMatrixAuthorizationStrategy", 0, function (e) {
if (e.getAttribute("name") === "__unused__") {
return;
}
if (!e.hasAttribute("data-checked")) {
FormChecker.delayedCheck(e.getAttribute("data-descriptor-url") + "/checkName?value=" + encodeURIComponent(e.getAttribute("name")), "GET", e.firstChild);
e.setAttribute("data-checked", "true");
}
});