-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdashboard.js
90 lines (77 loc) · 3.08 KB
/
dashboard.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
87
88
89
90
function DASHBOARD_URL(originUrl, dashboardId) { return `${originUrl}/api/dashboards/${dashboardId}` };
function DASHBOARD_ACL_URL(originUrl, dashboardId) { return `${originUrl}/api/dashboards/${dashboardId}/acl` };
function QUERY_ACL_URL(originUrl, queryId) { return `${originUrl}/api/queries/${queryId}/acl` };
async function requestDashboardInfo(originUrl, dashboardId) {
const resp = await fetch(DASHBOARD_URL(originUrl, dashboardId));
return await resp.json();
}
async function requestDashboardAccessControlList(originUrl, dashboardId) {
const resp = await fetch(`${DASHBOARD_ACL_URL(originUrl, dashboardId)}`);
return await resp.json();
}
function filterWidgetsWithQueries(dashboardInfo) {
const queries = [];
for (const widget of dashboardInfo.widgets) {
const visualization = widget.visualization;
if (visualization) {
queries.push(visualization.query.id);
}
}
return queries;
}
async function grantAccess() {
const url = window.location.href;
const originUrl = new URL(url).origin;
const dashboardId = url.split('dashboards/')[1].split('-')[0];
const dashboardInfo = await requestDashboardInfo(originUrl, dashboardId);
const dashboardUsers = await requestDashboardAccessControlList(originUrl, dashboardId);
for (const userId of dashboardUsers['modify'].map(user => user.id)) {
for (const queryId of filterWidgetsWithQueries(dashboardInfo)) {
await fetch(`${QUERY_ACL_URL(originUrl, queryId)}`, {
method: 'POST',
body: JSON.stringify({ access_type: "modify", user_id: userId})
});
}
}
}
function insertButtonWhenModalOpened() {
const buttonId = "accessButton";
const observer = new MutationObserver((mutationsList) => {
for (let mutation of mutationsList) {
if (mutation.type === 'childList') {
mutation.addedNodes.forEach(node => {
if (node.nodeName !== "DIV") {
return;
}
const title = node.querySelector(".ant-modal-title");
if (
!title ||
!title.childNodes[0] ||
!title.childNodes[1].innerText.includes('dashboard') ||
title.childNodes[0].textContent !== "Manage Permissions"
) {
return;
}
const parent = node.querySelector(".ant-modal-body");
if (!parent || parent.querySelector(`#${buttonId}`)) {
return;
}
const container = document.createElement("div");
container.id = `${buttonId}`;
container.style.display = "flex";
container.style.alignItems = "center";
const button = document.createElement("button");
button.className = "btn btn-sm hidden-xs btn-default";
button.style.marginRight = "8px";
button.textContent = "Grant access to all queries";
button.addEventListener("click", () => grantAccess());
container.appendChild(button);
parent.appendChild(container);
});
}
}
});
const config = { childList: true, subtree: true };
observer.observe(document.body, config);
}
insertButtonWhenModalOpened();