diff --git a/spectrum_manager/src/html/header.html b/spectrum_manager/src/html/header.html index f4baa073..b3dce4d9 100644 --- a/spectrum_manager/src/html/header.html +++ b/spectrum_manager/src/html/header.html @@ -20,9 +20,9 @@ diff --git a/spectrum_manager/src/server.cpp b/spectrum_manager/src/server.cpp index b255f505..bb3d3d38 100644 --- a/spectrum_manager/src/server.cpp +++ b/spectrum_manager/src/server.cpp @@ -229,6 +229,7 @@ std::string Server::send_command(const std::string &jid, const std::string &cmd) 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) { @@ -291,7 +292,24 @@ void Server::serve_cmd(struct mg_connection *conn, struct http_message *hm) { 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"); @@ -306,6 +324,24 @@ void Server::serve_users_add(struct mg_connection *conn, struct http_message *hm 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 = "

Spectrum 2 manager users

"; @@ -339,10 +375,10 @@ void Server::serve_users(struct mg_connection *conn, struct http_message *hm) { m_storage->getUsers(users); html += ""; - BOOST_FOREACH(std::string &jid, users) { + BOOST_FOREACH(std::string &user, users) { html += ""; - html += ""; - html += ""; + html += ""; + html += ""; html += ""; } html += "
UserAction
" + jid + " " + user + "Remove
"; @@ -386,8 +422,8 @@ void Server::serve_instances_unregister(struct mg_connection *conn, struct http_ 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; } @@ -538,6 +574,8 @@ void Server::event_handler(struct mg_connection *conn, int ev, void *p) { 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) { @@ -556,6 +594,8 @@ void Server::event_handler(struct mg_connection *conn, int ev, void *p) { 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); } diff --git a/spectrum_manager/src/server.h b/spectrum_manager/src/server.h index 95973110..5c1066d9 100644 --- a/spectrum_manager/src/server.h +++ b/spectrum_manager/src/server.h @@ -68,6 +68,8 @@ class Server { 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);