1 #include "users_methods.h"
3 #include "../../../users.h"
4 #include "../../../admins.h"
5 #include "../../../tariffs.h"
8 #include "user_helper.h"
13 //------------------------------------------------------------------------------
15 void METHOD_USER_GET::execute(xmlrpc_c::paramList const & paramList,
16 xmlrpc_c::value * const retvalPtr)
18 std::string cookie = paramList.getString(0);
19 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);
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);
61 paramList.verifyEnd(2);
65 if (config->GetAdminInfo(cookie, &adminInfo))
67 *retvalPtr = xmlrpc_c::value_boolean(false);
73 if (admins->FindAdmin(adminInfo.admin, &admin))
75 *retvalPtr = xmlrpc_c::value_boolean(false);
81 if (users->FindByName(login, &u))
83 if (users->Add(login, admin))
85 *retvalPtr = xmlrpc_c::value_boolean(false);
89 *retvalPtr = xmlrpc_c::value_boolean(true);
93 *retvalPtr = xmlrpc_c::value_boolean(false);
97 //------------------------------------------------------------------------------
99 void METHOD_USER_DEL::execute(xmlrpc_c::paramList const & paramList,
100 xmlrpc_c::value * const retvalPtr)
102 std::string cookie = paramList.getString(0);
103 std::string login = paramList.getString(1);
105 paramList.verifyEnd(2);
107 ADMIN_INFO adminInfo;
109 if (config->GetAdminInfo(cookie, &adminInfo))
111 *retvalPtr = xmlrpc_c::value_boolean(false);
117 if (admins->FindAdmin(adminInfo.admin, &admin))
119 *retvalPtr = xmlrpc_c::value_boolean(false);
125 if (users->FindByName(login, &u))
127 users->Del(login, admin);
128 *retvalPtr = xmlrpc_c::value_boolean(true);
132 *retvalPtr = xmlrpc_c::value_boolean(false);
136 //------------------------------------------------------------------------------
138 void METHOD_USERS_GET::execute(xmlrpc_c::paramList const & paramList,
139 xmlrpc_c::value * const retvalPtr)
141 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);
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));
197 paramList.verifyEnd(3);
199 ADMIN_INFO adminInfo;
201 if (config->GetAdminInfo(cookie, &adminInfo))
203 *retvalPtr = xmlrpc_c::value_boolean(false);
209 if (admins->FindAdmin(adminInfo.admin, &admin))
211 *retvalPtr = xmlrpc_c::value_boolean(false);
217 if (users->FindByName(login, &u))
219 *retvalPtr = xmlrpc_c::value_boolean(false);
223 USER_HELPER uhelper(u);
225 if (!adminInfo.priviledges.userConf || !adminInfo.priviledges.userPasswd)
227 uhelper.SetUserInfo(info, admin, login, *store, tariffs);
231 uhelper.SetUserInfo(info, admin, login, *store, tariffs);
237 *retvalPtr = xmlrpc_c::value_boolean(true);
240 //------------------------------------------------------------------------------
242 void METHOD_USER_CASH_ADD::execute(xmlrpc_c::paramList const & paramList,
243 xmlrpc_c::value * const retvalPtr)
245 std::string cookie = paramList.getString(0);
246 std::string login = paramList.getString(1);
247 double amount = paramList.getDouble(2);
248 std::string comment = IconvString(paramList.getString(3), "UTF-8", "KOI8-R");
250 paramList.verifyEnd(4);
252 ADMIN_INFO adminInfo;
254 if (config->GetAdminInfo(cookie, &adminInfo))
256 *retvalPtr = xmlrpc_c::value_boolean(false);
262 if (admins->FindAdmin(adminInfo.admin, &admin))
264 *retvalPtr = xmlrpc_c::value_boolean(false);
270 if (users->FindByName(login, &u))
272 *retvalPtr = xmlrpc_c::value_boolean(false);
276 double cash = u->property.cash.Get();
279 if (!u->property.cash.Set(cash, admin, login, store, comment))
281 *retvalPtr = xmlrpc_c::value_boolean(false);
287 *retvalPtr = xmlrpc_c::value_boolean(true);
290 //------------------------------------------------------------------------------
292 void METHOD_USER_CASH_SET::execute(xmlrpc_c::paramList const & paramList,
293 xmlrpc_c::value * const retvalPtr)
295 std::string cookie = paramList.getString(0);
296 std::string login = paramList.getString(1);
297 double cash = paramList.getDouble(2);
298 std::string comment = IconvString(paramList.getString(3), "UTF-8", "KOI8-R");
300 paramList.verifyEnd(4);
302 ADMIN_INFO adminInfo;
304 if (config->GetAdminInfo(cookie, &adminInfo))
306 *retvalPtr = xmlrpc_c::value_boolean(false);
312 if (admins->FindAdmin(adminInfo.admin, &admin))
314 *retvalPtr = xmlrpc_c::value_boolean(false);
320 if (users->FindByName(login, &u))
322 *retvalPtr = xmlrpc_c::value_boolean(false);
326 if (!u->property.cash.Set(cash, admin, login, store, comment))
328 *retvalPtr = xmlrpc_c::value_boolean(false);
334 *retvalPtr = xmlrpc_c::value_boolean(true);
337 //------------------------------------------------------------------------------
339 void METHOD_USER_TARIFF_CHANGE::execute(xmlrpc_c::paramList const & paramList,
340 xmlrpc_c::value * const retvalPtr)
342 std::string cookie = paramList.getString(0);
343 std::string login = paramList.getString(1);
344 std::string tariff = paramList.getString(2);
345 bool delayed = paramList.getBoolean(3);
346 std::string comment = IconvString(paramList.getString(4), "UTF-8", "KOI8-R");
348 paramList.verifyEnd(5);
350 ADMIN_INFO adminInfo;
352 if (config->GetAdminInfo(cookie, &adminInfo))
354 *retvalPtr = xmlrpc_c::value_boolean(false);
360 if (admins->FindAdmin(adminInfo.admin, &admin))
362 *retvalPtr = xmlrpc_c::value_boolean(false);
368 if (users->FindByName(login, &u))
370 *retvalPtr = xmlrpc_c::value_boolean(false);
374 if (tariffs->FindByName(tariff))
378 if (u->property.nextTariff.Set(tariff,
384 *retvalPtr = xmlrpc_c::value_boolean(true);
390 if (u->property.tariffName.Set(tariff,
396 *retvalPtr = xmlrpc_c::value_boolean(true);
402 *retvalPtr = xmlrpc_c::value_boolean(false);
405 //------------------------------------------------------------------------------
407 void METHOD_GET_ONLINE_IPS::execute(xmlrpc_c::paramList const & paramList,
408 xmlrpc_c::value * const retvalPtr)
410 std::string cookie = paramList.getString(0);
411 std::vector<xmlrpc_c::value> subnetsStr = paramList.getArray(1);
412 paramList.verifyEnd(2);
414 std::vector<IP_MASK> subnets;
416 std::vector<xmlrpc_c::value>::iterator it;
418 for (it = subnetsStr.begin(); it != subnetsStr.end(); ++it)
421 if (ParseNet(xmlrpc_c::value_string(*it), ipm))
423 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::execute(): Failed to parse subnet ('%s')\n", std::string(xmlrpc_c::value_string(*it)).c_str());
427 subnets.push_back(ipm);
431 std::map<std::string, xmlrpc_c::value> structVal;
432 ADMIN_INFO adminInfo;
434 if (config->GetAdminInfo(cookie, &adminInfo))
436 structVal["result"] = xmlrpc_c::value_boolean(false);
437 *retvalPtr = xmlrpc_c::value_struct(structVal);
441 std::vector<xmlrpc_c::value> ips;
445 int handle = users->OpenSearch();
448 printfd(__FILE__, "users->OpenSearch() error\n");
449 users->CloseSearch(handle);
455 if (users->SearchNext(handle, &u))
460 if (u->GetAuthorized())
462 uint32_t ip = u->GetCurrIP();
464 std::vector<IP_MASK>::iterator it;
465 for (it = subnets.begin(); it != subnets.end(); ++it)
467 if ((it->ip & it->mask) == (ip & it->mask))
469 ips.push_back(xmlrpc_c::value_string(inet_ntostring(u->GetCurrIP())));
476 structVal["ips"] = xmlrpc_c::value_array(ips);
478 *retvalPtr = xmlrpc_c::value_struct(structVal);
481 bool METHOD_GET_ONLINE_IPS::ParseNet(const std::string & net, IP_MASK & ipm) const
483 size_t pos = net.find_first_of('/');
485 if (pos == std::string::npos)
487 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Network address is not in CIDR-notation\n");
491 int res = inet_pton(AF_INET, net.substr(0, pos).c_str(), &ipm.ip);
495 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): '%s'\n", strerror(errno));
500 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Invalid network address\n", strerror(errno));
504 if (str2x(net.substr(pos + 1, net.length() - pos - 1), ipm.mask))
506 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Invalid network mask\n");
511 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Network mask is out of range\n");
514 ipm.mask = htonl(0xffFFffFF << (32 - ipm.mask));