X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/85513928f6a270af94c1477f5ae2773647b04cd7..08dd72f2d8d3d7766e4fa87f01840c3ed8211091:/projects/sgconf/admins.cpp?ds=inline diff --git a/projects/sgconf/admins.cpp b/projects/sgconf/admins.cpp index da404bf3..c0700e31 100644 --- a/projects/sgconf/admins.cpp +++ b/projects/sgconf/admins.cpp @@ -1,12 +1,19 @@ #include "admins.h" +#include "api_action.h" +#include "options.h" +#include "makeproto.h" #include "config.h" +#include "utils.h" #include "stg/servconf.h" #include "stg/servconf_types.h" -#include "stg/os_int.h" +#include "stg/admin_conf.h" #include +#include +#include +#include #include namespace @@ -19,7 +26,7 @@ if (level == 0) return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' '); } -std::string PrivToString(const PRIV& priv) +std::string PrivToString(const STG::Priv& priv) { return std::string("") + (priv.corpChg ? "1" : "0") + @@ -33,12 +40,37 @@ return std::string("") + (priv.userStat ? "1" : "0"); } -void PrintAdmin(const STG::GET_ADMIN::INFO & info, size_t level = 0) +void PrintAdmin(const STG::GetAdmin::Info & info, size_t level = 0) { std::cout << Indent(level, true) << "login: " << info.login << "\n" << Indent(level) << "priviledges: " << PrivToString(info.priv) << "\n"; } +std::vector GetAdminParams() +{ +std::vector params; +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, std::optional & res) +{ +if (value.length() != 9) + throw SGCONF::ACTION::ERROR("Priviledges value should be a 9-digits length binary number."); +STG::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*/) @@ -53,7 +85,7 @@ std::cout << "Success.\n"; void GetAdminsCallback(bool result, const std::string & reason, - const std::vector & info, + const std::vector & info, void * /*data*/) { if (!result) @@ -68,7 +100,7 @@ for (size_t i = 0; i < info.size(); ++i) void GetAdminCallback(bool result, const std::string & reason, - const std::vector & info, + const std::vector & info, void * data) { assert(data != NULL && "Expecting pointer to std::string with the admin's login."); @@ -83,59 +115,62 @@ for (size_t i = 0; i < info.size(); ++i) PrintAdmin(info[i]); } -} // namespace anonymous - -bool SGCONF::GetAdminsFunction(const SGCONF::CONFIG & config, - const std::string & /*arg*/, - const std::map & /*options*/) +bool 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; +return makeProto(config).GetAdmins(GetAdminsCallback, NULL) == STG::st_ok; } -bool SGCONF::GetAdminFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) +bool 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; +return makeProto(config).GetAdmins(GetAdminCallback, &login) == STG::st_ok; +} + +bool DelAdminFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & /*options*/) +{ +return makeProto(config).DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok; } -bool SGCONF::DelAdminFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) +bool AddAdminFunction(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; +STG::AdminConfOpt conf; +conf.login = arg; +SGCONF::MaybeSet(options, "priv", conf.priv, ConvPriv); +SGCONF::MaybeSet(options, "password", conf.password); +return makeProto(config).AddAdmin(arg, conf, SimpleCallback, NULL) == STG::st_ok; } -bool SGCONF::AddAdminFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & /*options*/) +bool ChgAdminFunction(const SGCONF::CONFIG & config, + const std::string & arg, + const std::map & options) { -// TODO -std::cerr << "Unimplemented.\n"; -return false; +STG::AdminConfOpt conf; +conf.login = arg; +SGCONF::MaybeSet(options, "priv", conf.priv, ConvPriv); +SGCONF::MaybeSet(options, "password", conf.password); +return makeProto(config).ChgAdmin(conf, SimpleCallback, NULL) == STG::st_ok; } -bool SGCONF::ChgAdminFunction(const SGCONF::CONFIG & config, - const std::string & arg, - const std::map & options) +} // namespace anonymous + +void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks) { -// TODO -std::cerr << "Unimplemented.\n"; -return false; +std::vector params(GetAdminParams()); +blocks.Add("Admin management options") + .Add("get-admins", SGCONF::MakeAPIAction(commands, GetAdminsFunction), "\tget admin list") + .Add("get-admin", SGCONF::MakeAPIAction(commands, "", GetAdminFunction), "get admin") + .Add("add-admin", SGCONF::MakeAPIAction(commands, "", params, AddAdminFunction), "add admin") + .Add("del-admin", SGCONF::MakeAPIAction(commands, "", DelAdminFunction), "del admin") + .Add("chg-admin", SGCONF::MakeAPIAction(commands, "", params, ChgAdminFunction), "change admin"); }