From f6cb536eb021abe2c834ac211c94fbeac083a95d Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Tue, 19 Jan 2016 17:25:30 +0100 Subject: [PATCH] Web interface: show registration form --- spectrum_manager/src/APIServer.cpp | 57 +++++++++++++++++ spectrum_manager/src/APIServer.h | 2 + .../src/html/instances/register.shtml | 32 ++++++++++ spectrum_manager/src/html/js/app.js | 61 +++++++++++++++---- 4 files changed, 139 insertions(+), 13 deletions(-) create mode 100644 spectrum_manager/src/html/instances/register.shtml diff --git a/spectrum_manager/src/APIServer.cpp b/spectrum_manager/src/APIServer.cpp index 9ba3b9b7..e129b7cd 100644 --- a/spectrum_manager/src/APIServer.cpp +++ b/spectrum_manager/src/APIServer.cpp @@ -152,6 +152,57 @@ void APIServer::serve_instances_stop(Server *server, Server::session *session, s send_ack(conn, response.find("OK") == std::string::npos, response); } +void APIServer::serve_instances_unregister(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) { + std::string uri(hm->uri.p, hm->uri.len); + std::string instance = uri.substr(uri.rfind("/") + 1); + + UserInfo info; + m_storage->getUser(session->user, info); + + std::string username = ""; + int type = (int) TYPE_STRING; + m_storage->getUserSetting(info.id, instance, type, username); + + if (!username.empty()) { + std::string response = server->send_command(instance, "unregister " + username); + if (!response.empty()) { + username = ""; + m_storage->updateUserSetting(info.id, instance, username); + send_ack(conn, false, response); + } + else { + send_ack(conn, true, "Unknown error."); + } + } + else { + send_ack(conn, true, "You are not registered to this Spectrum 2 instance."); + } +} + +void APIServer::serve_instances_register_form(Server *server, Server::session *session, struct mg_connection *conn, struct http_message *hm) { + std::string uri(hm->uri.p, hm->uri.len); + std::string instance = uri.substr(uri.rfind("/") + 1); + + std::string response = server->send_command(instance, "registration_fields"); + std::vector fields; + boost::split(fields, response, boost::is_any_of("\n")); + + if (fields.size() < 3) { + fields.clear(); + fields.push_back("Jabber ID"); + fields.push_back("3rd-party network username"); + fields.push_back("3rd-party network password"); + } + + Document json; + json.SetObject(); + json.AddMember("error", 0, json.GetAllocator()); + json.AddMember("username_label", fields[0].c_str(), json.GetAllocator()); + json.AddMember("legacy_username_label", fields[1].c_str(), json.GetAllocator()); + json.AddMember("password_label", fields[2].c_str(), json.GetAllocator()); + send_json(conn, json); +} + void APIServer::handleRequest(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm) { if (has_prefix(&hm->uri, "/api/v1/instances/start/")) { serve_instances_start(server, sess, conn, hm); @@ -159,6 +210,12 @@ void APIServer::handleRequest(Server *server, Server::session *sess, struct mg_c else if (has_prefix(&hm->uri, "/api/v1/instances/stop/")) { serve_instances_stop(server, sess, conn, hm); } + else if (has_prefix(&hm->uri, "/api/v1/instances/unregister/")) { + serve_instances_stop(server, sess, conn, hm); + } + else if (has_prefix(&hm->uri, "/api/v1/instances/register_form/")) { + serve_instances_register_form(server, sess, conn, hm); + } else if (mg_vcmp(&hm->uri, "/api/v1/instances") == 0) { serve_instances(server, sess, conn, hm); } diff --git a/spectrum_manager/src/APIServer.h b/spectrum_manager/src/APIServer.h index d1b047e2..3907beea 100644 --- a/spectrum_manager/src/APIServer.h +++ b/spectrum_manager/src/APIServer.h @@ -55,6 +55,8 @@ class APIServer { void serve_instances(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm); void serve_instances_start(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm); void serve_instances_stop(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm); + void serve_instances_unregister(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm); + void serve_instances_register_form(Server *server, Server::session *sess, struct mg_connection *conn, struct http_message *hm); void send_json(struct mg_connection *conn, const Document &d); void send_ack(struct mg_connection *conn, bool error, const std::string &message); diff --git a/spectrum_manager/src/html/instances/register.shtml b/spectrum_manager/src/html/instances/register.shtml new file mode 100644 index 00000000..c5d306b1 --- /dev/null +++ b/spectrum_manager/src/html/instances/register.shtml @@ -0,0 +1,32 @@ + + +

Register Spectrum 2 instance

+
+

Register Spectrum 2 instance + +

+ + + + + +

+ + + + diff --git a/spectrum_manager/src/html/js/app.js b/spectrum_manager/src/html/js/app.js index 4b1bfed7..835baf62 100644 --- a/spectrum_manager/src/html/js/app.js +++ b/spectrum_manager/src/html/js/app.js @@ -18,21 +18,56 @@ function show_instances() { else if (admin) { var command = "start"; } - var row = ''+ instance.name + '' + - '' + instance.status + '' + - '' + command + '' + ''; + var row = '' + row += '' + instance.name + '' + row += '' + instance.status + '' - $("#main_result > tbody:last-child").append(row); + if (command == 'register') { + row += '' + command + '' + ''; + $("#main_result > tbody:last-child").append(row); + } + else { + row += '' + command + '' + ''; + $(".button_command").click(function(e) { + e.preventDefault(); + $(this).parent().empty().progressbar( {value: false} ).css('height', '1em'); - $(".button_command").click(function(e) { - e.preventDefault(); - $(this).parent().empty().progressbar( {value: false} ).css('height', '1em'); - - var url = $(this).attr('href'); - $.get(url, function(data) { - show_instances(); - }); - }) + var url = $(this).attr('href'); + $.get(url, function(data) { + show_instances(); + }); + }) + } }); }); } + +function getQueryParams(qs) { + qs = qs.split('+').join(' '); + + var params = {}, + tokens, + re = /[?&]?([^=]+)=([^&]*)/g; + + while (tokens = re.exec(qs)) { + params[decodeURIComponent(tokens[1])] = decodeURIComponent(tokens[2]); + } + + return params; +} + +function fill_instances_register_form() { + var query = getQueryParams(document.location.search); + $("#instance").attr("value", query.id); + + $.get("/api/v1/instances/register_form/" + query.id, function(data) { + $("#jid_desc").html(data.username_label + ":"); + $("#uin_desc").html(data.legacy_username_label + ":"); + $("#password_desc").html(data.password_label + ":"); + + $("#jid").attr("placeholder", data.username_label); + $("#uin").attr("placeholder", data.legacy_username_label); + $("#password").attr("placeholder", data.password_label); + }); +} +