3 #include "users_methods.h"
8 #include "../../../user_property.h"
10 #include "rpcconfig.h"
11 #include "user_helper.h"
16 //------------------------------------------------------------------------------
18 void METHOD_USER_GET::execute(xmlrpc_c::paramList const & paramList,
19 xmlrpc_c::value * const retvalPtr)
21 std::string cookie = paramList.getString(0);
22 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);
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);
64 paramList.verifyEnd(2);
68 if (config->GetAdminInfo(cookie, &adminInfo))
70 *retvalPtr = xmlrpc_c::value_boolean(false);
76 if (admins->FindAdmin(adminInfo.admin, &admin))
78 *retvalPtr = xmlrpc_c::value_boolean(false);
84 if (users->FindByName(login, &u))
86 if (users->Add(login, admin))
88 *retvalPtr = xmlrpc_c::value_boolean(false);
92 *retvalPtr = xmlrpc_c::value_boolean(true);
96 *retvalPtr = xmlrpc_c::value_boolean(false);
100 //------------------------------------------------------------------------------
102 void METHOD_USER_DEL::execute(xmlrpc_c::paramList const & paramList,
103 xmlrpc_c::value * const retvalPtr)
105 std::string cookie = paramList.getString(0);
106 std::string login = paramList.getString(1);
108 paramList.verifyEnd(2);
110 ADMIN_INFO adminInfo;
112 if (config->GetAdminInfo(cookie, &adminInfo))
114 *retvalPtr = xmlrpc_c::value_boolean(false);
120 if (admins->FindAdmin(adminInfo.admin, &admin))
122 *retvalPtr = xmlrpc_c::value_boolean(false);
128 if (users->FindByName(login, &u))
130 users->Del(login, admin);
131 *retvalPtr = xmlrpc_c::value_boolean(true);
135 *retvalPtr = xmlrpc_c::value_boolean(false);
139 //------------------------------------------------------------------------------
141 void METHOD_USERS_GET::execute(xmlrpc_c::paramList const & paramList,
142 xmlrpc_c::value * const retvalPtr)
144 std::string cookie = paramList.getString(0);
146 paramList.verifyEnd(1);
148 std::map<std::string, xmlrpc_c::value> structVal;
149 std::vector<xmlrpc_c::value> retval;
150 ADMIN_INFO adminInfo;
152 if (config->GetAdminInfo(cookie, &adminInfo))
154 structVal["result"] = xmlrpc_c::value_boolean(false);
155 *retvalPtr = xmlrpc_c::value_struct(structVal);
159 bool hidePassword = !adminInfo.priviledges.userConf ||
160 !adminInfo.priviledges.userPasswd;
164 int h = users->OpenSearch();
167 printfd(__FILE__, "users->OpenSearch() error\n");
168 users->CloseSearch(h);
174 if (users->SearchNext(h, &u))
179 xmlrpc_c::value info;
181 USER_HELPER uhelper(u);
183 uhelper.GetUserInfo(&info, hidePassword);
185 retval.push_back(info);
188 *retvalPtr = xmlrpc_c::value_array(retval);
191 //------------------------------------------------------------------------------
193 void METHOD_USER_CHG::execute(xmlrpc_c::paramList const & paramList,
194 xmlrpc_c::value * const retvalPtr)
196 std::string cookie = paramList.getString(0);
197 std::string login = paramList.getString(1);
198 xmlrpc_c::value_struct info(paramList.getStruct(2));
200 paramList.verifyEnd(3);
202 ADMIN_INFO adminInfo;
204 if (config->GetAdminInfo(cookie, &adminInfo))
206 *retvalPtr = xmlrpc_c::value_boolean(false);
212 if (admins->FindAdmin(adminInfo.admin, &admin))
214 *retvalPtr = xmlrpc_c::value_boolean(false);
220 if (users->FindByName(login, &u))
222 *retvalPtr = xmlrpc_c::value_boolean(false);
226 USER_HELPER uhelper(u);
228 if (!adminInfo.priviledges.userConf || !adminInfo.priviledges.userPasswd)
230 uhelper.SetUserInfo(info, admin, login, *store, tariffs);
234 uhelper.SetUserInfo(info, admin, login, *store, tariffs);
240 *retvalPtr = xmlrpc_c::value_boolean(true);
243 //------------------------------------------------------------------------------
245 void METHOD_USER_CASH_ADD::execute(xmlrpc_c::paramList const & paramList,
246 xmlrpc_c::value * const retvalPtr)
248 std::string cookie = paramList.getString(0);
249 std::string login = paramList.getString(1);
250 double amount = paramList.getDouble(2);
251 std::string comment = IconvString(paramList.getString(3), "UTF-8", "KOI8-R");
253 paramList.verifyEnd(4);
255 ADMIN_INFO adminInfo;
257 if (config->GetAdminInfo(cookie, &adminInfo))
259 *retvalPtr = xmlrpc_c::value_boolean(false);
265 if (admins->FindAdmin(adminInfo.admin, &admin))
267 *retvalPtr = xmlrpc_c::value_boolean(false);
273 if (users->FindByName(login, &u))
275 *retvalPtr = xmlrpc_c::value_boolean(false);
279 double cash = u->GetProperty().cash.Get();
282 if (!u->GetProperty().cash.Set(cash, admin, login, store, comment))
284 *retvalPtr = xmlrpc_c::value_boolean(false);
290 *retvalPtr = xmlrpc_c::value_boolean(true);
293 //------------------------------------------------------------------------------
295 void METHOD_USER_CASH_SET::execute(xmlrpc_c::paramList const & paramList,
296 xmlrpc_c::value * const retvalPtr)
298 std::string cookie = paramList.getString(0);
299 std::string login = paramList.getString(1);
300 double cash = paramList.getDouble(2);
301 std::string comment = IconvString(paramList.getString(3), "UTF-8", "KOI8-R");
303 paramList.verifyEnd(4);
305 ADMIN_INFO adminInfo;
307 if (config->GetAdminInfo(cookie, &adminInfo))
309 *retvalPtr = xmlrpc_c::value_boolean(false);
315 if (admins->FindAdmin(adminInfo.admin, &admin))
317 *retvalPtr = xmlrpc_c::value_boolean(false);
323 if (users->FindByName(login, &u))
325 *retvalPtr = xmlrpc_c::value_boolean(false);
329 if (!u->GetProperty().cash.Set(cash, admin, login, store, comment))
331 *retvalPtr = xmlrpc_c::value_boolean(false);
337 *retvalPtr = xmlrpc_c::value_boolean(true);
340 //------------------------------------------------------------------------------
342 void METHOD_USER_TARIFF_CHANGE::execute(xmlrpc_c::paramList const & paramList,
343 xmlrpc_c::value * const retvalPtr)
345 std::string cookie = paramList.getString(0);
346 std::string login = paramList.getString(1);
347 std::string tariff = paramList.getString(2);
348 bool delayed = paramList.getBoolean(3);
349 std::string comment = IconvString(paramList.getString(4), "UTF-8", "KOI8-R");
351 paramList.verifyEnd(5);
353 ADMIN_INFO adminInfo;
355 if (config->GetAdminInfo(cookie, &adminInfo))
357 *retvalPtr = xmlrpc_c::value_boolean(false);
363 if (admins->FindAdmin(adminInfo.admin, &admin))
365 *retvalPtr = xmlrpc_c::value_boolean(false);
371 if (users->FindByName(login, &u))
373 *retvalPtr = xmlrpc_c::value_boolean(false);
377 if (tariffs->FindByName(tariff))
381 if (u->GetProperty().nextTariff.Set(tariff,
387 *retvalPtr = xmlrpc_c::value_boolean(true);
393 if (u->GetProperty().tariffName.Set(tariff,
399 *retvalPtr = xmlrpc_c::value_boolean(true);
405 *retvalPtr = xmlrpc_c::value_boolean(false);
408 //------------------------------------------------------------------------------
410 void METHOD_GET_ONLINE_IPS::execute(xmlrpc_c::paramList const & paramList,
411 xmlrpc_c::value * const retvalPtr)
413 std::string cookie = paramList.getString(0);
414 std::vector<xmlrpc_c::value> subnetsStr = paramList.getArray(1);
415 paramList.verifyEnd(2);
417 std::vector<IP_MASK> subnets;
419 std::vector<xmlrpc_c::value>::iterator it;
421 for (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);
444 std::vector<xmlrpc_c::value> ips;
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 std::vector<IP_MASK>::iterator it;
468 for (it = subnets.begin(); it != subnets.end(); ++it)
470 if ((it->ip & it->mask) == (ip & it->mask))
472 ips.push_back(xmlrpc_c::value_string(inet_ntostring(u->GetCurrIP())));
479 structVal["ips"] = xmlrpc_c::value_array(ips);
481 *retvalPtr = xmlrpc_c::value_struct(structVal);
484 bool METHOD_GET_ONLINE_IPS::ParseNet(const std::string & net, IP_MASK & ipm) const
486 size_t pos = net.find_first_of('/');
488 if (pos == std::string::npos)
490 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Network address is not in CIDR-notation\n");
494 int res = inet_pton(AF_INET, net.substr(0, pos).c_str(), &ipm.ip);
498 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): '%s'\n", strerror(errno));
503 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Invalid network address\n", strerror(errno));
507 if (str2x(net.substr(pos + 1, net.length() - pos - 1), ipm.mask))
509 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Invalid network mask\n");
514 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Network mask is out of range\n");
517 ipm.mask = htonl(0xffFFffFF << (32 - ipm.mask));