Changeset - 3ca4f89b8d2e
[Not reviewed]
0 3 0
Jan Kaluza - 10 years ago 2016-01-07 07:27:28
jkaluza@redhat.com
spectrum2_manager server: Allow logout and removing Spectrum 2 manager users
3 files changed with 49 insertions and 7 deletions:
0 comments (0 inline, 0 general)
spectrum_manager/src/html/header.html
Show inline comments
 
@@ -11,22 +11,22 @@
 
    <title>Spectrum 2</title>
 
  </head>
 

	
 
  <body>
 

	
 
    <!-- HEADER -->
 
    <div id="header_wrap" class="outer">
 
        <header class="inner">
 

	
 
            <img id="logo" src="/logo.png" style="width:250px; margin-left: auto;margin-right: auto; display:block;">
 

	
 
            <section id="menu" style="text-align: center;">
 
            <a class="menuitem" href="/">Home</a>
 
            <a class="menuitem" href="/">Instances</a>
 
            <a class="menuitem" href="/users">Users</a>
 
            <a class="menuitem" href="/instances">Instances</a>
 
            <a class="menuitem" href="/logout">Logout</a>
 
            </section>
 
        </header>
 
    </div>
 

	
 
    <!-- MAIN CONTENT -->
 
    <div id="main_content_wrap" class="outer">
 
      <section id="main_content" class="inner">
spectrum_manager/src/server.cpp
Show inline comments
 
@@ -220,24 +220,25 @@ void Server::print_html(struct mg_connection *conn, struct http_message *hm, con
 
			(int) html.size() + m_header.size() + m_footer.size(), m_header.c_str(), html.c_str(), m_footer.c_str());
 
}
 

	
 
std::string Server::send_command(const std::string &jid, const std::string &cmd) {
 
	Swift::SimpleEventLoop eventLoop;
 
	Swift::BoostNetworkFactories networkFactories(&eventLoop);
 

	
 
	ask_local_server(m_config, networkFactories, jid, cmd);
 
	eventLoop.runUntilEvents();
 
	struct timeval td_start,td_end;
 
	float elapsed = 0; 
 
	gettimeofday(&td_start, NULL);
 
	gettimeofday(&td_end, NULL);
 

	
 
	time_t started = time(NULL);
 
	while(get_response().empty() && td_end.tv_sec - td_start.tv_sec < 1) {
 
		gettimeofday(&td_end, NULL);
 
		eventLoop.runOnce();
 
	}
 

	
 
	return get_response();
 
}
 

	
 
void Server::serve_onlineusers(struct mg_connection *conn, struct http_message *hm) {
 
	std::string html;
 
@@ -282,39 +283,74 @@ void Server::serve_cmd(struct mg_connection *conn, struct http_message *hm) {
 
		eventLoop.runUntilEvents();
 
	}
 

	
 
	std::string response = get_response();
 
	
 
	html += "<pre>" + response + "</pre>";
 

	
 
	html += "<a href=\"/\">Back to main page</a>";
 
	html += "</body></html>";
 
	print_html(conn, hm, html);
 
}
 

	
 
void Server::serve_logout(struct mg_connection *conn, struct http_message *hm) {
 
	Server:session *session = get_session(hm);
 
	mg_printf(conn, "HTTP/1.1 302 Found\r\n"
 
		"Set-Cookie: session=%s; max-age=0\r\n"  // Session ID
 
		"Location: /\r\n\r\n",
 
		session->session_id);
 

	
 
	sessions.erase(session->session_id);
 
	delete session;
 
}
 

	
 
void Server::serve_users_add(struct mg_connection *conn, struct http_message *hm) {
 
	Server:session *session = get_session(hm);
 
	if (!session->admin) {
 
		redirect_to(conn, hm, "/");
 
		return;
 
	}
 

	
 
	std::string user = get_http_var(hm, "user");
 
	std::string password = get_http_var(hm, "password");
 

	
 
	if (!user.empty() && !password.empty()) {
 
		UserInfo info;
 
		info.jid = user;
 
		info.password = password;
 
		if (m_storage) {
 
			m_storage->setUser(info);
 
		}
 
	}
 
	redirect_to(conn, hm, "/users");
 
}
 

	
 
void Server::serve_users_remove(struct mg_connection *conn, struct http_message *hm) {
 
	Server:session *session = get_session(hm);
 
	if (!session->admin) {
 
		redirect_to(conn, hm, "/");
 
		return;
 
	}
 

	
 
	if (!m_storage) {
 
		return;
 
	}
 

	
 
	std::string user = get_http_var(hm, "user");
 
	UserInfo info;
 
	m_storage->getUser(user, info);
 
	m_storage->removeUser(info.id);
 
	redirect_to(conn, hm, "/users");
 
}
 

	
 
void Server::serve_users(struct mg_connection *conn, struct http_message *hm) {
 
	std::string html = "<h2>Spectrum 2 manager users</h2>";
 

	
 
	html += "<p>Here, you can add new users who will have access to this web interface. "
 
			"These users will be able to register new accounts on all Spectrum 2 instances "
 
			"running on these server. They won't be able to change any Spectrum 2 instance "
 
			"configuration influencing other users.</p>";
 

	
 
	if (!m_storage) {
 
		print_html(conn, hm, html);
 
		return;
 
	}
 
@@ -330,28 +366,28 @@ void Server::serve_users(struct mg_connection *conn, struct http_message *hm) {
 
	<label><span>Password:</span> \
 
		<input type=\"password\" id=\"password\" name=\"password\" placeholder=\"Password\"></textarea> \
 
	</label> \
 
	<label> \
 
		<span>&nbsp;</span> \
 
		<input type=\"submit\" class=\"button\" value=\"Add user\" />\
 
	</label> \
 
</form><br/>";
 
	std::vector<std::string> users;
 
	m_storage->getUsers(users);
 

	
 
	html += "<table><tr><th>User<th>Action</th></tr>";
 
	BOOST_FOREACH(std::string &jid, users) {
 
	BOOST_FOREACH(std::string &user, users) {
 
		html += "<tr>";
 
		html += "<td><a href=\"/users?jid=" + jid + "\">" + jid + "</a></td>";
 
		html += "<td> </td>";
 
		html += "<td><a href=\"/users?jid=" + user + "\">" + user + "</a></td>";
 
		html += "<td><a href=\"/users/remove?user=" + user + "\">Remove</a></td>";
 
		html += "</tr>";
 
	}
 
	html += "</table>";
 

	
 
	print_html(conn, hm, html);
 
}
 

	
 
void Server::serve_instances_start(struct mg_connection *conn, struct http_message *hm) {
 
	std::string html;
 
	std::string jid = get_http_var(hm, "jid");
 
	if (jid.empty()) {
 
		redirect_to(conn, hm, "/");
 
@@ -377,26 +413,26 @@ void Server::serve_instances_stop(struct mg_connection *conn, struct http_messag
 
void Server::serve_instance(struct mg_connection *conn, struct http_message *hm, const std::string &jid) {
 
	std::string html = "<h2>Spectrum 2 instance: " + jid + "</h2>";
 

	
 
	print_html(conn, hm, html);
 
}
 

	
 
void Server::serve_instances_unregister(struct mg_connection *conn, struct http_message *hm) {
 
	
 
}
 

	
 
void Server::serve_instances_register(struct mg_connection *conn, struct http_message *hm) {
 
	std::string instance = get_http_var(hm, "instance");
 
	if (!instance.empty()) {
 
		serve_instance(conn, hm, instance);
 
	if (instance.empty()) {
 
		serve_instances(conn, hm);
 
		return;
 
	}
 

	
 
	std::string jid = get_http_var(hm, "jid");
 
	std::string uin = get_http_var(hm, "uin");
 
	std::string password = get_http_var(hm, "password");
 
	Server:session *session = get_session(hm);
 
	UserInfo info;
 
	m_storage->getUser(session->user, info);
 

	
 
	if (uin.empty() || password.empty()) {
 
		std::string html = "<h2>Register Spectrum 2 instance</h2>";
 
@@ -529,40 +565,44 @@ void Server::event_handler(struct mg_connection *conn, int ev, void *p) {
 
	struct http_message *hm = (struct http_message *) p;
 

	
 
	if (ev != MG_EV_HTTP_REQUEST) {
 
		return;
 
	}
 

	
 
	if (!is_authorized(conn, hm)) {
 
		redirect_to(conn, hm, "/login");
 
	} else if (mg_vcmp(&hm->uri, "/authorize") == 0) {
 
		authorize(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/") == 0) {
 
		serve_instances(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/logout") == 0) {
 
		serve_logout(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/instances") == 0) {
 
		serve_instances(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/onlineusers") == 0) {
 
		serve_onlineusers(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/cmd") == 0) {
 
		serve_cmd(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/instances/start") == 0) {
 
		serve_instances_start(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/instances/stop") == 0) {
 
		serve_instances_stop(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/instances/register") == 0) {
 
		serve_instances_register(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/instances/unregister") == 0) {
 
		serve_instances_unregister(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/users") == 0) {
 
		serve_users(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/users/add") == 0) {
 
		serve_users_add(conn, hm);
 
	} else if (mg_vcmp(&hm->uri, "/users/remove") == 0) {
 
		serve_users_remove(conn, hm);
 
	} else {
 
		mg_serve_http(conn, hm, s_http_server_opts);
 
	}
 

	
 
	conn->flags |= MG_F_SEND_AND_CLOSE;
 
}
 

	
 

	
 

	
 

	
spectrum_manager/src/server.h
Show inline comments
 
@@ -59,24 +59,26 @@ class Server {
 

	
 
		void event_handler(struct mg_connection *nc, int ev, void *p);
 

	
 
	private:
 
		void serve_instance(struct mg_connection *conn, struct http_message *hm, const std::string &jid);
 
		void serve_instances(struct mg_connection *conn, struct http_message *hm);
 
		void serve_instances_start(struct mg_connection *conn, struct http_message *hm);
 
		void serve_instances_stop(struct mg_connection *conn, struct http_message *hm);
 
		void serve_instances_register(struct mg_connection *conn, struct http_message *hm);
 
		void serve_instances_unregister(struct mg_connection *conn, struct http_message *hm);
 
		void serve_users(struct mg_connection *conn, struct http_message *hm);
 
		void serve_users_add(struct mg_connection *conn, struct http_message *hm);
 
		void serve_users_remove(struct mg_connection *conn, struct http_message *hm);
 
		void serve_logout(struct mg_connection *conn, struct http_message *hm);
 
		void serve_onlineusers(struct mg_connection *conn, struct http_message *hm);
 
		void serve_cmd(struct mg_connection *conn, struct http_message *hm);
 
		void print_html(struct mg_connection *conn, struct http_message *hm, const std::string &html);
 
		std::string send_command(const std::string &jid, const std::string &cmd);
 

	
 
	private:
 
		bool check_password(const std::string &user, const std::string &password);
 
		session *new_session(const std::string &user);
 
		session *get_session(struct http_message *hm);
 

	
 
		void authorize(struct mg_connection *conn, struct http_message *hm);
 

	
0 comments (0 inline, 0 general)