lmc/user/grants/grants.js

277 lines
8.3 KiB
JavaScript
Raw Normal View History

2020-03-13 23:01:39 -04:00
/*
* This file is part of Linode Manager Classic.
*
* Linode Manager Classic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Linode Manager Classic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Linode Manager Classic. If not, see <https://www.gnu.org/licenses/>.
*/
import { settings, elements, apiGet, apiPut, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.billingNone = "billing-none";
elements.billingRO = "billing-ro";
elements.billingRW = "billing-rw";
elements.createDomain = "create-domain";
elements.createImage = "create-image";
elements.createLinode = "create-linode";
elements.createLV = "create-lv";
elements.createNB = "create-nb";
elements.createSS = "create-ss";
elements.createVolume = "create-volume";
elements.grantBox = "grant-box";
elements.grantTables = {};
elements.grantTables.domain = "dns-grants";
elements.grantTables.image = "image-grants";
elements.grantTables.linode = "linode-grants";
elements.grantTables.longview = "lv-grants";
elements.grantTables.nodebalancer = "nb-grants";
elements.grantTables.stackscript = "ss-grants";
elements.grantTables.volume = "volume-grants";
elements.modifyLV = "modify-lv";
elements.topUserLink = "top-user-link";
2020-03-13 23:01:39 -04:00
elements.updateButton = "update-button";
// Data received from API calls
var data = {};
// Static references to UI elements
var ui = {};
ui.billingNone = {};
ui.billingRO = {};
ui.billingRW = {};
ui.createDomain = {};
ui.createImage = {};
ui.createLinode = {};
ui.createLV = {};
ui.createNB = {};
ui.createSS = {};
ui.createVolume = {};
ui.grantTables = {};
ui.grantTables.domain = {};
ui.grantTables.image = {};
ui.grantTables.linode = {};
ui.grantTables.longview = {};
ui.grantTables.nodebalancer = {};
ui.grantTables.stackscript = {};
ui.grantTables.volume = {};
ui.modifyLV = {};
ui.topUserLink = {};
2020-03-13 23:01:39 -04:00
ui.updateButton = {};
// Creates a row for a grant table
var createGrantRow = function(type, perm)
{
var row = document.createElement("tr");
var label = document.createElement("td");
label.innerHTML = perm.label;
row.appendChild(label);
var none = document.createElement("td");
var noneLabel = document.createElement("label");
2020-03-14 00:33:19 -04:00
noneLabel.htmlFor = type + "-none-" + perm.id;
2020-03-13 23:01:39 -04:00
var noneBox = document.createElement("input");
noneBox.id = type + "-none-" + perm.id;
noneBox.className = elements.grantBox;
noneBox.type = "radio";
noneBox.name = type + "-" + perm.id;
noneLabel.appendChild(noneBox);
none.appendChild(noneLabel);
row.appendChild(none);
var ro = document.createElement("td");
var roLabel = document.createElement("label");
2020-03-14 00:33:19 -04:00
roLabel.htmlFor = type + "-ro-" + perm.id;
2020-03-13 23:01:39 -04:00
var roBox = document.createElement("input");
roBox.id = type + "-ro-" + perm.id;
roBox.className = elements.grantBox;
roBox.type = "radio";
roBox.name = type + "-" + perm.id;
roLabel.appendChild(roBox);
ro.appendChild(roLabel);
row.appendChild(ro);
var rw = document.createElement("td");
var rwLabel = document.createElement("label");
2020-03-14 00:33:19 -04:00
rwLabel.htmlFor = type + "-rw-" + perm.id;
2020-03-13 23:01:39 -04:00
var rwBox = document.createElement("input");
rwBox.id = type + "-rw-" + perm.id;
rwBox.className = elements.grantBox;
rwBox.type = "radio";
rwBox.name = type + "-" + perm.id;
rwLabel.appendChild(rwBox);
rw.appendChild(rwLabel);
row.appendChild(rw);
switch (perm.permissions) {
case "read_only":
roBox.checked = true;
break;
case "read_write":
rwBox.checked = true;
break;
default:
noneBox.checked = true;
}
return row;
};
// Callback for user grants API call
var displayGrants = function(response)
{
// Set global grants
ui.createLinode.checked = response.global.add_linodes;
ui.createNB.checked = response.global.add_nodebalancers;
ui.createLV.checked = response.global.add_longview;
ui.modifyLV.checked = response.global.longview_subscription;
ui.createDomain.checked = response.global.add_domains;
ui.createSS.checked = response.global.add_stackscripts;
ui.createImage.checked = response.global.add_images;
ui.createVolume.checked = response.global.add_volumes;
// Billing grants
switch (response.global.account_access) {
case "read_only":
ui.billingRO.checked = true;
break;
case "read_write":
ui.billingRW.checked = true;
break;
default:
ui.billingNone.checked = true;
}
// Specific grants
for (var i in ui.grantTables) {
for (var j = 0; j < response[i].length; j++)
ui.grantTables[i].appendChild(createGrantRow(i, response[i][j]));
}
ui.updateButton.disabled = false;
};
// Click handler for update button
var handleUpdate = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {};
// Add in global permissions
req.global = {
"add_linodes": ui.createLinode.checked,
"add_nodebalancers": ui.createNB.checked,
"add_longview": ui.createLV.checked,
"longview_subscription": ui.modifyLV.checked,
"add_domains": ui.createDomain.checked,
"add_stackscripts": ui.createSS.checked,
"add_images": ui.createImage.checked,
"add_volumes": ui.createVolume.checked
};
// Add in billing permissions
if (ui.billingRO.checked)
req.global.account_access = "read_only";
else if (ui.billingRW.checked)
req.global.account_access = "read_write";
else
req.global.account_access = null;
// Add in specific persmissions
for (var i in ui.grantTables)
req[i] = [];
var grantBoxes = document.getElementsByClassName(elements.grantBox);
for (var i = 0; i < grantBoxes.length; i++) {
if (!grantBoxes[i].checked)
continue;
// This will give us an array tuple of [type, permission, id]
var id = grantBoxes[i].id.split("-");
var perm = {};
perm.id = parseInt(id[2]);
if (id[1] == "ro")
perm.permissions = "read_only";
else if (id[1] == "rw")
perm.permissions = "read_write";
else
perm.permissions = null;
req[id[0]].push(perm);
}
apiPut("/account/users/" + data.params.user + "/grants", req, function(response)
{
location.href = "/user";
});
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
// We need a username, so die if we don't have it
if (!data.params.user) {
alert("No username supplied!");
return;
}
setupHeader();
// Highlight the account nav link
var navlinks = document.getElementsByClassName(elements.navlink);
for (var i = 0; i < navlinks.length; i++) {
if (navlinks[i].pathname == "/account/")
navlinks[i].className = " " + elements.navlinkActive;
}
// Get element references
ui.billingNone = document.getElementById(elements.billingNone);
ui.billingRO = document.getElementById(elements.billingRO);
ui.billingRW = document.getElementById(elements.billingRW);
ui.createDomain = document.getElementById(elements.createDomain);
ui.createImage = document.getElementById(elements.createImage);
ui.createLinode = document.getElementById(elements.createLinode);
ui.createLV = document.getElementById(elements.createLV);
ui.createNB = document.getElementById(elements.createNB);
ui.createSS = document.getElementById(elements.createSS);
ui.createVolume = document.getElementById(elements.createVolume);
for (var i in ui.grantTables)
ui.grantTables[i] = document.getElementById(elements.grantTables[i]);
ui.modifyLV = document.getElementById(elements.modifyLV);
ui.topUserLink = document.getElementById(elements.topUserLink);
2020-03-13 23:01:39 -04:00
ui.updateButton = document.getElementById(elements.updateButton);
// Populate username where we need it
document.title += " - " + data.params.user;
ui.topUserLink.href = "/user/edit?user=" + data.params.user;
ui.topUserLink.innerHTML = data.params.user;
2020-03-13 23:01:39 -04:00
// Attach event handlers
ui.updateButton.addEventListener("click", handleUpdate);
// Get data from API
apiGet("/account/users/" + data.params.user + "/grants", displayGrants, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();