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.userName.data(),
124 config.userPass.data());
125 return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
128 bool GetAdminFunction(const SGCONF::CONFIG & config,
129 const std::string & arg,
130 const std::map<std::string, std::string> & /*options*/)
132 STG::SERVCONF proto(config.server.data(),
134 config.userName.data(),
135 config.userPass.data());
136 // STG currently doesn't support <GetAdmin login="..."/>.
137 // So get a list of admins and filter it. 'data' param holds a pointer to 'login'.
138 std::string login(arg);
139 return proto.GetAdmins(GetAdminCallback, &login) == STG::st_ok;
142 bool DelAdminFunction(const SGCONF::CONFIG & config,
143 const std::string & arg,
144 const std::map<std::string, std::string> & /*options*/)
146 STG::SERVCONF proto(config.server.data(),
148 config.userName.data(),
149 config.userPass.data());
150 return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
153 bool AddAdminFunction(const SGCONF::CONFIG & config,
154 const std::string & arg,
155 const std::map<std::string, std::string> & options)
160 SGCONF::MaybeSet(options, "priv", conf.priv, ConvPriv);
161 STG::SERVCONF proto(config.server.data(),
163 config.userName.data(),
164 config.userPass.data());
165 return proto.AddAdmin(arg, conf, SimpleCallback, NULL) == STG::st_ok;
168 bool ChgAdminFunction(const SGCONF::CONFIG & config,
169 const std::string & arg,
170 const std::map<std::string, std::string> & options)
173 std::cerr << "Unimplemented.\n";
177 } // namespace anonymous
179 void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
181 std::vector<API_ACTION::PARAM> params(GetAdminParams());
182 blocks.Add("Admin management options")
183 .Add("get-admins", SGCONF::MakeAPIAction(commands, GetAdminsFunction), "\tget admin list")
184 .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", GetAdminFunction), "get admin")
185 .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", params, AddAdminFunction), "add admin")
186 .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", DelAdminFunction), "del admin")
187 .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", params, ChgAdminFunction), "change admin");