/* * Guacamole - Clientless Remote Desktop * Copyright (C) 2010 Michael Jumper * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program 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 Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ /** * General set of UI elements and UI-related functions regarding * administration. */ var GuacAdmin = { "containers" : { "connection_list" : document.getElementById("connection-list"), "user_list" : document.getElementById("user-list"), "user_list_buttons" : document.getElementById("user-list-buttons"), "connection_list_buttons" : document.getElementById("connection-list-buttons") }, "buttons" : { "back" : document.getElementById("back"), "logout" : document.getElementById("logout"), "add_connection" : document.getElementById("add-connection"), "add_user" : document.getElementById("add-user") }, "fields" : { "connection_id" : document.getElementById("connection-id"), "username" : document.getElementById("username") }, "cached_permissions" : null, "cached_protocols" : null, "cached_connections" : null, "selected_user" : null, "selected_connection" : null }; /** * An arbitrary input field. * * @constructor */ GuacAdmin.Field = function() { /** * Returns the DOM Element representing this field. * * @return {Element} The DOM Element representing this field. */ this.getElement = function() {}; /** * Returns the value of this field. * * @return {String} The value of this field. */ this.getValue = function() {}; /** * Sets the value of this field. * * @param {String} value The value of this field. */ this.setValue = function(value) {}; }; /** * Simple HTML input field. * * @augments GuacAdmin.Field * @param {String} type The type of HTML field. */ GuacAdmin.Field._HTML_INPUT = function(type) { // Call parent constructor GuacAdmin.Field.apply(this); // Create backing element var element = GuacUI.createElement("input"); element.setAttribute("type", type); this.getValue = function() { return element.value; }; this.getElement = function() { return element; }; this.setValue = function(value) { element.value = value; }; }; GuacAdmin.Field._HTML_INPUT.prototype = new GuacAdmin.Field(); /** * A basic text field. * * @augments GuacAdmin.Field._HTML_INPUT */ GuacAdmin.Field.TEXT = function() { GuacAdmin.Field._HTML_INPUT.apply(this, ["text"]); }; GuacAdmin.Field.TEXT.prototype = new GuacAdmin.Field._HTML_INPUT(); /** * A basic password field. * * @augments GuacAdmin.Field._HTML_INPUT */ GuacAdmin.Field.PASSWORD = function() { GuacAdmin.Field._HTML_INPUT.apply(this, ["password"]); }; GuacAdmin.Field.PASSWORD.prototype = new GuacAdmin.Field._HTML_INPUT(); /** * Simple checkbox. * * @augments GuacAdmin.Field */ GuacAdmin.Field.CHECKBOX = function(value) { // Call parent constructor GuacAdmin.Field.apply(this); // Create backing element var element = GuacUI.createElement("input"); element.setAttribute("type", "checkbox"); element.setAttribute("value", value); this.getValue = function() { if (element.checked) return value; else return ""; }; this.getElement = function() { return element; }; this.setValue = function(new_value) { if (new_value == value) element.checked = true; else element.checked = false; }; }; GuacAdmin.Field.CHECKBOX.prototype = new GuacAdmin.Field(); /** * Enumerated field type. * * @augments GuacAdmin.Field */ GuacAdmin.Field.ENUM = function(values) { // Call parent constructor GuacAdmin.Field.apply(this); // Create backing element var element = GuacUI.createElement("select"); for (var name in values) { var option = GuacUI.createChildElement(element, "option"); option.textContent = values[name]; option.value = name; } this.getValue = function() { return element.value; }; this.getElement = function() { return element; }; this.setValue = function(value) { element.value = value; }; }; GuacAdmin.Field.ENUM.prototype = new GuacAdmin.Field(); /** * An arbitrary button. * * @constructor * @param {String} title A human-readable title for the button. */ GuacAdmin.Button = function(title) { /** * A human-readable title describing this button. */ this.title = title; // Button element var element = GuacUI.createElement("button"); element.textContent = title; /** * Returns the DOM element associated with this button. */ this.getElement = function() { return element; }; }; /** * An arbitrary list item with an icon and caption. */ GuacAdmin.ListItem = function(type, title) { // Create connection display elements var element = GuacUI.createElement("div", "list-item"); var icon = GuacUI.createChildElement(element, "div", "icon"); var name = GuacUI.createChildElement(element, "span", "name"); GuacUI.addClass(icon, type); // Set name name.textContent = title; /** * Returns the DOM element representing this connection. */ this.getElement = function() { return element; }; }; /* * Set handler for logout */ GuacAdmin.buttons.logout.onclick = function() { window.location.href = "logout"; }; /* * Set handler for back button */ GuacAdmin.buttons.back.onclick = function() { window.location.href = "index.xhtml"; }; /** * Returns whether the given object has at least one property. */ GuacAdmin.hasEntry = function(object) { for (var name in object) return true; return false; }; /** * Given a Date, returns a formatted String. * * @param {Date} date The date tor format. * @return {String} A formatted String. */ GuacAdmin.formatDate = function(date) { var month = date.getMonth() + 1; var day = date.getDate(); var year = date.getFullYear(); var hour = date.getHours(); var minute = date.getMinutes(); var second = date.getSeconds(); return ("00" + month).slice(-2) + "/" + ("00" + day).slice(-2) + "/" + year + " " + ("00" + hour).slice(-2) + ":" + ("00" + minute).slice(-2) + ":" + ("00" + second).slice(-2); }; /** * Given a number of seconds, returns a String representing that length * of time in a human-readable format. * * @param {Number} seconds The number of seconds. * @return {String} A human-readable description of the duration specified. */ GuacAdmin.formatSeconds = function(seconds) { function round(value) { return Math.round(value * 10) / 10; } if (seconds < 60) return round(seconds) + " seconds"; if (seconds < 3600) return round(seconds / 60) + " minutes"; if (seconds < 86400) return round(seconds / 3600) + " hours"; return round(seconds / 86400) + " days"; }; /** * Currently-defined pager for users, if any. */ GuacAdmin.userPager = null; /** * Adds the user with the given name to the displayed user list. */ GuacAdmin.addUser = function(name) { // Create user list item var item = new GuacAdmin.ListItem("user", name); var item_element = item.getElement(); GuacAdmin.userPager.addElement(item_element); // When clicked, build and display property form item_element.onclick = function() { // Ignore clicks if any item is selected if (GuacAdmin.selected_user) return; else GuacAdmin.selected_user = name; // Get user permissions var user_perms = GuacamoleService.Permissions.list(name); // Permission deltas var added_perms = new GuacamoleService.PermissionSet(); var removed_perms = new GuacamoleService.PermissionSet(); // Create form base elements var form_element = GuacUI.createElement("div", "form"); var user_header = GuacUI.createChildElement(form_element, "h2"); var sections = GuacUI.createChildElement( GuacUI.createChildElement(form_element, "div", "settings section"), "dl"); var field_header = GuacUI.createChildElement(sections, "dt"); var field_table = GuacUI.createChildElement( GuacUI.createChildElement(sections, "dd"), "table", "fields section"); user_header.textContent = name; field_header.textContent = "Properties:"; // Deselect function deselect() { GuacUI.removeClass(GuacAdmin.containers.user_list, "disabled"); GuacUI.removeClass(item_element, "selected"); item_element.removeChild(form_element); GuacAdmin.selected_user = null; } // Select function select() { GuacUI.addClass(GuacAdmin.containers.user_list, "disabled"); GuacUI.addClass(item_element, "selected"); item_element.appendChild(form_element); } // Add password field var password_field = GuacUI.createChildElement( GuacUI.createTabulatedContainer(field_table, "Password:"), "input"); password_field.setAttribute("type", "password"); password_field.setAttribute("value", "password"); // Add password re-entry field var reenter_password_field = GuacUI.createChildElement( GuacUI.createTabulatedContainer(field_table, "Re-enter Password:"), "input"); reenter_password_field.setAttribute("type", "password"); reenter_password_field.setAttribute("value", "password"); // Update password if changed var password_modified = false; password_field.onchange = reenter_password_field.onchange = function() { password_modified = true; }; // If administrator, allow manipulation of admin permissions on users if (GuacAdmin.cached_permissions.administer) { var permission_header = GuacUI.createChildElement(sections, "dt"); var permission_table = GuacUI.createChildElement( GuacUI.createChildElement(sections, "dd"), "table", "permissions section"); permission_header.textContent = "Permissions:"; // Add system administration checkbox var is_admin = GuacUI.createChildElement( GuacUI.createTabulatedContainer(permission_table, "Administer system:"), "input"); is_admin.setAttribute("type", "checkbox"); is_admin.setAttribute("value", "administer"); // Check if set if (user_perms.administer) is_admin.checked = true; // Add create user permission checkbox var create_users = GuacUI.createChildElement( GuacUI.createTabulatedContainer(permission_table, "Create new users:"), "input"); create_users.setAttribute("type", "checkbox"); create_users.setAttribute("value", "create_user"); // Check if set if (user_perms.create_user) create_users.checked = true; // Add create connection permission checkbox var create_connections = GuacUI.createChildElement( GuacUI.createTabulatedContainer(permission_table, "Create new connections:"), "input"); create_connections.setAttribute("type", "checkbox"); create_connections.setAttribute("value", "create_connection"); // Check if set if (user_perms.create_connection) create_connections.checked = true; // Update system permissions when changed is_admin.onclick = create_users.onclick = create_connections.onclick = function() { // Update permission deltas for ADDED permission if (this.checked) { added_perms[this.value] = true; removed_perms[this.value] = false; } // Update permission deltas for REMOVED permission else { added_perms[this.value] = false; removed_perms[this.value] = true; } } } // If readable connections exist, list them if (GuacAdmin.cached_permissions.administer || GuacAdmin.hasEntry(GuacAdmin.cached_permissions.administer_connection)) { // Add fields for per-connection checkboxes var connections_header = GuacUI.createChildElement(sections, "dt"); connections_header.textContent = "Connections:"; var connections_section = GuacUI.createChildElement(sections, "dd"); // Actual paged connections list var connections = GuacUI.createChildElement( connections_section, "div", "list"); // Button div var connections_buttons = GuacUI.createChildElement( connections_section, "div", "list-pager-buttons"); // Create pager which populates list var connections_pager = new GuacUI.Pager(connections); // Add connections to pager var available_connections = GuacAdmin.cached_connections; for (var i=0; i 0) { // History section var history_section = GuacUI.createChildElement(sections, "dd"); var history_table = GuacUI.createChildElement(history_section, "table", "history section"); var history_table_header = GuacUI.createChildElement( history_table, "tr"); GuacUI.createChildElement(history_table_header, "th").textContent = "Username"; GuacUI.createChildElement(history_table_header, "th").textContent = "Start Time"; GuacUI.createChildElement(history_table_header, "th").textContent = "Duration"; // Paginated body of history var history_buttons = GuacUI.createChildElement(history_section, "div", "list-pager-buttons"); var history_body = GuacUI.createChildElement(history_table, "tbody"); var history_pager = new GuacUI.Pager(history_body); // Add history for (i=0; i