lmc/profile/api/add/add.js

192 lines
5.6 KiB
JavaScript

/*
* 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, apiPost, oauthScopes, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.addButton = "add-button";
elements.expiry = "expiry";
elements.label = "label";
elements.scopeBox = "scope-box";
elements.scopes = "scopes";
elements.selectNone = "select-none";
elements.selectRo = "select-read_only";
elements.selectRw = "select-read_write";
// Data received from API calls
var data = {};
// Static references to UI elements
var ui = {};
ui.addButton = {};
ui.expiry = {};
ui.label = {};
ui.scopes = {};
ui.selectNone = {};
ui.selectRo = {};
ui.selectRw = {};
// Display OAuth scopes in scope selector
var displayScopes = function()
{
for (var scope in oauthScopes) {
var row = document.createElement("tr");
var label = document.createElement("td");
label.innerHTML = oauthScopes[scope];
row.appendChild(label);
var none = document.createElement("td");
var noneLabel = document.createElement("label");
noneLabel.htmlFor = "scope-" + scope + "-none";
var noneBox = document.createElement("input");
noneBox.id = "scope-" + scope + "-none";
noneBox.className = elements.scopeBox;
noneBox.type = "radio";
noneBox.name = "scope-" + scope;
noneBox.checked = true;
noneBox.addEventListener("input", handleSelectScope);
noneLabel.appendChild(noneBox);
none.appendChild(noneLabel);
row.appendChild(none);
var ro = document.createElement("td");
var roLabel = document.createElement("label");
roLabel.htmlFor = "scope-" + scope + "-read_only";
var roBox = document.createElement("input");
roBox.id = "scope-" + scope + "-read_only";
roBox.className = elements.scopeBox;
roBox.type = "radio";
roBox.name = "scope-" + scope;
roBox.addEventListener("input", handleSelectScope);
roLabel.appendChild(roBox);
ro.appendChild(roLabel);
row.appendChild(ro);
var rw = document.createElement("td");
var rwLabel = document.createElement("label");
rwLabel.htmlFor = "scope-" + scope + "-read_write";
var rwBox = document.createElement("input");
rwBox.id = "scope-" + scope + "-read_write";
rwBox.className = elements.scopeBox;
rwBox.type = "radio";
rwBox.name = "scope-" + scope;
rwBox.addEventListener("input", handleSelectScope);
rwLabel.appendChild(rwBox);
rw.appendChild(rwLabel);
row.appendChild(rw);
ui.scopes.appendChild(row);
}
};
// Click handler for add button
var handleAdd = function(event)
{
var req = {
"label": ui.label.value
};
var expiry = parseInt(ui.expiry.value);
if (expiry) {
var expireDt = new Date(Date.now() + expiry).toISOString();
req.expiry = expireDt.slice(0, expireDt.indexOf("."));
}
if (ui.selectRw.checked) {
req.scopes = "*";
} else {
req.scopes = [];
var buttons = document.getElementsByClassName(elements.scopeBox);
for (var i = 0; i < buttons.length; i++) {
if (!buttons[i].checked)
continue;
var attrs = buttons[i].id.split("-");
if (attrs[2] == "none")
continue;
req.scopes.push(attrs[1] + ":" + attrs[2]);
}
req.scopes = req.scopes.join(" ");
if (!req.scopes.length) {
alert("You must select some permissions.");
return;
}
}
apiPost("/profile/tokens", req, function(response)
{
alert("Your API token has been created. Store this secret - it won't be shown again.\n" + response.token);
location.href = "/profile/api";
});
};
// Handler for the "select all" row
var handleSelectAll = function(event)
{
var column = event.currentTarget.id.split("-")[1];
var buttons = document.getElementsByClassName(elements.scopeBox);
for (var i = 0; i < buttons.length; i++) {
if (buttons[i].id.split("-")[2] == column)
buttons[i].checked = true;
}
};
// Handler for individual scope selectors
var handleSelectScope = function(event)
{
ui.selectNone.checked = false;
ui.selectRo.checked = false;
ui.selectRw.checked = false;
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
setupHeader();
// Get element references
ui.addButton = document.getElementById(elements.addButton);
ui.expiry = document.getElementById(elements.expiry);
ui.label = document.getElementById(elements.label);
ui.scopes = document.getElementById(elements.scopes);
ui.selectNone = document.getElementById(elements.selectNone);
ui.selectRo = document.getElementById(elements.selectRo);
ui.selectRw = document.getElementById(elements.selectRw);
// Add scopes to tables
displayScopes();
ui.selectNone.checked = false;
ui.selectRo.checked = false;
ui.selectRw.checked = false;
// Register event handlers
ui.addButton.addEventListener("click", handleAdd);
ui.selectNone.addEventListener("change", handleSelectAll);
ui.selectRo.addEventListener("change", handleSelectAll);
ui.selectRw.addEventListener("change", handleSelectAll);
};
// Attach onload handler
window.addEventListener("load", setup);
})();