Changeset - f6cb536eb021
[Not reviewed]
0 3 1
Jan Kaluza - 10 years ago 2016-01-19 17:25:30
jkaluza@redhat.com
Web interface: show registration form
4 files changed with 139 insertions and 13 deletions:
0 comments (0 inline, 0 general)
spectrum_manager/src/APIServer.cpp
Show inline comments
 
@@ -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<std::string> 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);
 
	}
spectrum_manager/src/APIServer.h
Show inline comments
 
@@ -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);
 

	
spectrum_manager/src/html/instances/register.shtml
Show inline comments
 
new file 100644
 
<!--#include virtual="/header.html" -->
 

	
 
<h2>Register Spectrum 2 instance</h2>
 
<form action="/api/v1/instances/register" class="basic-grey" method="POST"> 
 
<h1>Register Spectrum 2 instance 
 
	<span id="description"></span> 
 
</h1> 
 
<label> 
 
	<span id="jid_desc"></span> 
 
	<input type="text" id="jid" name="jid" placeholder=""></textarea> 
 
</label> 
 
<label> 
 
	<span id="uin_desc"></span> 
 
	<input type="text" id="uin" name="uin" placeholder=""></textarea> 
 
</label> 
 
<label><span id="password_desc"></span> 
 
	<input type="password" id="password" name="password" placeholder=""></textarea> 
 
</label> 
 
<label> 
 
	<span>&nbsp;</span> 
 
	<input type="submit" class="button" value="Register" />
 
</label> 
 
<input type="hidden" name="instance" id="instance" value=""></input> 
 
</form><br/>
 

	
 
<script type="text/javascript">
 
$(function() {
 
	fill_instances_register_form();
 
});
 
</script>
 

	
 
<!--#include virtual="/footer.html" -->
spectrum_manager/src/html/js/app.js
Show inline comments
 
@@ -18,21 +18,56 @@ function show_instances() {
 
			else if (admin) {
 
				var command = "start";
 
			}
 
			var row = '<tr><td>'+ instance.name + '</td>' +
 
			'<td>' + instance.status + '</td>' +
 
			'<td><a class="button_command" href="/api/v1/instances/' + command + '/' + instance.id + '">' + command + '</a>' + '</td></tr>';
 
			var row = '<tr>'
 
			row += '<td>' + instance.name + '</td>'
 
			row += '<td>' + instance.status + '</td>'
 

	
 
			$("#main_result  > tbody:last-child").append(row);
 

	
 
			$(".button_command").click(function(e) {
 
				e.preventDefault();
 
				$(this).parent().empty().progressbar( {value: false} ).css('height', '1em');
 
			if (command == 'register') {
 
				row += '<td><a class="button_command" href="/instances/register.shtml?id=' + instance.id + '">' + command + '</a>' + '</td></tr>';
 
				$("#main_result  > tbody:last-child").append(row);
 
			}
 
			else {
 
				row += '<td><a class="button_command" href="/api/v1/instances/' + command + '/' + instance.id + '">' + command + '</a>' + '</td></tr>';
 
				$(".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);
 
	});
 
}
 

	
0 comments (0 inline, 0 general)