1 #include "admins_methods.h"
4 #include "stg/common.h"
6 #include "stg/admins.h"
8 #include "stg/admin_conf.h"
10 #include <ostream> // xmlrpc-c devs have missed something :)
12 //------------------------------------------------------------------------------
14 void METHOD_ADMIN_GET::execute(xmlrpc_c::paramList const & paramList,
15 xmlrpc_c::value * const retvalPtr)
17 std::string cookie = paramList.getString(0);
18 std::string login = paramList.getString(1);
19 paramList.verifyEnd(2);
21 std::map<std::string, xmlrpc_c::value> structVal;
24 if (config->GetAdminInfo(cookie, &adminInfo))
26 structVal["result"] = xmlrpc_c::value_boolean(false);
27 *retvalPtr = xmlrpc_c::value_struct(structVal);
33 if (admins->find(login, &admin))
35 structVal["result"] = xmlrpc_c::value_boolean(false);
36 *retvalPtr = xmlrpc_c::value_struct(structVal);
40 structVal["result"] = xmlrpc_c::value_boolean(true);
41 structVal["login"] = xmlrpc_c::value_string(admin->login());
42 structVal["password"] = xmlrpc_c::value_string(admin->password());
44 const auto priv = admin->priv();
46 structVal["user_stat"] = xmlrpc_c::value_boolean(priv.userStat);
47 structVal["user_conf"] = xmlrpc_c::value_boolean(priv.userConf);
48 structVal["user_cash"] = xmlrpc_c::value_boolean(priv.userCash);
49 structVal["user_passwd"] = xmlrpc_c::value_boolean(priv.userPasswd);
50 structVal["user_add_del"] = xmlrpc_c::value_boolean(priv.userAddDel);
51 structVal["admin_chg"] = xmlrpc_c::value_boolean(priv.adminChg);
52 structVal["tariff_chg"] = xmlrpc_c::value_boolean(priv.tariffChg);
54 *retvalPtr = xmlrpc_c::value_struct(structVal);
57 //------------------------------------------------------------------------------
59 void METHOD_ADMIN_ADD::execute(xmlrpc_c::paramList const & paramList,
60 xmlrpc_c::value * const retvalPtr)
62 std::string cookie = paramList.getString(0);
63 std::string login = paramList.getString(1);
64 paramList.verifyEnd(2);
68 if (config->GetAdminInfo(cookie, &adminInfo))
70 printfd(__FILE__, "METHOD_ADMIN_ADD::execute(): 'Not logged or cookie timeout'\n");
71 *retvalPtr = xmlrpc_c::value_boolean(false);
77 if (admins->find(adminInfo.admin, &admin))
79 printfd(__FILE__, "METHOD_ADMIN_ADD::execute(): 'Invalid admin (logged)'\n");
80 *retvalPtr = xmlrpc_c::value_boolean(false);
84 if (admins->add(login, *admin))
86 printfd(__FILE__, "METHOD_ADMIN_ADD::execute(): 'Failed to add admin'\n");
87 *retvalPtr = xmlrpc_c::value_boolean(false);
91 *retvalPtr = xmlrpc_c::value_boolean(true);
94 //------------------------------------------------------------------------------
96 void METHOD_ADMIN_DEL::execute(xmlrpc_c::paramList const & paramList,
97 xmlrpc_c::value * const retvalPtr)
99 std::string cookie = paramList.getString(0);
100 std::string login = paramList.getString(1);
101 paramList.verifyEnd(2);
103 ADMIN_INFO adminInfo;
105 if (config->GetAdminInfo(cookie, &adminInfo))
107 *retvalPtr = xmlrpc_c::value_boolean(false);
113 if (admins->find(adminInfo.admin, &admin))
115 *retvalPtr = xmlrpc_c::value_boolean(false);
119 if (admins->del(login, *admin))
121 *retvalPtr = xmlrpc_c::value_boolean(false);
125 *retvalPtr = xmlrpc_c::value_boolean(true);
128 //------------------------------------------------------------------------------
130 void METHOD_ADMIN_CHG::execute(xmlrpc_c::paramList const & paramList,
131 xmlrpc_c::value * const retvalPtr)
133 std::string cookie = paramList.getString(0);
134 std::string login = paramList.getString(1);
135 xmlrpc_c::value_struct info(paramList.getStruct(2));
136 paramList.verifyEnd(3);
138 ADMIN_INFO adminInfo;
140 if (config->GetAdminInfo(cookie, &adminInfo))
142 *retvalPtr = xmlrpc_c::value_boolean(false);
146 STG::Admin * loggedAdmin;
148 if (admins->find(adminInfo.admin, &loggedAdmin))
150 *retvalPtr = xmlrpc_c::value_boolean(false);
156 if (admins->find(login, &admin))
158 *retvalPtr = xmlrpc_c::value_boolean(false);
164 conf.priv = admin->priv();
165 conf.password = admin->password();
168 std::map<std::string, xmlrpc_c::value> structVal = info;
170 std::map<std::string, xmlrpc_c::value>::iterator it;
172 if ((it = structVal.find("password")) != structVal.end())
174 conf.password = xmlrpc_c::value_string(it->second);
177 if ((it = structVal.find("user_stat")) != structVal.end())
179 conf.priv.userStat = xmlrpc_c::value_boolean(it->second);
182 if ((it = structVal.find("user_conf")) != structVal.end())
184 conf.priv.userConf = xmlrpc_c::value_boolean(it->second);
187 if ((it = structVal.find("user_cash")) != structVal.end())
189 conf.priv.userCash = xmlrpc_c::value_boolean(it->second);
192 if ((it = structVal.find("user_passwd")) != structVal.end())
194 conf.priv.userPasswd = xmlrpc_c::value_boolean(it->second);
197 if ((it = structVal.find("user_add_del")) != structVal.end())
199 conf.priv.userAddDel = xmlrpc_c::value_boolean(it->second);
202 if ((it = structVal.find("admin_chg")) != structVal.end())
204 conf.priv.adminChg = xmlrpc_c::value_boolean(it->second);
207 if ((it = structVal.find("tariff_chg")) != structVal.end())
209 conf.priv.tariffChg = xmlrpc_c::value_boolean(it->second);
212 if (admins->change(conf, *loggedAdmin))
214 *retvalPtr = xmlrpc_c::value_boolean(false);
217 *retvalPtr = xmlrpc_c::value_boolean(true);
220 //------------------------------------------------------------------------------
222 void METHOD_ADMINS_GET::execute(xmlrpc_c::paramList const & paramList,
223 xmlrpc_c::value * const retvalPtr)
225 std::string cookie = paramList.getString(0);
226 paramList.verifyEnd(1);
228 std::map<std::string, xmlrpc_c::value> mainStructVal;
229 std::vector<xmlrpc_c::value> retval;
230 ADMIN_INFO adminInfo;
232 if (config->GetAdminInfo(cookie, &adminInfo))
234 mainStructVal["result"] = xmlrpc_c::value_boolean(false);
235 *retvalPtr = xmlrpc_c::value_struct(mainStructVal);
239 admins->fmap([&retval](const auto& admin)
241 const std::map<std::string, xmlrpc_c::value> structVal{
242 {"result", xmlrpc_c::value_boolean(true)},
243 {"login", xmlrpc_c::value_string(admin.login())},
244 {"password", xmlrpc_c::value_string(admin.password())},
245 {"user_stat", xmlrpc_c::value_boolean(admin.priv().userStat)},
246 {"user_conf", xmlrpc_c::value_boolean(admin.priv().userConf)},
247 {"user_cash", xmlrpc_c::value_boolean(admin.priv().userCash)},
248 {"user_passwd", xmlrpc_c::value_boolean(admin.priv().userPasswd)},
249 {"user_add_del", xmlrpc_c::value_boolean(admin.priv().userAddDel)},
250 {"admin_chg", xmlrpc_c::value_boolean(admin.priv().adminChg)},
251 {"tariff_chg", xmlrpc_c::value_boolean(admin.priv().tariffChg)}
253 retval.push_back(xmlrpc_c::value_struct(structVal));
256 *retvalPtr = xmlrpc_c::value_array(retval);