lmc/user/user.js

200 lines
5.9 KiB
JavaScript
Raw Permalink 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, md5, parseParams, setupHeader, timeString } from "/global.js";
2020-03-13 23:01:39 -04:00
(function()
{
// Element names specific to this page
elements.info = "info";
elements.lmcRow = "lmc-tr1";
elements.lmcRowAlt = "lmc-tr2";
elements.loading = "loading";
elements.navlinkActive = "navlink-active";
elements.profileImg = "profile-img";
elements.userTable = "user-table";
// Data received from API calls
var data = {};
data.logins = [];
2020-03-13 23:01:39 -04:00
data.users = [];
// Static references to UI elements
var ui = {};
ui.loading = {};
ui.userTable = {};
// Temporary state
var state = {};
state.haveLogins = false;
state.haveUsers = false;
2020-03-13 23:01:39 -04:00
// Generates a user table row
var createUserRow = function(user, alt)
{
var row = document.createElement("tr");
if (alt)
row.className = elements.lmcRowAlt;
else
row.className = elements.lmcRow;
var nameCell = document.createElement("td");
var imgLink = document.createElement("a");
imgLink.href = "/user/edit?user=" + user.username;
var img = document.createElement("img");
img.className = elements.profileImg;
img.src = "https://www.gravatar.com/avatar/" + md5(user.email, false);
2020-03-13 23:01:39 -04:00
img.alt = user.username;
imgLink.appendChild(img);
var separator = document.createElement("span");
separator.innerHTML = " ";
var nameLink = document.createElement("a");
nameLink.href = "/user/edit?user=" + user.username;
nameLink.innerHTML = user.username;
nameCell.appendChild(imgLink);
nameCell.appendChild(separator);
nameCell.appendChild(nameLink);
row.appendChild(nameCell);
var lastLogin = document.createElement("td");
var loginDate = null;
for (var i = data.logins.length - 1; i >= 0; i--) {
if (data.logins[i].username == user.username) {
loginDate = new Date(data.logins[i].datetime + "Z");
break;
}
}
if (loginDate) {
var now = new Date();
var ago = document.createElement("span");
ago.innerHTML = timeString(now - loginDate, true);
lastLogin.appendChild(ago);
var br = document.createElement("br");
lastLogin.appendChild(br);
var dateString = document.createElement("span");
dateString.className = elements.info;
dateString.innerHTML = loginDate.toLocaleString();
lastLogin.appendChild(dateString);
} else {
lastLogin.innerHTML = "Never";
}
row.appendChild(lastLogin);
2020-03-13 23:01:39 -04:00
var restricted = document.createElement("td");
if (user.restricted) {
var yes = document.createElement("span");
yes.innerHTML = "Yes (";
var permsLink = document.createElement("a");
permsLink.href = "/user/grants?user=" + user.username;
permsLink.innerHTML = "edit permissions";
var close = document.createElement("span");
close.innerHTML = ")";
restricted.appendChild(yes);
restricted.appendChild(permsLink);
restricted.appendChild(close);
} else {
restricted.innerHTML = "No";
}
row.appendChild(restricted);
var options = document.createElement("td");
var editLink = document.createElement("a");
editLink.href = "/user/edit?user=" + user.username;
editLink.innerHTML = "Edit";
var pipe = document.createElement("span");
pipe.innerHTML = " | ";
var removeLink = document.createElement("a");
removeLink.href = "/user/remove?user=" + user.username;
removeLink.innerHTML = "Remove";
options.appendChild(editLink);
options.appendChild(pipe);
options.appendChild(removeLink);
row.appendChild(options);
return row;
};
// Callback for logins API call
var displayLogins = function(response)
{
data.logins = data.logins.concat(response.data);
// Request the next page if there are more
if (response.page != response.pages) {
apiGet("/account/logins?page=" + (response.page + 1), displayLogins, null);
return;
}
state.haveLogins = true;
if (state.haveUsers) {
ui.loading.remove();
// Add users to table
for (var i = 0; i < data.users.length; i++)
ui.userTable.appendChild(createUserRow(data.users[i], ui.userTable.children.length % 2));
}
};
2020-03-13 23:01:39 -04:00
// Callback for users API call
var displayUsers = function(response)
{
data.users = data.users.concat(response.data);
// Request the next page if there are more
if (response.page != response.pages) {
apiGet("/account/users?page=" + (response.page + 1), displayUsers, null);
return;
}
state.haveUsers = true;
if (state.haveLogins) {
ui.loading.remove();
2020-03-13 23:01:39 -04:00
// Add users to table
for (var i = 0; i < data.users.length; i++)
ui.userTable.appendChild(createUserRow(data.users[i], ui.userTable.children.length % 2));
}
2020-03-13 23:01:39 -04:00
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
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.loading = document.getElementById(elements.loading);
ui.userTable = document.getElementById(elements.userTable);
// Get data from API
apiGet("/account/users", displayUsers, null);
apiGet("/account/logins", displayLogins, null);
2020-03-13 23:01:39 -04:00
};
// Attach onload handler
window.addEventListener("load", setup);
})();