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 const TARIFF * newTariff = tariffs->FindByName(tariff);
387 const TARIFF * currentTariff = u->GetTariff();
388 std::string message = currentTariff->TariffChangeIsAllowed(*newTariff);
391 if (u->GetProperty().tariffName.Set(tariff,
397 u->ResetNextTariff();
399 *retvalPtr = xmlrpc_c::value_boolean(true);
405 GetStgLogger()("Tariff change is prohibited for user %s. %s", u->GetLogin().c_str(), message.c_str());
411 *retvalPtr = xmlrpc_c::value_boolean(false);
414 //------------------------------------------------------------------------------
416 void METHOD_GET_ONLINE_IPS::execute(xmlrpc_c::paramList const & paramList,
417 xmlrpc_c::value * const retvalPtr)
419 std::string cookie = paramList.getString(0);
420 typedef std::vector<xmlrpc_c::value> ValueVector;
421 ValueVector subnetsStr = paramList.getArray(1);
422 paramList.verifyEnd(2);
424 std::vector<IP_MASK> subnets;
426 for (ValueVector::const_iterator it(subnetsStr.begin()); it != subnetsStr.end(); ++it)
429 if (ParseNet(xmlrpc_c::value_string(*it), ipm))
431 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::execute(): Failed to parse subnet ('%s')\n", std::string(xmlrpc_c::value_string(*it)).c_str());
435 subnets.push_back(ipm);
439 std::map<std::string, xmlrpc_c::value> structVal;
440 ADMIN_INFO adminInfo;
442 if (config->GetAdminInfo(cookie, &adminInfo))
444 structVal["result"] = xmlrpc_c::value_boolean(false);
445 *retvalPtr = xmlrpc_c::value_struct(structVal);
453 int handle = users->OpenSearch();
456 printfd(__FILE__, "users->OpenSearch() error\n");
457 users->CloseSearch(handle);
463 if (users->SearchNext(handle, &u))
468 if (u->GetAuthorized())
470 uint32_t ip = u->GetCurrIP();
472 for (std::vector<IP_MASK>::const_iterator it(subnets.begin()); it != subnets.end(); ++it)
474 if ((it->ip & it->mask) == (ip & it->mask))
476 ips.push_back(xmlrpc_c::value_string(inet_ntostring(u->GetCurrIP())));
483 structVal["ips"] = xmlrpc_c::value_array(ips);
485 *retvalPtr = xmlrpc_c::value_struct(structVal);
488 bool METHOD_GET_ONLINE_IPS::ParseNet(const std::string & net, IP_MASK & ipm) const
490 size_t pos = net.find_first_of('/');
492 if (pos == std::string::npos)
494 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Network address is not in CIDR-notation\n");
498 int res = inet_pton(AF_INET, net.substr(0, pos).c_str(), &ipm.ip);
502 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): '%s'\n", strerror(errno));
507 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Invalid network address\n", strerror(errno));
511 if (str2x(net.substr(pos + 1, net.length() - pos - 1), ipm.mask))
513 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Invalid network mask\n");
518 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Network mask is out of range\n");
521 ipm.mask = htonl(0xffFFffFF << (32 - ipm.mask));
526 void METHOD_GET_USER_AUTH_BY::execute(xmlrpc_c::paramList const & paramList,
527 xmlrpc_c::value * const retvalPtr)
529 std::string cookie = paramList.getString(0);
530 std::string login = paramList.getString(1);
531 paramList.verifyEnd(2);
533 std::map<std::string, xmlrpc_c::value> structVal;
534 ADMIN_INFO adminInfo;
536 if (config->GetAdminInfo(cookie, &adminInfo))
538 structVal["result"] = xmlrpc_c::value_boolean(false);
539 *retvalPtr = xmlrpc_c::value_struct(structVal);
545 if (users->FindByName(login, &u))
547 structVal["result"] = xmlrpc_c::value_boolean(false);
548 *retvalPtr = xmlrpc_c::value_struct(structVal);
552 std::vector<std::string> list(u->GetAuthorizers());
553 std::vector<xmlrpc_c::value> authList;
554 for (std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it)
555 authList.push_back(xmlrpc_c::value_string(*it));
556 *retvalPtr = xmlrpc_c::value_array(authList);