4 #include "stg/admins.h"
5 #include "stg/tariffs.h"
6 #include "stg/user_ips.h"
7 #include "stg/common.h"
8 #include "stg/user_property.h"
10 #include "users_methods.h"
11 #include "rpcconfig.h"
12 #include "user_helper.h"
14 //------------------------------------------------------------------------------
16 void METHOD_USER_GET::execute(xmlrpc_c::paramList const & paramList,
17 xmlrpc_c::value * const retvalPtr)
19 std::string cookie = paramList.getString(0);
20 std::string login = paramList.getString(1);
21 paramList.verifyEnd(2);
23 std::map<std::string, xmlrpc_c::value> structVal;
26 if (config->GetAdminInfo(cookie, &adminInfo))
28 structVal["result"] = xmlrpc_c::value_boolean(false);
29 *retvalPtr = xmlrpc_c::value_struct(structVal);
35 if (users->FindByName(login, &u))
37 structVal["result"] = xmlrpc_c::value_boolean(false);
38 *retvalPtr = xmlrpc_c::value_struct(structVal);
42 USER_HELPER uhelper(u, *users);
44 if (!adminInfo.priviledges.userConf || !adminInfo.priviledges.userPasswd)
46 uhelper.GetUserInfo(retvalPtr, true);
50 uhelper.GetUserInfo(retvalPtr);
53 //------------------------------------------------------------------------------
55 void METHOD_USER_ADD::execute(xmlrpc_c::paramList const & paramList,
56 xmlrpc_c::value * const retvalPtr)
58 std::string cookie = paramList.getString(0);
59 std::string login = paramList.getString(1);
60 paramList.verifyEnd(2);
64 if (config->GetAdminInfo(cookie, &adminInfo))
66 *retvalPtr = xmlrpc_c::value_boolean(false);
72 if (admins->Find(adminInfo.admin, &admin))
74 *retvalPtr = xmlrpc_c::value_boolean(false);
80 if (users->FindByName(login, &u))
82 if (users->Add(login, admin))
84 *retvalPtr = xmlrpc_c::value_boolean(false);
88 *retvalPtr = xmlrpc_c::value_boolean(true);
92 *retvalPtr = xmlrpc_c::value_boolean(false);
96 //------------------------------------------------------------------------------
98 void METHOD_USER_DEL::execute(xmlrpc_c::paramList const & paramList,
99 xmlrpc_c::value * const retvalPtr)
101 std::string cookie = paramList.getString(0);
102 std::string login = paramList.getString(1);
103 paramList.verifyEnd(2);
105 ADMIN_INFO adminInfo;
107 if (config->GetAdminInfo(cookie, &adminInfo))
109 *retvalPtr = xmlrpc_c::value_boolean(false);
115 if (admins->Find(adminInfo.admin, &admin))
117 *retvalPtr = xmlrpc_c::value_boolean(false);
123 if (!users->FindByName(login, &u))
125 users->Del(login, admin);
126 *retvalPtr = xmlrpc_c::value_boolean(true);
130 *retvalPtr = xmlrpc_c::value_boolean(false);
134 //------------------------------------------------------------------------------
136 void METHOD_USERS_GET::execute(xmlrpc_c::paramList const & paramList,
137 xmlrpc_c::value * const retvalPtr)
139 std::string cookie = paramList.getString(0);
140 paramList.verifyEnd(1);
142 std::map<std::string, xmlrpc_c::value> structVal;
143 std::vector<xmlrpc_c::value> retval;
144 ADMIN_INFO adminInfo;
146 if (config->GetAdminInfo(cookie, &adminInfo))
148 structVal["result"] = xmlrpc_c::value_boolean(false);
149 *retvalPtr = xmlrpc_c::value_struct(structVal);
153 bool hidePassword = !adminInfo.priviledges.userConf ||
154 !adminInfo.priviledges.userPasswd;
158 int h = users->OpenSearch();
161 printfd(__FILE__, "users->OpenSearch() error\n");
162 users->CloseSearch(h);
168 if (users->SearchNext(h, &u))
173 xmlrpc_c::value info;
175 USER_HELPER uhelper(u, *users);
177 uhelper.GetUserInfo(&info, hidePassword);
179 retval.push_back(info);
182 *retvalPtr = xmlrpc_c::value_array(retval);
185 //------------------------------------------------------------------------------
187 void METHOD_USER_CHG::execute(xmlrpc_c::paramList const & paramList,
188 xmlrpc_c::value * const retvalPtr)
190 std::string cookie = paramList.getString(0);
191 std::string login = paramList.getString(1);
192 xmlrpc_c::value_struct info(paramList.getStruct(2));
193 paramList.verifyEnd(3);
195 ADMIN_INFO adminInfo;
197 if (config->GetAdminInfo(cookie, &adminInfo))
199 *retvalPtr = xmlrpc_c::value_boolean(false);
205 if (admins->Find(adminInfo.admin, &admin))
207 *retvalPtr = xmlrpc_c::value_boolean(false);
213 if (users->FindByName(login, &u))
215 *retvalPtr = xmlrpc_c::value_boolean(false);
219 USER_HELPER uhelper(u, *users);
221 if (!adminInfo.priviledges.userConf || !adminInfo.priviledges.userPasswd)
223 uhelper.SetUserInfo(info, admin, login, *store, tariffs);
227 uhelper.SetUserInfo(info, admin, login, *store, tariffs);
233 *retvalPtr = xmlrpc_c::value_boolean(true);
236 //------------------------------------------------------------------------------
238 void METHOD_USER_CASH_ADD::execute(xmlrpc_c::paramList const & paramList,
239 xmlrpc_c::value * const retvalPtr)
241 std::string cookie = paramList.getString(0);
242 std::string login = paramList.getString(1);
243 double amount = paramList.getDouble(2);
244 std::string comment = IconvString(paramList.getString(3), "UTF-8", "KOI8-R");
245 paramList.verifyEnd(4);
247 ADMIN_INFO adminInfo;
249 if (config->GetAdminInfo(cookie, &adminInfo))
251 *retvalPtr = xmlrpc_c::value_boolean(false);
257 if (admins->Find(adminInfo.admin, &admin))
259 *retvalPtr = xmlrpc_c::value_boolean(false);
265 if (users->FindByName(login, &u))
267 *retvalPtr = xmlrpc_c::value_boolean(false);
271 double cash = u->GetProperty().cash.Get();
274 if (!u->GetProperty().cash.Set(cash, admin, login, store, comment))
276 *retvalPtr = xmlrpc_c::value_boolean(false);
282 *retvalPtr = xmlrpc_c::value_boolean(true);
285 //------------------------------------------------------------------------------
287 void METHOD_USER_CASH_SET::execute(xmlrpc_c::paramList const & paramList,
288 xmlrpc_c::value * const retvalPtr)
290 std::string cookie = paramList.getString(0);
291 std::string login = paramList.getString(1);
292 double cash = paramList.getDouble(2);
293 std::string comment = IconvString(paramList.getString(3), "UTF-8", "KOI8-R");
294 paramList.verifyEnd(4);
296 ADMIN_INFO adminInfo;
298 if (config->GetAdminInfo(cookie, &adminInfo))
300 *retvalPtr = xmlrpc_c::value_boolean(false);
306 if (admins->Find(adminInfo.admin, &admin))
308 *retvalPtr = xmlrpc_c::value_boolean(false);
314 if (users->FindByName(login, &u))
316 *retvalPtr = xmlrpc_c::value_boolean(false);
320 if (!u->GetProperty().cash.Set(cash, admin, login, store, comment))
322 *retvalPtr = xmlrpc_c::value_boolean(false);
328 *retvalPtr = xmlrpc_c::value_boolean(true);
331 //------------------------------------------------------------------------------
333 void METHOD_USER_TARIFF_CHANGE::execute(xmlrpc_c::paramList const & paramList,
334 xmlrpc_c::value * const retvalPtr)
336 std::string cookie = paramList.getString(0);
337 std::string login = paramList.getString(1);
338 std::string tariff = paramList.getString(2);
339 bool delayed = paramList.getBoolean(3);
340 std::string comment = IconvString(paramList.getString(4), "UTF-8", "KOI8-R");
341 paramList.verifyEnd(5);
343 ADMIN_INFO adminInfo;
345 if (config->GetAdminInfo(cookie, &adminInfo))
347 *retvalPtr = xmlrpc_c::value_boolean(false);
353 if (admins->Find(adminInfo.admin, &admin))
355 *retvalPtr = xmlrpc_c::value_boolean(false);
361 if (users->FindByName(login, &u))
363 *retvalPtr = xmlrpc_c::value_boolean(false);
367 if (tariffs->FindByName(tariff))
371 if (u->GetProperty().nextTariff.Set(tariff,
378 *retvalPtr = xmlrpc_c::value_boolean(true);
384 if (u->GetProperty().tariffName.Set(tariff,
390 u->ResetNextTariff();
392 *retvalPtr = xmlrpc_c::value_boolean(true);
398 *retvalPtr = xmlrpc_c::value_boolean(false);
401 //------------------------------------------------------------------------------
403 void METHOD_GET_ONLINE_IPS::execute(xmlrpc_c::paramList const & paramList,
404 xmlrpc_c::value * const retvalPtr)
406 std::string cookie = paramList.getString(0);
407 typedef std::vector<xmlrpc_c::value> ValueVector;
408 ValueVector subnetsStr = paramList.getArray(1);
409 paramList.verifyEnd(2);
411 std::vector<IP_MASK> subnets;
413 for (ValueVector::const_iterator it(subnetsStr.begin()); it != subnetsStr.end(); ++it)
416 if (ParseNet(xmlrpc_c::value_string(*it), ipm))
418 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::execute(): Failed to parse subnet ('%s')\n", std::string(xmlrpc_c::value_string(*it)).c_str());
422 subnets.push_back(ipm);
426 std::map<std::string, xmlrpc_c::value> structVal;
427 ADMIN_INFO adminInfo;
429 if (config->GetAdminInfo(cookie, &adminInfo))
431 structVal["result"] = xmlrpc_c::value_boolean(false);
432 *retvalPtr = xmlrpc_c::value_struct(structVal);
440 int handle = users->OpenSearch();
443 printfd(__FILE__, "users->OpenSearch() error\n");
444 users->CloseSearch(handle);
450 if (users->SearchNext(handle, &u))
455 if (u->GetAuthorized())
457 uint32_t ip = u->GetCurrIP();
459 for (std::vector<IP_MASK>::const_iterator it(subnets.begin()); it != subnets.end(); ++it)
461 if ((it->ip & it->mask) == (ip & it->mask))
463 ips.push_back(xmlrpc_c::value_string(inet_ntostring(u->GetCurrIP())));
470 structVal["ips"] = xmlrpc_c::value_array(ips);
472 *retvalPtr = xmlrpc_c::value_struct(structVal);
475 bool METHOD_GET_ONLINE_IPS::ParseNet(const std::string & net, IP_MASK & ipm) const
477 size_t pos = net.find_first_of('/');
479 if (pos == std::string::npos)
481 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Network address is not in CIDR-notation\n");
485 int res = inet_pton(AF_INET, net.substr(0, pos).c_str(), &ipm.ip);
489 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): '%s'\n", strerror(errno));
494 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Invalid network address\n", strerror(errno));
498 if (str2x(net.substr(pos + 1, net.length() - pos - 1), ipm.mask))
500 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Invalid network mask\n");
505 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Network mask is out of range\n");
508 ipm.mask = htonl(0xffFFffFF << (32 - ipm.mask));
513 void METHOD_GET_USER_AUTH_BY::execute(xmlrpc_c::paramList const & paramList,
514 xmlrpc_c::value * const retvalPtr)
516 std::string cookie = paramList.getString(0);
517 std::string login = paramList.getString(1);
518 paramList.verifyEnd(2);
520 std::map<std::string, xmlrpc_c::value> structVal;
521 ADMIN_INFO adminInfo;
523 if (config->GetAdminInfo(cookie, &adminInfo))
525 structVal["result"] = xmlrpc_c::value_boolean(false);
526 *retvalPtr = xmlrpc_c::value_struct(structVal);
532 if (users->FindByName(login, &u))
534 structVal["result"] = xmlrpc_c::value_boolean(false);
535 *retvalPtr = xmlrpc_c::value_struct(structVal);
539 std::vector<std::string> list(u->GetAuthorizers());
540 std::vector<xmlrpc_c::value> authList;
541 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
542 authList.push_back(xmlrpc_c::value_string(*it));
543 *retvalPtr = xmlrpc_c::value_array(authList);