From: Maxim Mamontov Date: Tue, 20 May 2014 19:42:31 +0000 (+0300) Subject: Implemented some operations with admins. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/20d884ddac6b8cacedb2701e282efe3ff9785cbf Implemented some operations with admins. --- diff --git a/projects/sgconf/Makefile b/projects/sgconf/Makefile index e564b246..dd2cc66b 100644 --- a/projects/sgconf/Makefile +++ b/projects/sgconf/Makefile @@ -10,6 +10,7 @@ SRCS = ./main.cpp \ ./common_sg.cpp \ ./options.cpp \ ./actions.cpp \ + ./admins.cpp \ ./xml.cpp STGLIBS = conffiles \ diff --git a/projects/sgconf/admins.cpp b/projects/sgconf/admins.cpp new file mode 100644 index 00000000..03aa5a20 --- /dev/null +++ b/projects/sgconf/admins.cpp @@ -0,0 +1,137 @@ +#include "admins.h" + +#include "config.h" + +#include "stg/servconf.h" +#include "stg/servconf_types.h" +#include "stg/os_int.h" + +#include +#include + +namespace +{ + +std::string Indent(size_t level, bool dash = false) +{ +if (level == 0) + return ""; +return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' '); +} + +std::string PrivToString(const PRIV& priv) +{ +return std::string("") + + (priv.corpChg ? "1" : "0") + + (priv.serviceChg ? "1" : "0") + + (priv.tariffChg ? "1" : "0") + + (priv.adminChg ? "1" : "0") + + (priv.userAddDel ? "1" : "0") + + (priv.userPasswd ? "1" : "0") + + (priv.userCash ? "1" : "0") + + (priv.userConf ? "1" : "0") + + (priv.userStat ? "1" : "0"); +} + +void PrintAdmin(const STG::GET_ADMIN::INFO & info, size_t level = 0) +{ +std::cout << Indent(level, true) << "login: " << info.login << "\n" + << Indent(level) << "priviledges: " << PrivToString(info.priv) << "\n"; +} + +void SimpleCallback(bool result, + const std::string & reason, + void * /*data*/) +{ +if (!result) + { + std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl; + return; + } +std::cout << "Success.\n"; +} + +void GetAdminsCallback(bool result, + const std::string & reason, + const std::vector & info, + void * /*data*/) +{ +if (!result) + { + std::cerr << "Failed to get admin list. Reason: '" << reason << "'." << std::endl; + return; + } +std::cout << "Admins:\n"; +for (size_t i = 0; i < info.size(); ++i) + PrintAdmin(info[i], 1); +} + +void GetAdminCallback(bool result, + const std::string & reason, + const std::vector & info, + void * data) +{ +assert(data != NULL && "Expecting pointer to std::string with the admin's login."); +const std::string & login = *static_cast(data); +if (!result) + { + std::cerr << "Failed to get admin. Reason: '" << reason << "'." << std::endl; + return; + } +for (size_t i = 0; i < info.size(); ++i) + if (info[i].login == login) + PrintAdmin(info[i]); +} + +} + + +bool SGCONF::GetAdminsFunction(const SGCONF::CONFIG & config, + const std::string & /*arg*/, + const std::map & /*options*/) +{ +STG::SERVCONF proto(config.server.data(), + config.port.data(), + config.userName.data(), + config.userPass.data()); +return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok; +} + +bool SGCONF::GetAdminFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) +{ +STG::SERVCONF proto(config.server.data(), + config.port.data(), + config.userName.data(), + config.userPass.data()); +// STG currently doesn't support . +// So get a list of admins and filter it. 'data' param holds a pointer to 'login'. +std::string login(arg); +return proto.GetAdmins(GetAdminCallback, &login) == STG::st_ok; +} + +bool SGCONF::DelAdminFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) +{ +STG::SERVCONF proto(config.server.data(), + config.port.data(), + config.userName.data(), + config.userPass.data()); +return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok; +} + +bool SGCONF::AddAdminFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) +{ +return false; +} + +bool SGCONF::ChgAdminFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & options) +{ +return false; +} diff --git a/projects/sgconf/admins.h b/projects/sgconf/admins.h new file mode 100644 index 00000000..0c28e923 --- /dev/null +++ b/projects/sgconf/admins.h @@ -0,0 +1,34 @@ +#ifndef __STG_SGCONF_ADMINS_H__ +#define __STG_SGCONF_ADMINS_H__ + +#include +#include + +namespace SGCONF +{ + +class CONFIG; + +bool GetAdminsFunction(const CONFIG & config, + const std::string & /*arg*/, + const std::map & /*options*/); + +bool GetAdminFunction(const CONFIG & config, + const std::string & arg, + const std::map & /*options*/); + +bool DelAdminFunction(const CONFIG & config, + const std::string & arg, + const std::map & /*options*/); + +bool AddAdminFunction(const CONFIG & config, + const std::string & arg, + const std::map & options); + +bool ChgAdminFunction(const CONFIG & config, + const std::string & arg, + const std::map & options); + +} // namespace SGCONF + +#endif diff --git a/projects/sgconf/config.h b/projects/sgconf/config.h index 0c76d2de..424ca519 100644 --- a/projects/sgconf/config.h +++ b/projects/sgconf/config.h @@ -70,6 +70,6 @@ struct CONFIG } }; -} +} // namespace SGCONF #endif diff --git a/projects/sgconf/main.cpp b/projects/sgconf/main.cpp index 3ad0519e..babfd86f 100644 --- a/projects/sgconf/main.cpp +++ b/projects/sgconf/main.cpp @@ -24,6 +24,7 @@ #include "sg_error_codes.h" #include "xml.h" +#include "admins.h" #include "options.h" #include "actions.h" #include "config.h" @@ -377,6 +378,13 @@ ACTION * MakeAPIAction(COMMANDS & commands, return new API_ACTION(commands, paramDescription, needArgument, funPtr); } +inline +ACTION * MakeAPIAction(COMMANDS & commands, + API_FUNCTION funPtr) +{ +return new API_ACTION(commands, "", false, funPtr); +} + bool RawXMLFunction(const SGCONF::CONFIG & config, const std::string & arg, const std::map & /*options*/) @@ -1375,12 +1383,12 @@ SGCONF::OPTION_BLOCK & block = blocks.Add("Connection options") .Add("a", "address", SGCONF::MakeParamAction(config, ""), "connection params as a single string in format: :@:"); blocks.Add("Raw XML") .Add("r", "raw", SGCONF::MakeAPIAction(commands, "", true, SGCONF::RawXMLFunction), "\tmake raw XML request"); -/*blocks.Add("Admins management options") - .Add("get-admins", SGCONF::MakeConfAction()) - .Add("get-admin", SGCONF::MakeConfAction()) - .Add("add-admin", SGCONF::MakeConfAction()) - .Add("del-admin", SGCONF::MakeConfAction()) - .Add("chg-admin", SGCONF::MakeConfAction());*/ +blocks.Add("Admins management options") + .Add("get-admins", SGCONF::MakeAPIAction(commands, SGCONF::GetAdminsFunction), "\tget admin list") + .Add("get-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::GetAdminFunction), "\tget admin") + .Add("add-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::AddAdminFunction), "\tadd admin") + .Add("del-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::DelAdminFunction), "\tdel admin") + .Add("chg-admin", SGCONF::MakeAPIAction(commands, "", true, SGCONF::ChgAdminFunction), "\tchange admin"); SGCONF::PARSER_STATE state(false, argc, argv); diff --git a/stglibs/srvconf.lib/parsers/get_admin.cpp b/stglibs/srvconf.lib/parsers/get_admin.cpp index 41217dfb..809280b7 100644 --- a/stglibs/srvconf.lib/parsers/get_admin.cpp +++ b/stglibs/srvconf.lib/parsers/get_admin.cpp @@ -34,10 +34,10 @@ namespace STG template <> inline -bool GetValue(const char ** attr, PRIV & value) +bool GetValue(const char ** attr, PRIV & value, const std::string & attrName) { uint32_t priv; -if (!GetValue(attr, priv)) +if (!GetValue(attr, priv, attrName)) return false; value = priv; return true; @@ -69,8 +69,8 @@ depth++; if (depth == 1) ParseAdmin(el, attr); -if (depth == 2 && parsingAnswer) - ParseAdminParams(el, attr); +/*if (depth == 2 && parsingAnswer) + ParseAdminParams(el, attr);*/ return 0; } @@ -101,15 +101,23 @@ if (strcasecmp(el, "admin") == 0) error = "Admin not found."; } else + { parsingAnswer = true; + for (const char ** pos = attr; *pos != NULL; pos = pos + 2) + if (!TryParse(propertyParsers, ToLower(*pos), pos, *pos)) + { + error = "Invalid parameter."; + break; + } + } } else parsingAnswer = true; } } //----------------------------------------------------------------------------- -void GET_ADMIN::PARSER::ParseAdminParams(const char * el, const char ** attr) +/*void GET_ADMIN::PARSER::ParseAdminParams(const char * el, const char ** attr) { if (!TryParse(propertyParsers, ToLower(el), attr)) error = "Invalid parameter."; -} +}*/ diff --git a/stglibs/srvconf.lib/parsers/get_admin.h b/stglibs/srvconf.lib/parsers/get_admin.h index d8e2fe9e..0e6eee73 100644 --- a/stglibs/srvconf.lib/parsers/get_admin.h +++ b/stglibs/srvconf.lib/parsers/get_admin.h @@ -54,7 +54,7 @@ private: std::string error; void ParseAdmin(const char * el, const char ** attr); - void ParseAdminParams(const char * el, const char ** attr); + //void ParseAdminParams(const char * el, const char ** attr); }; } // namespace GET_ADMIN diff --git a/stglibs/srvconf.lib/parsers/get_tariff.cpp b/stglibs/srvconf.lib/parsers/get_tariff.cpp index e861843e..eb94441e 100644 --- a/stglibs/srvconf.lib/parsers/get_tariff.cpp +++ b/stglibs/srvconf.lib/parsers/get_tariff.cpp @@ -39,7 +39,7 @@ class AOS_PARSER : public BASE_PROPERTY_PARSER public: typedef bool (* FUNC)(const char **, A &, T A::value_type:: *); AOS_PARSER(A & a, T A::value_type:: * fld, FUNC f) : array(a), field(fld), func(f) {} - virtual bool Parse(const char ** attr) { return func(attr, array, field); } + virtual bool Parse(const char ** attr, const std::string & /*attrName*/) { return func(attr, array, field); } private: A & array; T A::value_type:: * field; @@ -53,13 +53,13 @@ void AddAOSParser(PROPERTY_PARSERS & parsers, const std::string & name, A & arra parsers.insert(std::make_pair(ToLower(name), new AOS_PARSER(array, field, func))); } -bool GetTimeSpan(const char ** attr, DIRPRICE_DATA & value) +bool GetTimeSpan(const char ** attr, DIRPRICE_DATA & value, const std::string & attrName) { int hb = 0; int mb = 0; int he = 0; int me = 0; -if (CheckValue(attr)) +if (CheckValue(attr, attrName)) if (ParseTariffTimeStr(attr[1], hb, mb, he, me) == 0) { value.hDay = hb; @@ -72,9 +72,9 @@ return false; } template -bool GetTraffType(const char ** attr, T & value) +bool GetTraffType(const char ** attr, T & value, const std::string & attrName) { -if (!CheckValue(attr)) +if (!CheckValue(attr, attrName)) return false; std::string type(attr[1]); if (type == "up") @@ -93,7 +93,7 @@ return true; template bool GetSlashedValue(const char ** attr, A & array, T A::value_type:: * field) { -if (!CheckValue(attr)) +if (!CheckValue(attr, "value")) return false; const char * start = attr[1]; size_t item = 0; diff --git a/stglibs/srvconf.lib/parsers/get_user.cpp b/stglibs/srvconf.lib/parsers/get_user.cpp index 54cfc0f9..cd19d1f6 100644 --- a/stglibs/srvconf.lib/parsers/get_user.cpp +++ b/stglibs/srvconf.lib/parsers/get_user.cpp @@ -34,7 +34,7 @@ namespace STG { template <> -bool GetValue(const char ** attr, GET_USER::STAT & value) +bool GetValue(const char ** attr, GET_USER::STAT & value, const std::string & /*attrName*/) { if (!attr) return false; diff --git a/stglibs/srvconf.lib/parsers/property.cpp b/stglibs/srvconf.lib/parsers/property.cpp index 8a78fe76..7edae520 100644 --- a/stglibs/srvconf.lib/parsers/property.cpp +++ b/stglibs/srvconf.lib/parsers/property.cpp @@ -22,22 +22,22 @@ #include -bool STG::CheckValue(const char ** attr) +bool STG::CheckValue(const char ** attr, const std::string & attrName) { -return attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0; +return attr && attr[0] && attr[1] && strcasecmp(attr[0], attrName.c_str()) == 0; } -bool STG::GetEncodedValue(const char ** attr, std::string & value) +bool STG::GetEncodedValue(const char ** attr, std::string & value, const std::string & attrName) { -if (!CheckValue(attr)) +if (!CheckValue(attr, attrName)) return false; Decode21str(value, attr[1]); return true; } -bool STG::GetIPValue(const char ** attr, uint32_t & value) +bool STG::GetIPValue(const char ** attr, uint32_t & value, const std::string & attrName) { -if (!CheckValue(attr)) +if (!CheckValue(attr, attrName)) return false; std::string ip(attr[1]); value = inet_strington(attr[1]); @@ -46,10 +46,10 @@ if (value == 0 && ip != "0.0.0.0") return true; } -bool STG::TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr) +bool STG::TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr, const std::string & attrName) { PROPERTY_PARSERS::iterator it(parsers.find(name)); if (it != parsers.end()) - return it->second->Parse(attr); + return it->second->Parse(attr, attrName); return true; // Assume that non-existing params are ok. } diff --git a/stglibs/srvconf.lib/parsers/property.h b/stglibs/srvconf.lib/parsers/property.h index 3469b987..59d47817 100644 --- a/stglibs/srvconf.lib/parsers/property.h +++ b/stglibs/srvconf.lib/parsers/property.h @@ -33,16 +33,16 @@ class BASE_PROPERTY_PARSER { public: virtual ~BASE_PROPERTY_PARSER() {} - virtual bool Parse(const char ** attr) = 0; + virtual bool Parse(const char ** attr, const std::string & attrName) = 0; }; template class PROPERTY_PARSER : public BASE_PROPERTY_PARSER { public: - typedef bool (* FUNC)(const char **, T &); + typedef bool (* FUNC)(const char **, T &, const std::string &); PROPERTY_PARSER(T & v, FUNC f) : value(v), func(f) {} - virtual bool Parse(const char ** attr) { return func(attr, value); } + virtual bool Parse(const char ** attr, const std::string & attrName) { return func(attr, value, attrName); } private: T & value; FUNC func; @@ -50,13 +50,13 @@ class PROPERTY_PARSER : public BASE_PROPERTY_PARSER typedef std::map PROPERTY_PARSERS; -bool CheckValue(const char ** attr); +bool CheckValue(const char ** attr, const std::string & attrName); template inline -bool GetValue(const char ** attr, T & value) +bool GetValue(const char ** attr, T & value, const std::string & attrName) { -if (CheckValue(attr)) +if (CheckValue(attr, attrName)) if (str2x(attr[1], value) < 0) return false; return true; @@ -64,9 +64,9 @@ return true; template <> inline -bool GetValue(const char ** attr, std::string & value) +bool GetValue(const char ** attr, std::string & value, const std::string & attrName) { -if (!CheckValue(attr)) +if (!CheckValue(attr, attrName)) return false; value = attr[1]; return true; @@ -74,17 +74,17 @@ return true; template <> inline -bool GetValue(const char ** attr, double & value) +bool GetValue(const char ** attr, double & value, const std::string & attrName) { -if (CheckValue(attr)) +if (CheckValue(attr, attrName)) if (strtodouble2(attr[1], value)) return false; return true; } -bool GetEncodedValue(const char ** attr, std::string & value); +bool GetEncodedValue(const char ** attr, std::string & value, const std::string & attrName); -bool GetIPValue(const char ** attr, uint32_t& value); +bool GetIPValue(const char ** attr, uint32_t& value, const std::string & attrName); template void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER::FUNC & func = GetValue); @@ -96,7 +96,7 @@ void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER(value, func))); } -bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr); +bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr, const std::string & attrName = "value"); } // namespace STG