3 #include "api_action.h"
8 #include "stg/servconf.h"
9 #include "stg/servconf_types.h"
10 #include "stg/os_int.h"
20 std::string Indent(size_t level, bool dash = false)
24 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
27 std::string PrivToString(const PRIV& priv)
29 return std::string("") +
30 (priv.corpChg ? "1" : "0") +
31 (priv.serviceChg ? "1" : "0") +
32 (priv.tariffChg ? "1" : "0") +
33 (priv.adminChg ? "1" : "0") +
34 (priv.userAddDel ? "1" : "0") +
35 (priv.userPasswd ? "1" : "0") +
36 (priv.userCash ? "1" : "0") +
37 (priv.userConf ? "1" : "0") +
38 (priv.userStat ? "1" : "0");
41 void PrintAdmin(const STG::GET_ADMIN::INFO & info, size_t level = 0)
43 std::cout << Indent(level, true) << "login: " << info.login << "\n"
44 << Indent(level) << "priviledges: " << PrivToString(info.priv) << "\n";
47 std::vector<SGCONF::API_ACTION::PARAM> GetAdminParams()
49 std::vector<SGCONF::API_ACTION::PARAM> params;
50 params.push_back(SGCONF::API_ACTION::PARAM("password", "<password>", "password"));
51 params.push_back(SGCONF::API_ACTION::PARAM("priv", "<priv>", "priviledges"));
55 void ConvPriv(const std::string & value, RESETABLE<PRIV> & res)
57 if (value.length() != 9)
58 throw SGCONF::ACTION::ERROR("Priviledges value should be a 9-digits length binary number.");
60 priv.corpChg = (value[0] == '0' ? 0 : 1);
61 priv.serviceChg = (value[1] == '0' ? 0 : 1);
62 priv.tariffChg = (value[2] == '0' ? 0 : 1);
63 priv.adminChg = (value[3] == '0' ? 0 : 1);
64 priv.userAddDel = (value[4] == '0' ? 0 : 1);
65 priv.userPasswd = (value[5] == '0' ? 0 : 1);
66 priv.userCash = (value[6] == '0' ? 0 : 1);
67 priv.userConf = (value[7] == '0' ? 0 : 1);
68 priv.userStat = (value[8] == '0' ? 0 : 1);
72 void SimpleCallback(bool result,
73 const std::string & reason,
78 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
81 std::cout << "Success.\n";
84 void GetAdminsCallback(bool result,
85 const std::string & reason,
86 const std::vector<STG::GET_ADMIN::INFO> & info,
91 std::cerr << "Failed to get admin list. Reason: '" << reason << "'." << std::endl;
94 std::cout << "Admins:\n";
95 for (size_t i = 0; i < info.size(); ++i)
96 PrintAdmin(info[i], 1);
99 void GetAdminCallback(bool result,
100 const std::string & reason,
101 const std::vector<STG::GET_ADMIN::INFO> & info,
104 assert(data != NULL && "Expecting pointer to std::string with the admin's login.");
105 const std::string & login = *static_cast<const std::string *>(data);
108 std::cerr << "Failed to get admin. Reason: '" << reason << "'." << std::endl;
111 for (size_t i = 0; i < info.size(); ++i)
112 if (info[i].login == login)
117 bool GetAdminsFunction(const SGCONF::CONFIG & config,
118 const std::string & /*arg*/,
119 const std::map<std::string, std::string> & /*options*/)
121 STG::SERVCONF proto(config.server.data(),
123 config.localAddress.data(),
124 config.localPort.data(),
125 config.userName.data(),
126 config.userPass.data());
127 return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
130 bool GetAdminFunction(const SGCONF::CONFIG & config,
131 const std::string & arg,
132 const std::map<std::string, std::string> & /*options*/)
134 STG::SERVCONF proto(config.server.data(),
136 config.localAddress.data(),
137 config.localPort.data(),
138 config.userName.data(),
139 config.userPass.data());
140 // STG currently doesn't support <GetAdmin login="..."/>.
141 // So get a list of admins and filter it. 'data' param holds a pointer to 'login'.
142 std::string login(arg);
143 return proto.GetAdmins(GetAdminCallback, &login) == STG::st_ok;
146 bool DelAdminFunction(const SGCONF::CONFIG & config,
147 const std::string & arg,
148 const std::map<std::string, std::string> & /*options*/)
150 STG::SERVCONF proto(config.server.data(),
152 config.localAddress.data(),
153 config.localPort.data(),
154 config.userName.data(),
155 config.userPass.data());
156 return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
159 bool AddAdminFunction(const SGCONF::CONFIG & config,
160 const std::string & arg,
161 const std::map<std::string, std::string> & options)
165 SGCONF::MaybeSet(options, "priv", conf.priv, ConvPriv);
166 SGCONF::MaybeSet(options, "password", conf.password);
167 STG::SERVCONF proto(config.server.data(),
169 config.localAddress.data(),
170 config.localPort.data(),
171 config.userName.data(),
172 config.userPass.data());
173 return proto.AddAdmin(arg, conf, SimpleCallback, NULL) == STG::st_ok;
176 bool ChgAdminFunction(const SGCONF::CONFIG & config,
177 const std::string & arg,
178 const std::map<std::string, std::string> & options)
182 SGCONF::MaybeSet(options, "priv", conf.priv, ConvPriv);
183 SGCONF::MaybeSet(options, "password", conf.password);
184 STG::SERVCONF proto(config.server.data(),
186 config.localAddress.data(),
187 config.localPort.data(),
188 config.userName.data(),
189 config.userPass.data());
190 return proto.ChgAdmin(conf, SimpleCallback, NULL) == STG::st_ok;
193 } // namespace anonymous
195 void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
197 std::vector<API_ACTION::PARAM> params(GetAdminParams());
198 blocks.Add("Admin management options")
199 .Add("get-admins", SGCONF::MakeAPIAction(commands, GetAdminsFunction), "\tget admin list")
200 .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", GetAdminFunction), "get admin")
201 .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", params, AddAdminFunction), "add admin")
202 .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", DelAdminFunction), "del admin")
203 .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", params, ChgAdminFunction), "change admin");