1 #include "users_methods.h"
3 #include "user_helper.h"
6 #include "stg/admins.h"
7 #include "stg/tariffs.h"
8 #include "stg/tariff.h"
10 #include "stg/user_property.h"
11 #include "stg/common.h"
15 using UserPtr = STG::User*;
17 //------------------------------------------------------------------------------
19 void METHOD_USER_GET::execute(xmlrpc_c::paramList const & paramList,
20 xmlrpc_c::value * const retvalPtr)
22 std::string cookie = paramList.getString(0);
23 std::string login = paramList.getString(1);
24 paramList.verifyEnd(2);
26 std::map<std::string, xmlrpc_c::value> structVal;
29 if (config->GetAdminInfo(cookie, &adminInfo))
31 structVal["result"] = xmlrpc_c::value_boolean(false);
32 *retvalPtr = xmlrpc_c::value_struct(structVal);
38 if (users->FindByName(login, &u))
40 structVal["result"] = xmlrpc_c::value_boolean(false);
41 *retvalPtr = xmlrpc_c::value_struct(structVal);
45 USER_HELPER uhelper(u, *users);
47 if (!adminInfo.priviledges.userConf || !adminInfo.priviledges.userPasswd)
49 uhelper.GetUserInfo(retvalPtr, true);
53 uhelper.GetUserInfo(retvalPtr);
56 //------------------------------------------------------------------------------
58 void METHOD_USER_ADD::execute(xmlrpc_c::paramList const & paramList,
59 xmlrpc_c::value * const retvalPtr)
61 std::string cookie = paramList.getString(0);
62 std::string login = paramList.getString(1);
63 paramList.verifyEnd(2);
67 if (config->GetAdminInfo(cookie, &adminInfo))
69 *retvalPtr = xmlrpc_c::value_boolean(false);
73 STG::Admin * admin = NULL;
75 if (admins->find(adminInfo.admin, &admin))
77 *retvalPtr = xmlrpc_c::value_boolean(false);
83 if (users->FindByName(login, &u))
85 if (users->Add(login, admin))
87 *retvalPtr = xmlrpc_c::value_boolean(false);
91 *retvalPtr = xmlrpc_c::value_boolean(true);
95 *retvalPtr = xmlrpc_c::value_boolean(false);
99 //------------------------------------------------------------------------------
101 void METHOD_USER_DEL::execute(xmlrpc_c::paramList const & paramList,
102 xmlrpc_c::value * const retvalPtr)
104 std::string cookie = paramList.getString(0);
105 std::string login = paramList.getString(1);
106 paramList.verifyEnd(2);
108 ADMIN_INFO adminInfo;
110 if (config->GetAdminInfo(cookie, &adminInfo))
112 *retvalPtr = xmlrpc_c::value_boolean(false);
118 if (admins->find(adminInfo.admin, &admin))
120 *retvalPtr = xmlrpc_c::value_boolean(false);
126 if (!users->FindByName(login, &u))
128 users->Del(login, admin);
129 *retvalPtr = xmlrpc_c::value_boolean(true);
133 *retvalPtr = xmlrpc_c::value_boolean(false);
137 //------------------------------------------------------------------------------
139 void METHOD_USERS_GET::execute(xmlrpc_c::paramList const & paramList,
140 xmlrpc_c::value * const retvalPtr)
142 std::string cookie = paramList.getString(0);
143 paramList.verifyEnd(1);
145 std::map<std::string, xmlrpc_c::value> structVal;
146 std::vector<xmlrpc_c::value> retval;
147 ADMIN_INFO adminInfo;
149 if (config->GetAdminInfo(cookie, &adminInfo))
151 structVal["result"] = xmlrpc_c::value_boolean(false);
152 *retvalPtr = xmlrpc_c::value_struct(structVal);
156 bool hidePassword = !adminInfo.priviledges.userConf ||
157 !adminInfo.priviledges.userPasswd;
161 int h = users->OpenSearch();
164 printfd(__FILE__, "users->OpenSearch() error\n");
165 users->CloseSearch(h);
171 if (users->SearchNext(h, &u))
176 xmlrpc_c::value info;
178 USER_HELPER uhelper(u, *users);
180 uhelper.GetUserInfo(&info, hidePassword);
182 retval.push_back(info);
185 *retvalPtr = xmlrpc_c::value_array(retval);
188 //------------------------------------------------------------------------------
190 void METHOD_USER_CHG::execute(xmlrpc_c::paramList const & paramList,
191 xmlrpc_c::value * const retvalPtr)
193 std::string cookie = paramList.getString(0);
194 std::string login = paramList.getString(1);
195 xmlrpc_c::value_struct info(paramList.getStruct(2));
196 paramList.verifyEnd(3);
198 ADMIN_INFO adminInfo;
200 if (config->GetAdminInfo(cookie, &adminInfo))
202 *retvalPtr = xmlrpc_c::value_boolean(false);
208 if (admins->find(adminInfo.admin, &admin))
210 *retvalPtr = xmlrpc_c::value_boolean(false);
216 if (users->FindByName(login, &u))
218 *retvalPtr = xmlrpc_c::value_boolean(false);
222 USER_HELPER uhelper(u, *users);
224 if (!adminInfo.priviledges.userConf || !adminInfo.priviledges.userPasswd)
226 uhelper.SetUserInfo(info, *admin, login, *store, tariffs);
230 uhelper.SetUserInfo(info, *admin, login, *store, tariffs);
236 *retvalPtr = xmlrpc_c::value_boolean(true);
239 //------------------------------------------------------------------------------
241 void METHOD_USER_CASH_ADD::execute(xmlrpc_c::paramList const & paramList,
242 xmlrpc_c::value * const retvalPtr)
244 std::string cookie = paramList.getString(0);
245 std::string login = paramList.getString(1);
246 double amount = paramList.getDouble(2);
247 std::string comment = IconvString(paramList.getString(3), "UTF-8", "KOI8-R");
248 paramList.verifyEnd(4);
250 ADMIN_INFO adminInfo;
252 if (config->GetAdminInfo(cookie, &adminInfo))
254 *retvalPtr = xmlrpc_c::value_boolean(false);
260 if (admins->find(adminInfo.admin, &admin))
262 *retvalPtr = xmlrpc_c::value_boolean(false);
268 if (users->FindByName(login, &u))
270 *retvalPtr = xmlrpc_c::value_boolean(false);
274 double cash = u->GetProperties().cash.Get();
277 if (!u->GetProperties().cash.Set(cash, *admin, login, *store, comment))
279 *retvalPtr = xmlrpc_c::value_boolean(false);
285 *retvalPtr = xmlrpc_c::value_boolean(true);
288 //------------------------------------------------------------------------------
290 void METHOD_USER_CASH_SET::execute(xmlrpc_c::paramList const & paramList,
291 xmlrpc_c::value * const retvalPtr)
293 std::string cookie = paramList.getString(0);
294 std::string login = paramList.getString(1);
295 double cash = paramList.getDouble(2);
296 std::string comment = IconvString(paramList.getString(3), "UTF-8", "KOI8-R");
297 paramList.verifyEnd(4);
299 ADMIN_INFO adminInfo;
301 if (config->GetAdminInfo(cookie, &adminInfo))
303 *retvalPtr = xmlrpc_c::value_boolean(false);
309 if (admins->find(adminInfo.admin, &admin))
311 *retvalPtr = xmlrpc_c::value_boolean(false);
317 if (users->FindByName(login, &u))
319 *retvalPtr = xmlrpc_c::value_boolean(false);
323 if (!u->GetProperties().cash.Set(cash, *admin, login, *store, comment))
325 *retvalPtr = xmlrpc_c::value_boolean(false);
331 *retvalPtr = xmlrpc_c::value_boolean(true);
334 //------------------------------------------------------------------------------
336 void METHOD_USER_TARIFF_CHANGE::execute(xmlrpc_c::paramList const & paramList,
337 xmlrpc_c::value * const retvalPtr)
339 std::string cookie = paramList.getString(0);
340 std::string login = paramList.getString(1);
341 std::string tariff = paramList.getString(2);
342 bool delayed = paramList.getBoolean(3);
343 std::string comment = IconvString(paramList.getString(4), "UTF-8", "KOI8-R");
344 paramList.verifyEnd(5);
346 ADMIN_INFO adminInfo;
348 if (config->GetAdminInfo(cookie, &adminInfo))
350 *retvalPtr = xmlrpc_c::value_boolean(false);
356 if (admins->find(adminInfo.admin, &admin))
358 *retvalPtr = xmlrpc_c::value_boolean(false);
364 if (users->FindByName(login, &u))
366 *retvalPtr = xmlrpc_c::value_boolean(false);
370 if (tariffs->FindByName(tariff))
374 if (u->GetProperties().nextTariff.Set(tariff, *admin, login, *store, comment))
377 *retvalPtr = xmlrpc_c::value_boolean(true);
383 const auto newTariff = tariffs->FindByName(tariff);
386 const auto currentTariff = u->GetTariff();
387 std::string message = currentTariff->TariffChangeIsAllowed(*newTariff, time(NULL));
390 if (u->GetProperties().tariffName.Set(tariff, *admin, login, *store, comment))
392 u->ResetNextTariff();
394 *retvalPtr = xmlrpc_c::value_boolean(true);
400 STG::PluginLogger::get("conf_rpc")("Tariff change is prohibited for user %s. %s", u->GetLogin().c_str(), message.c_str());
406 *retvalPtr = xmlrpc_c::value_boolean(false);
409 //------------------------------------------------------------------------------
411 void METHOD_GET_ONLINE_IPS::execute(xmlrpc_c::paramList const & paramList,
412 xmlrpc_c::value * const retvalPtr)
414 std::string cookie = paramList.getString(0);
415 typedef std::vector<xmlrpc_c::value> ValueVector;
416 ValueVector subnetsStr = paramList.getArray(1);
417 paramList.verifyEnd(2);
419 std::vector<STG::IPMask> subnets;
421 for (ValueVector::const_iterator it(subnetsStr.begin()); it != subnetsStr.end(); ++it)
424 if (ParseNet(xmlrpc_c::value_string(*it), ipm))
426 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::execute(): Failed to parse subnet ('%s')\n", std::string(xmlrpc_c::value_string(*it)).c_str());
430 subnets.push_back(ipm);
434 std::map<std::string, xmlrpc_c::value> structVal;
435 ADMIN_INFO adminInfo;
437 if (config->GetAdminInfo(cookie, &adminInfo))
439 structVal["result"] = xmlrpc_c::value_boolean(false);
440 *retvalPtr = xmlrpc_c::value_struct(structVal);
448 int handle = users->OpenSearch();
451 printfd(__FILE__, "users->OpenSearch() error\n");
452 users->CloseSearch(handle);
458 if (users->SearchNext(handle, &u))
463 if (u->GetAuthorized())
465 uint32_t ip = u->GetCurrIP();
467 for (std::vector<STG::IPMask>::const_iterator it(subnets.begin()); it != subnets.end(); ++it)
469 if ((it->ip & it->mask) == (ip & it->mask))
471 ips.push_back(xmlrpc_c::value_string(inet_ntostring(u->GetCurrIP())));
478 structVal["ips"] = xmlrpc_c::value_array(ips);
480 *retvalPtr = xmlrpc_c::value_struct(structVal);
483 bool METHOD_GET_ONLINE_IPS::ParseNet(const std::string & net, STG::IPMask & ipm) const
485 size_t pos = net.find_first_of('/');
487 if (pos == std::string::npos)
489 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Network address is not in CIDR-notation\n");
493 int res = inet_pton(AF_INET, net.substr(0, pos).c_str(), &ipm.ip);
497 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): '%s'\n", strerror(errno));
502 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Invalid network address\n", strerror(errno));
506 if (str2x(net.substr(pos + 1, net.length() - pos - 1), ipm.mask))
508 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Invalid network mask\n");
513 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Network mask is out of range\n");
516 ipm.mask = htonl(0xffFFffFF << (32 - ipm.mask));
521 void METHOD_GET_USER_AUTH_BY::execute(xmlrpc_c::paramList const & paramList,
522 xmlrpc_c::value * const retvalPtr)
524 std::string cookie = paramList.getString(0);
525 std::string login = paramList.getString(1);
526 paramList.verifyEnd(2);
528 std::map<std::string, xmlrpc_c::value> structVal;
529 ADMIN_INFO adminInfo;
531 if (config->GetAdminInfo(cookie, &adminInfo))
533 structVal["result"] = xmlrpc_c::value_boolean(false);
534 *retvalPtr = xmlrpc_c::value_struct(structVal);
540 if (users->FindByName(login, &u))
542 structVal["result"] = xmlrpc_c::value_boolean(false);
543 *retvalPtr = xmlrpc_c::value_struct(structVal);
547 std::vector<std::string> list(u->GetAuthorizers());
548 std::vector<xmlrpc_c::value> authList;
549 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
550 authList.push_back(xmlrpc_c::value_string(*it));
551 *retvalPtr = xmlrpc_c::value_array(authList);