3 #include "api_action.h"
9 #include "stg/servconf.h"
10 #include "stg/servconf_types.h"
11 #include "stg/admin_conf.h"
22 std::string Indent(size_t level, bool dash = false)
26 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
29 std::string PrivToString(const STG::Priv& priv)
31 return std::string("") +
32 (priv.corpChg ? "1" : "0") +
33 (priv.serviceChg ? "1" : "0") +
34 (priv.tariffChg ? "1" : "0") +
35 (priv.adminChg ? "1" : "0") +
36 (priv.userAddDel ? "1" : "0") +
37 (priv.userPasswd ? "1" : "0") +
38 (priv.userCash ? "1" : "0") +
39 (priv.userConf ? "1" : "0") +
40 (priv.userStat ? "1" : "0");
43 void PrintAdmin(const STG::GetAdmin::Info & info, size_t level = 0)
45 std::cout << Indent(level, true) << "login: " << info.login << "\n"
46 << Indent(level) << "priviledges: " << PrivToString(info.priv) << "\n";
49 std::vector<SGCONF::API_ACTION::PARAM> GetAdminParams()
51 std::vector<SGCONF::API_ACTION::PARAM> params;
52 params.push_back(SGCONF::API_ACTION::PARAM("password", "<password>", "password"));
53 params.push_back(SGCONF::API_ACTION::PARAM("priv", "<priv>", "priviledges"));
57 void ConvPriv(const std::string & value, std::optional<STG::Priv> & res)
59 if (value.length() != 9)
60 throw SGCONF::ACTION::ERROR("Priviledges value should be a 9-digits length binary number.");
62 priv.corpChg = (value[0] == '0' ? 0 : 1);
63 priv.serviceChg = (value[1] == '0' ? 0 : 1);
64 priv.tariffChg = (value[2] == '0' ? 0 : 1);
65 priv.adminChg = (value[3] == '0' ? 0 : 1);
66 priv.userAddDel = (value[4] == '0' ? 0 : 1);
67 priv.userPasswd = (value[5] == '0' ? 0 : 1);
68 priv.userCash = (value[6] == '0' ? 0 : 1);
69 priv.userConf = (value[7] == '0' ? 0 : 1);
70 priv.userStat = (value[8] == '0' ? 0 : 1);
74 void SimpleCallback(bool result,
75 const std::string & reason,
80 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
83 std::cout << "Success.\n";
86 void GetAdminsCallback(bool result,
87 const std::string & reason,
88 const std::vector<STG::GetAdmin::Info> & info,
93 std::cerr << "Failed to get admin list. Reason: '" << reason << "'." << std::endl;
96 std::cout << "Admins:\n";
97 for (size_t i = 0; i < info.size(); ++i)
98 PrintAdmin(info[i], 1);
101 void GetAdminCallback(bool result,
102 const std::string & reason,
103 const std::vector<STG::GetAdmin::Info> & info,
106 assert(data != NULL && "Expecting pointer to std::string with the admin's login.");
107 const std::string & login = *static_cast<const std::string *>(data);
110 std::cerr << "Failed to get admin. Reason: '" << reason << "'." << std::endl;
113 for (size_t i = 0; i < info.size(); ++i)
114 if (info[i].login == login)
119 bool GetAdminsFunction(const SGCONF::CONFIG & config,
120 const std::string & /*arg*/,
121 const std::map<std::string, std::string> & /*options*/)
123 return makeProto(config).GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
126 bool GetAdminFunction(const SGCONF::CONFIG & config,
127 const std::string & arg,
128 const std::map<std::string, std::string> & /*options*/)
130 // STG currently doesn't support <GetAdmin login="..."/>.
131 // So get a list of admins and filter it. 'data' param holds a pointer to 'login'.
132 std::string login(arg);
133 return makeProto(config).GetAdmins(GetAdminCallback, &login) == STG::st_ok;
136 bool DelAdminFunction(const SGCONF::CONFIG & config,
137 const std::string & arg,
138 const std::map<std::string, std::string> & /*options*/)
140 return makeProto(config).DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
143 bool AddAdminFunction(const SGCONF::CONFIG & config,
144 const std::string & arg,
145 const std::map<std::string, std::string> & options)
147 STG::AdminConfOpt conf;
149 SGCONF::MaybeSet(options, "priv", conf.priv, ConvPriv);
150 SGCONF::MaybeSet(options, "password", conf.password);
151 return makeProto(config).AddAdmin(arg, conf, SimpleCallback, NULL) == STG::st_ok;
154 bool ChgAdminFunction(const SGCONF::CONFIG & config,
155 const std::string & arg,
156 const std::map<std::string, std::string> & options)
158 STG::AdminConfOpt conf;
160 SGCONF::MaybeSet(options, "priv", conf.priv, ConvPriv);
161 SGCONF::MaybeSet(options, "password", conf.password);
162 return makeProto(config).ChgAdmin(conf, SimpleCallback, NULL) == STG::st_ok;
165 } // namespace anonymous
167 void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
169 std::vector<API_ACTION::PARAM> params(GetAdminParams());
170 blocks.Add("Admin management options")
171 .Add("get-admins", SGCONF::MakeAPIAction(commands, GetAdminsFunction), "\tget admin list")
172 .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", GetAdminFunction), "get admin")
173 .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", params, AddAdminFunction), "add admin")
174 .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", DelAdminFunction), "del admin")
175 .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", params, ChgAdminFunction), "change admin");