Changeset - 6e1af765cdc0
[Not reviewed]
0 6 0
Jan Kaluza - 9 years ago 2016-03-06 14:50:05
jkaluza@redhat.com
AdminInterface: Support labels for commands
6 files changed with 35 insertions and 17 deletions:
0 comments (0 inline, 0 general)
include/transport/AdminInterfaceCommand.h
Show inline comments
 
@@ -60,25 +60,25 @@ class AdminInterfaceCommand {
 

	
 
		class Arg {
 
			public:
 
				Arg(const std::string &_name, const std::string &_label, const std::string &_example) :
 
					name(_name), label(_label), example(_example) {}
 
				~Arg() {}
 

	
 
				std::string name;
 
				std::string label;
 
				std::string example;
 
		};
 

	
 
		AdminInterfaceCommand(const std::string &name, Category category, Context context, AccessMode accessMode, Actions actions);
 
		AdminInterfaceCommand(const std::string &name, Category category, Context context, AccessMode accessMode, Actions actions, const std::string &label = "");
 

	
 
		virtual ~AdminInterfaceCommand() { }
 

	
 
		void setDescription(const std::string &desc) {
 
			m_desc = desc;
 
		}
 

	
 
		const std::string &getDescription() {
 
			return m_desc;
 
		}
 

	
 
		const std::string &getName() {
 
@@ -103,27 +103,32 @@ class AdminInterfaceCommand {
 
			return m_accessMode;
 
		}
 

	
 
		void addArg(const std::string &name, const std::string &label, const std::string &example = "") {
 
			Arg arg(name, label, example);
 
			m_args.push_back(arg);
 
		}
 

	
 
		const std::list<Arg> &getArgs() {
 
			return m_args;
 
		}
 

	
 
		const std::string &getLabel() {
 
			return m_label;
 
		}
 

	
 
		virtual std::string handleSetRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args);
 
		virtual std::string handleGetRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args);
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args);
 

	
 
	private:
 
		std::string m_name;
 
		Category m_category;
 
		Context m_context;
 
		AccessMode m_accessMode;
 
		Actions m_actions;
 
		std::string m_desc;
 
		std::list<Arg> m_args;
 
		std::string m_label;
 
};
 

	
 
}
libtransport/AdminInterface.cpp
Show inline comments
 
@@ -238,25 +238,26 @@ class BackendsCountCommand : public AdminInterfaceCommand {
 

	
 
	private:
 
		NetworkPluginServer *m_server;
 
};
 

	
 
class ReloadCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		ReloadCommand(Component *component) : AdminInterfaceCommand("reload",
 
							AdminInterfaceCommand::General,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Reload Spectrum 2 configuration") {
 
			m_component = component;
 
			setDescription("Reloads config file");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			bool done = m_component->getConfig()->reload();
 
			if (done) {
 
@@ -269,25 +270,25 @@ class ReloadCommand : public AdminInterfaceCommand {
 

	
 
	private:
 
		Component *m_component;
 
};
 

	
 
class HasOnlineUserCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		HasOnlineUserCommand(UserManager *userManager) : AdminInterfaceCommand("has_online_user",
 
							AdminInterfaceCommand::Users,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute, "Has online user") {
 
			m_userManager = userManager;
 
			setDescription("Returns 1 if user is online");
 
			addArg("username", "Username", "user@domain.tld");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			if (args.empty()) {
 
@@ -694,25 +695,26 @@ class MessagesToXMPPCommand : public AdminInterfaceCommand {
 
		}
 

	
 
	private:
 
		UserManager *m_userManager;
 
};
 

	
 
class RegisterCommand : public AdminInterfaceCommand {
 
	public:
 
		RegisterCommand(UserRegistration *userRegistration, Component *component) : AdminInterfaceCommand("register",
 
							AdminInterfaceCommand::Users,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::UserMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Register") {
 
			m_userRegistration = userRegistration;
 
			setDescription("Registers the new user");
 

	
 
			std::string fields = component->getFrontend()->getRegistrationFields();
 
			std::vector<std::string> args;
 
			boost::split(args, fields, boost::is_any_of("\n"));
 
			addArg("username", args[0]);
 
			if (fields.size() > 1) {
 
				addArg("legacy_username", args[1]);
 
			}
 
			if (fields.size() > 2) {
 
				addArg("legacy_password", args[2]);
 
@@ -752,25 +754,26 @@ class RegisterCommand : public AdminInterfaceCommand {
 

	
 
	private:
 
		UserRegistration *m_userRegistration;
 
};
 

	
 
class UnregisterCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		UnregisterCommand(UserRegistration *userRegistration, Component *component) : AdminInterfaceCommand("unregister",
 
							AdminInterfaceCommand::Users,
 
							AdminInterfaceCommand::UserContext,
 
							AdminInterfaceCommand::UserMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Unregister") {
 
			m_userRegistration = userRegistration;
 
			setDescription("Unregisters existing user");
 

	
 
// 			std::string fields = component->getFrontend()->getRegistrationFields();
 
// 			std::vector<std::string> args;
 
// 			boost::split(args, fields, boost::is_any_of("\n"));
 
// 			addArg("username", args[0]);
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
@@ -857,25 +860,26 @@ class GetOAuth2URLCommand : public AdminInterfaceCommand {
 

	
 
	private:
 
		Component *m_component;
 
};
 

	
 
class HelpCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		HelpCommand(std::map<std::string, AdminInterfaceCommand *> *commands) : AdminInterfaceCommand("help",
 
							AdminInterfaceCommand::General,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Help") {
 
			m_commands = commands;
 
			setDescription("Shows help message");
 
		}
 

	
 
		void generateCategory(AdminInterfaceCommand::Category category, std::string &output) {
 
			output += getCategoryName(category) + ":\n";
 

	
 
			for (std::map<std::string, AdminInterfaceCommand *>::iterator it = m_commands->begin(); it != m_commands->end(); it++) {
 
				AdminInterfaceCommand *command = it->second;
 
				if (command->getCategory() != category) {
 
					continue;
 
				}
 
@@ -911,25 +915,26 @@ class HelpCommand : public AdminInterfaceCommand {
 

	
 
	private:
 
		std::map<std::string, AdminInterfaceCommand *> *m_commands;
 
};
 

	
 
class CommandsCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		CommandsCommand(std::map<std::string, AdminInterfaceCommand *> *commands) : AdminInterfaceCommand("commands",
 
							AdminInterfaceCommand::General,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Available commands") {
 
			m_commands = commands;
 
			setDescription("Shows all the available commands with extended information.");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			std::string output;
 
			for (std::map<std::string, AdminInterfaceCommand *>::iterator it = m_commands->begin(); it != m_commands->end(); it++) {
 
@@ -949,43 +954,46 @@ class CommandsCommand : public AdminInterfaceCommand {
 
				else {
 
					output += " Admin";
 
				}
 

	
 
				output += " Context:";
 
				if (command->getContext() == AdminInterfaceCommand::UserContext) {
 
					output += " User";
 
				}
 
				else {
 
					output += " Global";
 
				}
 

	
 
				output += " Label: \"" + (command->getLabel().empty() ? command->getName() : command->getLabel()) + "\"";
 

	
 
				output += "\n";
 
			}
 

	
 
			return output;
 
		}
 

	
 
	private:
 
		std::map<std::string, AdminInterfaceCommand *> *m_commands;
 
};
 

	
 

	
 
class VariablesCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		VariablesCommand(std::map<std::string, AdminInterfaceCommand *> *commands) : AdminInterfaceCommand("variables",
 
							AdminInterfaceCommand::General,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Available variables") {
 
			m_commands = commands;
 
			setDescription("Shows all the available variables.");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			std::string output;
 
			for (std::map<std::string, AdminInterfaceCommand *>::iterator it = m_commands->begin(); it != m_commands->end(); it++) {
 
@@ -1031,25 +1039,25 @@ class VariablesCommand : public AdminInterfaceCommand {
 

	
 
	private:
 
		std::map<std::string, AdminInterfaceCommand *> *m_commands;
 
};
 

	
 
class ArgsCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		ArgsCommand(std::map<std::string, AdminInterfaceCommand *> *commands) : AdminInterfaceCommand("args",
 
							AdminInterfaceCommand::General,
 
							AdminInterfaceCommand::GlobalContext,
 
							AdminInterfaceCommand::AdminMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute, "Command's arguments") {
 
			m_commands = commands;
 
			setDescription("Shows descripton of arguments for command");
 
			addArg("command", "Command", "register");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			std::map<std::string, AdminInterfaceCommand *>::iterator it = m_commands->find(args[0]);
libtransport/AdminInterfaceCommand.cpp
Show inline comments
 
@@ -21,30 +21,31 @@
 
#include "transport/AdminInterfaceCommand.h"
 
#include "transport/User.h"
 

	
 
#include <boost/foreach.hpp>
 
#include <boost/lexical_cast.hpp>
 
#include <iostream>
 

	
 
#include <Swiften/Version.h>
 
#define HAVE_SWIFTEN_3  (SWIFTEN_VERSION >= 0x030000)
 

	
 
namespace Transport {
 

	
 
AdminInterfaceCommand::AdminInterfaceCommand(const std::string &name, Category category, Context context, AccessMode accessMode, Actions actions) {
 
AdminInterfaceCommand::AdminInterfaceCommand(const std::string &name, Category category, Context context, AccessMode accessMode, Actions actions, const std::string &label) {
 
	m_name = name;
 
	m_category = category;
 
	m_context = context;
 
	m_accessMode = accessMode;
 
	m_actions = actions;
 
	m_label = label;
 
}
 

	
 
const std::string AdminInterfaceCommand::getCategoryName(Category category) {
 
	switch (category) {
 
		case AdminInterfaceCommand::General:
 
			return "General";
 
		case AdminInterfaceCommand::Users:
 
			return "Users";
 
		case AdminInterfaceCommand::Messages:
 
			return "Messages";
 
		case AdminInterfaceCommand::Frontend:
 
			return "Frontend";
spectrum/src/frontends/slack/SlackUserManager.cpp
Show inline comments
 
@@ -40,25 +40,26 @@
 

	
 
namespace Transport {
 

	
 
DEFINE_LOGGER(logger, "SlackUserManager");
 

	
 
class ListRoomsCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		ListRoomsCommand(StorageBackend *storageBackend) : AdminInterfaceCommand("list_rooms",
 
							AdminInterfaceCommand::Frontend,
 
							AdminInterfaceCommand::UserContext,
 
							AdminInterfaceCommand::UserMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"List joined 3rd-party network rooms") {
 
			m_storageBackend = storageBackend;
 
			setDescription("List connected rooms");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *user, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, user, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
 
			if (uinfo.id == -1) {
 
				return "Error: Unknown user";
 
@@ -72,25 +73,26 @@ class ListRoomsCommand : public AdminInterfaceCommand {
 

	
 
	private:
 
		StorageBackend *m_storageBackend;
 
};
 

	
 
class JoinRoomCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		JoinRoomCommand(StorageBackend *storageBackend, Config *cfg) : AdminInterfaceCommand("join_room",
 
							AdminInterfaceCommand::Frontend,
 
							AdminInterfaceCommand::UserContext,
 
							AdminInterfaceCommand::UserMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Join 3rd-party network room") {
 
			m_storageBackend = storageBackend;
 
			setDescription("Join the room");
 

	
 
			std::string legacyRoomLabel = CONFIG_STRING_DEFAULTED(cfg, "service.join_room_room_label", "3rd-party room name");
 
			if (legacyRoomLabel[0] == '%') {
 
				legacyRoomLabel[0] = '#';
 
			}
 

	
 
			std::string legacyRoomExample = CONFIG_STRING_DEFAULTED(cfg, "service.join_room_room_example", "3rd-party room name");
 
			if (legacyRoomExample[0] == '%') {
 
				legacyRoomExample[0] = '#';
 
			}
 
@@ -131,25 +133,26 @@ class JoinRoomCommand : public AdminInterfaceCommand {
 

	
 
	private:
 
		StorageBackend *m_storageBackend;
 
};
 

	
 
class LeaveRoomCommand : public AdminInterfaceCommand {
 
	public:
 
		
 
		LeaveRoomCommand(StorageBackend *storageBackend) : AdminInterfaceCommand("leave_room",
 
							AdminInterfaceCommand::Frontend,
 
							AdminInterfaceCommand::UserContext,
 
							AdminInterfaceCommand::UserMode,
 
							AdminInterfaceCommand::Execute) {
 
							AdminInterfaceCommand::Execute,
 
							"Leave 3rd-party network room") {
 
			m_storageBackend = storageBackend;
 
			setDescription("Leave the room");
 

	
 
			addArg("slack_channel", "Slack Chanel", "mychannel");
 
		}
 

	
 
		virtual std::string handleExecuteRequest(UserInfo &uinfo, User *u, std::vector<std::string> &args) {
 
			std::string ret = AdminInterfaceCommand::handleExecuteRequest(uinfo, u, args);
 
			if (!ret.empty()) {
 
				return ret;
 
			}
 

	
spectrum_manager/src/APIServer.cpp
Show inline comments
 
@@ -301,25 +301,25 @@ void APIServer::serve_instances_commands(Server *server, Server::session *sessio
 

	
 
	std::vector<std::vector<std::string> > tmp;
 
	Value cmds(kArrayType);
 
	BOOST_FOREACH(const std::string &command, commands) {
 
		escaped_list_separator<char> els('\\', ' ', '\"');
 
		tokenizer<escaped_list_separator<char> > tok(command, els);
 

	
 
		std::vector<std::string> tokens;
 
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
 
			tokens.push_back(*beg);
 
		}
 

	
 
		if (tokens.size() != 9) {
 
		if (tokens.size() != 11) {
 
			continue;
 
		}
 

	
 
		if (!session->admin && tokens[6] == "Admin") {
 
			continue;
 
		}
 

	
 
		// Skip command which needs registered users.
 
		if (!session->admin && username.empty() && tokens[8] == "User") {
 
			continue;
 
		}
 

	
 
@@ -328,24 +328,25 @@ void APIServer::serve_instances_commands(Server *server, Server::session *sessio
 
		if (!session->admin && !username.empty() && tokens[0] == "register") {
 
			continue;
 
		}
 

	
 
		tmp.push_back(tokens);
 

	
 
		Value cmd;
 
		cmd.SetObject();
 
		cmd.AddMember("name", tokens[0].c_str(), json.GetAllocator());
 
		cmd.AddMember("desc", tokens[2].c_str(), json.GetAllocator());
 
		cmd.AddMember("category", tokens[4].c_str(), json.GetAllocator());
 
		cmd.AddMember("context", tokens[8].c_str(), json.GetAllocator());
 
		cmd.AddMember("label", tokens[10].c_str(), json.GetAllocator());
 
		cmds.PushBack(cmd, json.GetAllocator());
 
	}
 

	
 
	json.AddMember("commands", cmds, json.GetAllocator());
 
	send_json(conn, json);
 
}
 

	
 
void APIServer::serve_instances_variables(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;
 
@@ -413,25 +414,25 @@ void APIServer::serve_instances_command_args(Server *server, Server::session *se
 
	bool userContext = false;
 
	std::vector<std::string> commands;
 
	boost::split(commands, response, boost::is_any_of("\n"));
 
	BOOST_FOREACH(const std::string &cmd, commands) {
 
		escaped_list_separator<char> els('\\', ' ', '\"');
 
		tokenizer<escaped_list_separator<char> > tok(cmd, els);
 

	
 
		std::vector<std::string> tokens;
 
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
 
			tokens.push_back(*beg);
 
		}
 

	
 
		if (tokens.size() != 9) {
 
		if (tokens.size() != 11) {
 
			continue;
 
		}
 

	
 
		std::cout << tokens[0] << " " << command << "\n";
 
		if (tokens[0] != command) {
 
			continue;
 
		}
 

	
 
		if (!session->admin && tokens[6] == "Admin") {
 
			send_ack(conn, false, "Only admin is able to query this command.");
 
			return;
 
		}
 
@@ -515,25 +516,25 @@ void APIServer::serve_instances_execute(Server *server, Server::session *session
 
	bool userContext = false;
 
	std::vector<std::string> commands;
 
	boost::split(commands, response, boost::is_any_of("\n"));
 
	BOOST_FOREACH(const std::string &cmd, commands) {
 
		escaped_list_separator<char> els('\\', ' ', '\"');
 
		tokenizer<escaped_list_separator<char> > tok(cmd, els);
 

	
 
		std::vector<std::string> tokens;
 
		for(tokenizer<escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end(); ++beg) {
 
			tokens.push_back(*beg);
 
		}
 

	
 
		if (tokens.size() != 9) {
 
		if (tokens.size() != 11) {
 
			continue;
 
		}
 

	
 
		std::cout << tokens[0] << " " << command << "\n";
 
		if (tokens[0] != command) {
 
			continue;
 
		}
 

	
 
		if (!session->admin && tokens[6] == "Admin") {
 
			send_ack(conn, false, "Only admin is able to execute.");
 
			return;
 
		}
spectrum_manager/src/html/js/app.js
Show inline comments
 
@@ -236,25 +236,25 @@ function execute_command(instance, command) {
 
		})
 
	});
 
}
 

	
 
function show_instance() {
 
	var query = getQueryParams(document.location.search);
 

	
 
	$("#main_content").html("<h2>Instance: " + query.id + "</h2><h4>Available commands:</h4><table id='commands'><tr><th>Name<th>Category</th><th>Description</th></tr></table><h4>Available variables:</h4><table id='variables'><tr><th>Name<th>Value</th><th>Read-only</th><th>Desc</th></tr></table>");
 

	
 
	$.get($.cookie("base_location") + "api/v1/instances/commands/" + query.id, function(data) {
 
		$.each(data.commands, function(i, command) {
 
			var row = '<tr>'
 
			row += '<td><a class="button_command" command="' + command.name + '" instance="' + query.id + '" href="' + $.cookie("base_location") +  'api/v1/instances/command_args/' + query.id + '?command=' + command.name +'">' + command.name + '</a></td>';
 
			row += '<td><a class="button_command" command="' + command.name + '" instance="' + query.id + '" href="' + $.cookie("base_location") +  'api/v1/instances/command_args/' + query.id + '?command=' + command.name +'">' + command.label + '</a></td>';
 
			row += '<td>' + command.category + '</td>';
 
			row += '<td>' + command.desc + '</td>';
 
			row += '</tr>';
 
			$("#commands  > tbody:last-child").append(row);
 
		});
 

	
 
		$(".button_command").click(function(e) {
 
			e.preventDefault();
 

	
 
			var command = $(this).attr('command');
 
			var instance = $(this).attr('instance');
 
			execute_command(instance, command);
0 comments (0 inline, 0 general)