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
 
@@ -17,15 +17,15 @@
 
    <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">
spectrum_manager/src/server.cpp
Show inline comments
 
@@ -226,12 +226,13 @@ std::string Server::send_command(const std::string &jid, const std::string &cmd)
 

	
 
	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();
 
	}
 
@@ -288,13 +289,30 @@ void Server::serve_cmd(struct mg_connection *conn, struct http_message *hm) {
 

	
 
	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;
 
@@ -303,12 +321,30 @@ void Server::serve_users_add(struct mg_connection *conn, struct http_message *hm
 
			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 "
 
@@ -336,16 +372,16 @@ void Server::serve_users(struct mg_connection *conn, struct http_message *hm) {
 
	</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);
 
}
 
@@ -383,14 +419,14 @@ void Server::serve_instance(struct mg_connection *conn, struct http_message *hm,
 
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");
 
@@ -535,12 +571,14 @@ void Server::event_handler(struct mg_connection *conn, int ev, void *p) {
 
	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);
 
@@ -553,12 +591,14 @@ void Server::event_handler(struct mg_connection *conn, int ev, void *p) {
 
	} 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
 
@@ -65,12 +65,14 @@ class Server {
 
		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:
0 comments (0 inline, 0 general)