From 8df5cd7d1b477ce2425e06dc7cc3e6c0dee30023 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Thu, 7 Jan 2016 18:32:17 +0100 Subject: [PATCH] Spectrum2 manager server: Allow registration of users --- include/transport/Frontend.h | 1 + include/transport/UserRegistration.h | 2 +- libtransport/AdminInterface.cpp | 6 +- libtransport/UserRegistration.cpp | 10 +- .../src/frontends/slack/SlackFrontend.cpp | 4 + spectrum/src/frontends/slack/SlackFrontend.h | 1 + spectrum_manager/src/html/login/index.html | 1 + spectrum_manager/src/server.cpp | 107 ++++++++++++------ 8 files changed, 89 insertions(+), 43 deletions(-) diff --git a/include/transport/Frontend.h b/include/transport/Frontend.h index c989bb26..a5f7acfb 100644 --- a/include/transport/Frontend.h +++ b/include/transport/Frontend.h @@ -93,6 +93,7 @@ class Frontend { virtual std::string setOAuth2Code(const std::string &code, const std::string &state) { return "OAuth2 code is not needed for this frontend."; } virtual std::string getOAuth2URL(const std::vector &args) { return ""; } + virtual std::string getRegistrationFields() { return "Jabber ID\n3rd-party network username\n3rd-party network password"; } boost::signal onVCardRequired; boost::signal vcard)> onVCardUpdated; diff --git a/include/transport/UserRegistration.h b/include/transport/UserRegistration.h index b4c5a13f..f95d0fae 100644 --- a/include/transport/UserRegistration.h +++ b/include/transport/UserRegistration.h @@ -50,7 +50,7 @@ class UserRegistration { /// Registers new user. This function stores user into database and subscribe user to transport. /// \param userInfo UserInfo struct with informations about registered user /// \return false if user is already registered - bool registerUser(const UserInfo &userInfo); + bool registerUser(const UserInfo &userInfo, bool allowPasswordChange = false); /// Unregisters user. This function removes all data about user from databa, unsubscribe all buddies /// managed by this transport and disconnects user if he's connected. diff --git a/libtransport/AdminInterface.cpp b/libtransport/AdminInterface.cpp index 80bb4e75..ad171006 100644 --- a/libtransport/AdminInterface.cpp +++ b/libtransport/AdminInterface.cpp @@ -325,6 +325,10 @@ void AdminInterface::handleQuery(Swift::Message::ref message) { message->setBody("Bad argument count. See 'help'."); } } + else if (message->getBody() == "registration_fields") { + std::string fields = m_component->getFrontend()->getRegistrationFields(); + message->setBody(fields); + } else if (message->getBody().find("help") == 0) { std::string help; help += "General:\n"; @@ -363,7 +367,7 @@ void AdminInterface::handleQuery(Swift::Message::ref message) { message->setBody(help); } else { - message->setBody("Unknown command. Try \"help\""); + message->setBody("Unknown command \"" + message->getBody() + "\". Try \"help\""); } } diff --git a/libtransport/UserRegistration.cpp b/libtransport/UserRegistration.cpp index 2f1f29c5..4da63760 100644 --- a/libtransport/UserRegistration.cpp +++ b/libtransport/UserRegistration.cpp @@ -47,13 +47,15 @@ UserRegistration::UserRegistration(Component *component, UserManager *userManage UserRegistration::~UserRegistration(){ } -bool UserRegistration::registerUser(const UserInfo &row) { +bool UserRegistration::registerUser(const UserInfo &row, bool allowPasswordChange) { UserInfo dummy; bool registered = m_storageBackend->getUser(row.jid, dummy); - m_storageBackend->setUser(row); - doUserRegistration(row); - onUserRegistered(row); + if (registered && !allowPasswordChange) { + m_storageBackend->setUser(row); + doUserRegistration(row); + onUserRegistered(row); + } return !registered; } diff --git a/spectrum/src/frontends/slack/SlackFrontend.cpp b/spectrum/src/frontends/slack/SlackFrontend.cpp index 826f312e..06cf45d0 100644 --- a/spectrum/src/frontends/slack/SlackFrontend.cpp +++ b/spectrum/src/frontends/slack/SlackFrontend.cpp @@ -122,6 +122,10 @@ std::string SlackFrontend::getOAuth2URL(const std::vector &args) { return static_cast(m_userManager)->getOAuth2URL(args); } +std::string SlackFrontend::getRegistrationFields() { + return "Slack team name\n3rd-party network username\n3rd-party network password"; +} + void SlackFrontend::disconnectFromServer() { } diff --git a/spectrum/src/frontends/slack/SlackFrontend.h b/spectrum/src/frontends/slack/SlackFrontend.h index c7a50d04..622047a2 100644 --- a/spectrum/src/frontends/slack/SlackFrontend.h +++ b/spectrum/src/frontends/slack/SlackFrontend.h @@ -67,6 +67,7 @@ namespace Transport { virtual void addRoomToRoomList(const std::string &handle, const std::string &name); virtual std::string setOAuth2Code(const std::string &code, const std::string &state); virtual std::string getOAuth2URL(const std::vector &args); + virtual std::string getRegistrationFields(); void handleMessage(boost::shared_ptr message); diff --git a/spectrum_manager/src/html/login/index.html b/spectrum_manager/src/html/login/index.html index c97ad5aa..969f17ed 100644 --- a/spectrum_manager/src/html/login/index.html +++ b/spectrum_manager/src/html/login/index.html @@ -34,6 +34,7 @@
+

If you don't have Spectrum 2 master account yet, you can Register it here. You will be able to manager your Spectrum 2 IM transports after that.

Login form diff --git a/spectrum_manager/src/server.cpp b/spectrum_manager/src/server.cpp index e9a66910..d57eda4e 100644 --- a/spectrum_manager/src/server.cpp +++ b/spectrum_manager/src/server.cpp @@ -189,6 +189,8 @@ bool Server::is_authorized(const struct mg_connection *conn, struct http_message !mg_vcmp(&hm->uri, "/form.css") || !mg_vcmp(&hm->uri, "/style.css") || !mg_vcmp(&hm->uri, "/logo.png") || + !mg_vcmp(&hm->uri, "/users") || + !mg_vcmp(&hm->uri, "/users/add") || !mg_vcmp(&hm->uri, "/authorize")) { return true; } @@ -315,24 +317,26 @@ void Server::serve_logout(struct mg_connection *conn, struct http_message *hm) { } 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); + UserInfo dummy; + bool registered = m_storage->getUser(user, dummy); + if (!registered) { + UserInfo info; + info.jid = user; + info.password = password; + m_storage->setUser(info); + } + else { + redirect_to(conn, hm, "/users?error=This+username+is+already+registered"); + return; + } } } - redirect_to(conn, hm, "/users"); + redirect_to(conn, hm, "/users?ok=1"); } void Server::serve_users_remove(struct mg_connection *conn, struct http_message *hm) { @@ -354,19 +358,35 @@ void Server::serve_users_remove(struct mg_connection *conn, struct http_message } void Server::serve_users(struct mg_connection *conn, struct http_message *hm) { - std::string html = "

Spectrum 2 manager users

"; - + std::string html; Server:session *session = get_session(hm); - if (!session->admin) { - html += "

Only Spectrum 2 manager administrator can access this page.

"; - print_html(conn, hm, html); - return; + if (!session) { + std::string ok = get_http_var(hm, "ok"); + if (!ok.empty()) { + redirect_to(conn, hm, "/"); + return; + } + html += "

Register new Spectrum 2 master account

"; + } + else { + html += "

Spectrum 2 manager users

"; + + if (!session->admin) { + html += "

Only Spectrum 2 manager administrator can access this page.

"; + print_html(conn, hm, html); + return; + } + + html += "

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.

"; } - html += "

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.

"; + std::string error = get_http_var(hm, "error"); + if (!error.empty()) { + html += "

Error: " + error + "

"; + } if (!m_storage) { print_html(conn, hm, html); @@ -374,8 +394,8 @@ void Server::serve_users(struct mg_connection *conn, struct http_message *hm) { } html += " \ -

Add user \ - Add new user to Spectrum 2 manager web interface. \ +

Register user \ + Register new user to Spectrum 2 manager web interface. \

\