/*
* 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 user login and
* connection management.
*/
var GuacamoleRootUI = {
"sections": {
"login_form" : document.getElementById("login-form"),
"recent_connections" : document.getElementById("recent-connections"),
"all_connections" : document.getElementById("all-connections")
},
"messages": {
"login_error" : document.getElementById("login-error"),
"no_recent_connections" : document.getElementById("no-recent")
},
"fields": {
"username" : document.getElementById("username"),
"password" : document.getElementById("password"),
"clipboard" : document.getElementById("clipboard")
},
"buttons": {
"login" : document.getElementById("login"),
"logout" : document.getElementById("logout"),
"manage" : document.getElementById("manage")
},
"settings": {
"auto_fit" : document.getElementById("auto-fit"),
"disable_sound" : document.getElementById("disable-sound")
},
"views": {
"login" : document.getElementById("login-ui"),
"connections" : document.getElementById("connection-list-ui")
},
"session_state" : new GuacamoleSessionState()
};
/**
* Attempts to login the given user using the given password, throwing an
* error if the process fails.
*
* @param {String} username The name of the user to login as.
* @param {String} password The password to use to authenticate the user.
*/
GuacamoleRootUI.login = function(username, password) {
// Get parameters from query string
var parameters = window.location.search.substring(1);
// Get username and password from form
var data =
"username=" + encodeURIComponent(username)
+ "&password=" + encodeURIComponent(password)
// Include query parameters in submission data
if (parameters) data += "&" + parameters;
// Log in
var xhr = new XMLHttpRequest();
xhr.open("POST", "login", false);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(data);
// Handle failures
if (xhr.status != 200)
throw new Error("Invalid login");
};
/**
* Set of all thumbnailed connections, indexed by ID.
*/
GuacamoleRootUI.thumbnailConnections = {};
/**
* Set of all connections, indexed by ID.
*/
GuacamoleRootUI.connections = {};
/**
* Adds the given connection to the recent connections list.
*/
GuacamoleRootUI.addRecentConnection = function(connection) {
// Add connection object to list of thumbnailed connections
GuacamoleRootUI.thumbnailConnections[connection.connection.id] =
connection;
// Add connection to recent list
GuacamoleRootUI.sections.recent_connections.appendChild(
connection.getElement());
// Hide "No recent connections" message
GuacamoleRootUI.messages.no_recent_connections.style.display = "none";
};
/**
* Resets the interface such that the login UI is displayed if
* the user is not authenticated (or authentication fails) and
* the connection list UI (or the client for the only available
* connection, if there is only one) is displayed if the user is
* authenticated.
*/
GuacamoleRootUI.reset = function() {
// Get parameters from query string
var parameters = window.location.search.substring(1);
function hasEntry(object) {
for (var name in object)
return true;
return false;
}
// Read connections
var connections;
try {
connections = GuacamoleService.Connections.list(parameters);
// Show admin elements if admin permissions available
var permissions = GuacamoleService.Permissions.list();
if (permissions.create_connection
|| permissions.create_user
|| hasEntry(permissions.update_user)
|| hasEntry(permissions.remove_user)
|| hasEntry(permissions.administer_user)
|| hasEntry(permissions.update_connection)
|| hasEntry(permissions.remove_connection)
|| hasEntry(permissions.administer_connection))
GuacUI.addClass(document.body, "admin");
else
GuacUI.removeClass(document.body, "admin");
}
catch (e) {
// Show login UI if unable to get connections
GuacamoleRootUI.views.login.style.display = "";
GuacamoleRootUI.views.connections.style.display = "none";
return;
}
// Add connection icons
for (var i=0; i