From c00e81f9f50c4fe50ee32b02a689d68c9bc595b6 Mon Sep 17 00:00:00 2001 From: Maxim Mamontov Date: Wed, 28 May 2014 23:01:41 +0300 Subject: [PATCH] Implemented --add-admin. --- include/stg/admin_conf.h | 1 + projects/sgconf/action.h | 2 +- projects/sgconf/actions.h | 24 ++++++++--------- projects/sgconf/admins.cpp | 33 ++++++++++++++++++++--- projects/sgconf/api_action.cpp | 6 ++--- projects/sgconf/api_action.h | 9 ++++++- projects/sgconf/corps.cpp | 2 +- projects/sgconf/main.cpp | 10 +++---- projects/sgconf/options.cpp | 14 ++++++---- projects/sgconf/options.h | 4 +-- projects/sgconf/services.cpp | 6 ++--- projects/sgconf/tariffs.cpp | 18 ++++++------- projects/sgconf/users.cpp | 48 +++++++++++++++++----------------- projects/sgconf/utils.h | 48 ++++++++++++++++++++++++++++++++++ 14 files changed, 155 insertions(+), 70 deletions(-) create mode 100644 projects/sgconf/utils.h diff --git a/include/stg/admin_conf.h b/include/stg/admin_conf.h index 8b1a988e..7fbf4440 100644 --- a/include/stg/admin_conf.h +++ b/include/stg/admin_conf.h @@ -78,6 +78,7 @@ struct ADMIN_CONF //----------------------------------------------------------------------------- struct ADMIN_CONF_RES { + ADMIN_CONF_RES() {} ADMIN_CONF_RES(const ADMIN_CONF & conf) : priv(conf.priv), login(conf.login), diff --git a/projects/sgconf/action.h b/projects/sgconf/action.h index b45ac470..940ea7f6 100644 --- a/projects/sgconf/action.h +++ b/projects/sgconf/action.h @@ -41,7 +41,7 @@ class ACTION virtual std::string ParamDescription() const = 0; virtual std::string DefaultDescription() const = 0; virtual OPTION_BLOCK & Suboptions() = 0; - virtual PARSER_STATE Parse(int argc, char ** argv) = 0; + virtual PARSER_STATE Parse(int argc, char ** argv, void * data = NULL) = 0; virtual void ParseValue(const std::string &) {} class ERROR : public std::runtime_error diff --git a/projects/sgconf/actions.h b/projects/sgconf/actions.h index c47f9776..07bcfb8b 100644 --- a/projects/sgconf/actions.h +++ b/projects/sgconf/actions.h @@ -30,6 +30,8 @@ #include +#include + namespace SGCONF { @@ -46,7 +48,7 @@ class FUNC0_ACTION : public ACTION virtual std::string ParamDescription() const { return ""; } virtual std::string DefaultDescription() const { return ""; } virtual OPTION_BLOCK & Suboptions() { return m_suboptions; } - virtual PARSER_STATE Parse(int argc, char ** argv) + virtual PARSER_STATE Parse(int argc, char ** argv, void * /*data*/) { m_func(); return PARSER_STATE(true, argc, argv); @@ -88,7 +90,7 @@ class PARAM_ACTION : public ACTION virtual std::string ParamDescription() const { return m_description; } virtual std::string DefaultDescription() const; virtual OPTION_BLOCK & Suboptions() { return m_suboptions; } - virtual PARSER_STATE Parse(int argc, char ** argv); + virtual PARSER_STATE Parse(int argc, char ** argv, void * /*data*/); virtual void ParseValue(const std::string & value); private: @@ -117,7 +119,7 @@ return m_hasDefault ? " (default: '" + m_defaltValue + "')" template inline -PARSER_STATE PARAM_ACTION::Parse(int argc, char ** argv) +PARSER_STATE PARAM_ACTION::Parse(int argc, char ** argv, void * /*data*/) { if (argc == 0 || argv == NULL || @@ -151,7 +153,7 @@ m_param = stringValue; template <> inline -PARSER_STATE PARAM_ACTION::Parse(int argc, char ** argv) +PARSER_STATE PARAM_ACTION::Parse(int argc, char ** argv, void * /*data*/) { if (argc == 0 || argv == NULL || @@ -182,10 +184,8 @@ class KV_ACTION : public ACTION { public: KV_ACTION(const std::string & name, - std::map & kvs, const std::string & paramDescription) : m_name(name), - m_kvs(kvs), m_description(paramDescription) {} @@ -194,32 +194,32 @@ class KV_ACTION : public ACTION virtual std::string ParamDescription() const { return m_description; } virtual std::string DefaultDescription() const { return ""; } virtual OPTION_BLOCK & Suboptions() { return m_suboptions; } - virtual PARSER_STATE Parse(int argc, char ** argv); + virtual PARSER_STATE Parse(int argc, char ** argv, void * data); private: std::string m_name; - std::map & m_kvs; std::string m_description; OPTION_BLOCK m_suboptions; }; inline -PARSER_STATE KV_ACTION::Parse(int argc, char ** argv) +PARSER_STATE KV_ACTION::Parse(int argc, char ** argv, void * data) { if (argc == 0 || argv == NULL || *argv == NULL) throw ERROR("Missing argument."); -m_kvs[m_name] = *argv; +assert(data != NULL && "Expecting container pointer."); +std::map & kvs = *static_cast*>(data); +kvs[m_name] = *argv; return PARSER_STATE(false, --argc, ++argv); } inline KV_ACTION * MakeKVAction(const std::string & name, - std::map & kvs, const std::string & paramDescription) { -return new KV_ACTION(name, kvs, paramDescription); +return new KV_ACTION(name, paramDescription); } } // namespace SGCONF diff --git a/projects/sgconf/admins.cpp b/projects/sgconf/admins.cpp index b560f581..6a59fa28 100644 --- a/projects/sgconf/admins.cpp +++ b/projects/sgconf/admins.cpp @@ -3,6 +3,7 @@ #include "api_action.h" #include "options.h" #include "config.h" +#include "utils.h" #include "stg/servconf.h" #include "stg/servconf_types.h" @@ -46,10 +47,28 @@ std::cout << Indent(level, true) << "login: " << info.login << "\n" std::vector GetAdminParams() { std::vector params; -params.push_back({"priv", "", "priviledges"}); +params.push_back(SGCONF::API_ACTION::PARAM("password", "", "password")); +params.push_back(SGCONF::API_ACTION::PARAM("priv", "", "priviledges")); return params; } +void ConvPriv(const std::string & value, RESETABLE & res) +{ +if (value.length() != 9) + throw SGCONF::ACTION::ERROR("Priviledges value should be a 9-digits length binary number."); +PRIV priv; +priv.corpChg = (value[0] == '0' ? 0 : 1); +priv.serviceChg = (value[1] == '0' ? 0 : 1); +priv.tariffChg = (value[2] == '0' ? 0 : 1); +priv.adminChg = (value[3] == '0' ? 0 : 1); +priv.userAddDel = (value[4] == '0' ? 0 : 1); +priv.userPasswd = (value[5] == '0' ? 0 : 1); +priv.userCash = (value[6] == '0' ? 0 : 1); +priv.userConf = (value[7] == '0' ? 0 : 1); +priv.userStat = (value[8] == '0' ? 0 : 1); +res = priv; +} + void SimpleCallback(bool result, const std::string & reason, void * /*data*/) @@ -133,11 +152,17 @@ return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok; bool AddAdminFunction(const SGCONF::CONFIG & config, const std::string & arg, - const std::map & /*options*/) + const std::map & options) { // TODO -std::cerr << "Unimplemented.\n"; -return false; +ADMIN_CONF_RES conf; +conf.login = arg; +SGCONF::MaybeSet(options, "priv", conf.priv, ConvPriv); +STG::SERVCONF proto(config.server.data(), + config.port.data(), + config.userName.data(), + config.userPass.data()); +return proto.AddAdmin(arg, conf, SimpleCallback, NULL) == STG::st_ok; } bool ChgAdminFunction(const SGCONF::CONFIG & config, diff --git a/projects/sgconf/api_action.cpp b/projects/sgconf/api_action.cpp index 420f5f0a..d5b1e8e6 100644 --- a/projects/sgconf/api_action.cpp +++ b/projects/sgconf/api_action.cpp @@ -3,7 +3,7 @@ #include "actions.h" #include "parser_state.h" -SGCONF::PARSER_STATE SGCONF::API_ACTION::Parse(int argc, char ** argv) +SGCONF::PARSER_STATE SGCONF::API_ACTION::Parse(int argc, char ** argv, void * /*data*/) { PARSER_STATE state(false, argc, argv); if (!m_argument.empty()) @@ -16,7 +16,7 @@ if (!m_argument.empty()) --state.argc; ++state.argv; } -m_suboptions.Parse(state.argc, state.argv); +state = m_suboptions.Parse(state.argc, state.argv, &m_params); m_commands.Add(m_funPtr, m_argument, m_params); return state; } @@ -34,7 +34,7 @@ SGCONF::API_ACTION::API_ACTION(COMMANDS & commands, std::vector::const_iterator it(params.begin()); while (it != params.end()) { - m_suboptions.Add(it->name, MakeKVAction(it->name, m_params, it->shortDescr), it->longDescr); + m_suboptions.Add(it->name, MakeKVAction(it->name, it->shortDescr), it->longDescr); ++it; } } diff --git a/projects/sgconf/api_action.h b/projects/sgconf/api_action.h index e5f95b2c..f27715ca 100644 --- a/projects/sgconf/api_action.h +++ b/projects/sgconf/api_action.h @@ -63,6 +63,13 @@ class API_ACTION : public ACTION public: struct PARAM { + PARAM(const std::string & n, + const std::string & s, + const std::string & l) + : name(n), + shortDescr(s), + longDescr(l) + {} std::string name; std::string shortDescr; std::string longDescr; @@ -88,7 +95,7 @@ class API_ACTION : public ACTION virtual std::string ParamDescription() const { return m_description; } virtual std::string DefaultDescription() const { return ""; } virtual OPTION_BLOCK & Suboptions() { return m_suboptions; } - virtual PARSER_STATE Parse(int argc, char ** argv); + virtual PARSER_STATE Parse(int argc, char ** argv, void * /*data*/); private: COMMANDS & m_commands; diff --git a/projects/sgconf/corps.cpp b/projects/sgconf/corps.cpp index 97c3add0..82673377 100644 --- a/projects/sgconf/corps.cpp +++ b/projects/sgconf/corps.cpp @@ -32,7 +32,7 @@ std::cout << Indent(level, true) << "name: " << info.name << "\n" std::vector GetCorpParams() { std::vector params; -params.push_back({"cash", "", "\tcorporation's cash"}); +params.push_back(SGCONF::API_ACTION::PARAM("cash", "", "\tcorporation's cash")); return params; } diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp index 73917852..22e998f3 100644 --- a/projects/sgconf/main.cpp +++ b/projects/sgconf/main.cpp @@ -163,7 +163,7 @@ class CONFIG_ACTION : public ACTION virtual std::string ParamDescription() const { return m_description; } virtual std::string DefaultDescription() const { return ""; } virtual OPTION_BLOCK & Suboptions() { return m_suboptions; } - virtual PARSER_STATE Parse(int argc, char ** argv); + virtual PARSER_STATE Parse(int argc, char ** argv, void * /*data*/); private: SGCONF::CONFIG & m_config; @@ -175,7 +175,7 @@ class CONFIG_ACTION : public ACTION }; -PARSER_STATE CONFIG_ACTION::Parse(int argc, char ** argv) +PARSER_STATE CONFIG_ACTION::Parse(int argc, char ** argv, void * /*data*/) { if (argc == 0 || argv == NULL || @@ -298,15 +298,15 @@ else } config = configOverride; + +std::cerr << "Config: " << config.Serialize() << std::endl; +return commands.Execute(config) ? 0 : -1; } catch (const std::exception& ex) { std::cerr << ex.what() << "\n"; return -1; } - -std::cerr << "Config: " << config.Serialize() << std::endl; -return commands.Execute(config) ? 0 : -1; } //----------------------------------------------------------------------------- diff --git a/projects/sgconf/options.cpp b/projects/sgconf/options.cpp index f5e423ad..49e04d64 100644 --- a/projects/sgconf/options.cpp +++ b/projects/sgconf/options.cpp @@ -134,18 +134,20 @@ if (*arg++ != '-') return false; if (*arg == '-') +{ return m_longName == arg + 1; +} return m_shortName == arg; } -PARSER_STATE OPTION::Parse(int argc, char ** argv) +PARSER_STATE OPTION::Parse(int argc, char ** argv, void * data) { if (!m_action) throw ERROR("Option is not defined."); try { - return m_action->Parse(argc, argv); + return m_action->Parse(argc, argv, data); } catch (const ACTION::ERROR & ex) { @@ -199,14 +201,16 @@ std::for_each(m_options.begin(), std::bind2nd(std::mem_fun_ref(&OPTION::Help), level + 1)); } -PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv) +PARSER_STATE OPTION_BLOCK::Parse(int argc, char ** argv, void * data) { PARSER_STATE state(false, argc, argv); +if (state.argc == 0) + return state; while (state.argc > 0 && !state.stop) { std::vector