3 #include "api_action.h"
8 #include "stg/servconf.h"
9 #include "stg/servconf_types.h"
10 #include "stg/admin_conf.h"
21 std::string Indent(size_t level, bool dash = false)
25 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
28 std::string PrivToString(const STG::Priv& priv)
30 return std::string("") +
31 (priv.corpChg ? "1" : "0") +
32 (priv.serviceChg ? "1" : "0") +
33 (priv.tariffChg ? "1" : "0") +
34 (priv.adminChg ? "1" : "0") +
35 (priv.userAddDel ? "1" : "0") +
36 (priv.userPasswd ? "1" : "0") +
37 (priv.userCash ? "1" : "0") +
38 (priv.userConf ? "1" : "0") +
39 (priv.userStat ? "1" : "0");
42 void PrintAdmin(const STG::GetAdmin::Info & info, size_t level = 0)
44 std::cout << Indent(level, true) << "login: " << info.login << "\n"
45 << Indent(level) << "priviledges: " << PrivToString(info.priv) << "\n";
48 std::vector<SGCONF::API_ACTION::PARAM> GetAdminParams()
50 std::vector<SGCONF::API_ACTION::PARAM> params;
51 params.push_back(SGCONF::API_ACTION::PARAM("password", "<password>", "password"));
52 params.push_back(SGCONF::API_ACTION::PARAM("priv", "<priv>", "priviledges"));
56 void ConvPriv(const std::string & value, STG::Optional<STG::Priv> & res)
58 if (value.length() != 9)
59 throw SGCONF::ACTION::ERROR("Priviledges value should be a 9-digits length binary number.");
61 priv.corpChg = (value[0] == '0' ? 0 : 1);
62 priv.serviceChg = (value[1] == '0' ? 0 : 1);
63 priv.tariffChg = (value[2] == '0' ? 0 : 1);
64 priv.adminChg = (value[3] == '0' ? 0 : 1);
65 priv.userAddDel = (value[4] == '0' ? 0 : 1);
66 priv.userPasswd = (value[5] == '0' ? 0 : 1);
67 priv.userCash = (value[6] == '0' ? 0 : 1);
68 priv.userConf = (value[7] == '0' ? 0 : 1);
69 priv.userStat = (value[8] == '0' ? 0 : 1);
73 void SimpleCallback(bool result,
74 const std::string & reason,
79 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
82 std::cout << "Success.\n";
85 void GetAdminsCallback(bool result,
86 const std::string & reason,
87 const std::vector<STG::GetAdmin::Info> & info,
92 std::cerr << "Failed to get admin list. Reason: '" << reason << "'." << std::endl;
95 std::cout << "Admins:\n";
96 for (size_t i = 0; i < info.size(); ++i)
97 PrintAdmin(info[i], 1);
100 void GetAdminCallback(bool result,
101 const std::string & reason,
102 const std::vector<STG::GetAdmin::Info> & info,
105 assert(data != NULL && "Expecting pointer to std::string with the admin's login.");
106 const std::string & login = *static_cast<const std::string *>(data);
109 std::cerr << "Failed to get admin. Reason: '" << reason << "'." << std::endl;
112 for (size_t i = 0; i < info.size(); ++i)
113 if (info[i].login == login)
118 bool GetAdminsFunction(const SGCONF::CONFIG & config,
119 const std::string & /*arg*/,
120 const std::map<std::string, std::string> & /*options*/)
122 STG::ServConf proto(config.server.data(),
124 config.localAddress.data(),
125 config.localPort.data(),
126 config.userName.data(),
127 config.userPass.data());
128 return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
131 bool GetAdminFunction(const SGCONF::CONFIG & config,
132 const std::string & arg,
133 const std::map<std::string, std::string> & /*options*/)
135 STG::ServConf proto(config.server.data(),
137 config.localAddress.data(),
138 config.localPort.data(),
139 config.userName.data(),
140 config.userPass.data());
141 // STG currently doesn't support <GetAdmin login="..."/>.
142 // So get a list of admins and filter it. 'data' param holds a pointer to 'login'.
143 std::string login(arg);
144 return proto.GetAdmins(GetAdminCallback, &login) == STG::st_ok;
147 bool DelAdminFunction(const SGCONF::CONFIG & config,
148 const std::string & arg,
149 const std::map<std::string, std::string> & /*options*/)
151 STG::ServConf proto(config.server.data(),
153 config.localAddress.data(),
154 config.localPort.data(),
155 config.userName.data(),
156 config.userPass.data());
157 return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
160 bool AddAdminFunction(const SGCONF::CONFIG & config,
161 const std::string & arg,
162 const std::map<std::string, std::string> & options)
164 STG::AdminConfOpt conf;
166 SGCONF::MaybeSet(options, "priv", conf.priv, ConvPriv);
167 SGCONF::MaybeSet(options, "password", conf.password);
168 STG::ServConf proto(config.server.data(),
170 config.localAddress.data(),
171 config.localPort.data(),
172 config.userName.data(),
173 config.userPass.data());
174 return proto.AddAdmin(arg, conf, SimpleCallback, NULL) == STG::st_ok;
177 bool ChgAdminFunction(const SGCONF::CONFIG & config,
178 const std::string & arg,
179 const std::map<std::string, std::string> & options)
181 STG::AdminConfOpt conf;
183 SGCONF::MaybeSet(options, "priv", conf.priv, ConvPriv);
184 SGCONF::MaybeSet(options, "password", conf.password);
185 STG::ServConf proto(config.server.data(),
187 config.localAddress.data(),
188 config.localPort.data(),
189 config.userName.data(),
190 config.userPass.data());
191 return proto.ChgAdmin(conf, SimpleCallback, NULL) == STG::st_ok;
194 } // namespace anonymous
196 void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
198 std::vector<API_ACTION::PARAM> params(GetAdminParams());
199 blocks.Add("Admin management options")
200 .Add("get-admins", SGCONF::MakeAPIAction(commands, GetAdminsFunction), "\tget admin list")
201 .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", GetAdminFunction), "get admin")
202 .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", params, AddAdminFunction), "add admin")
203 .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", DelAdminFunction), "del admin")
204 .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", params, ChgAdminFunction), "change admin");