3 #include "users_methods.h"
5 #include "../../../users.h"
6 #include "../../../admins.h"
7 #include "../../../tariffs.h"
10 #include "user_helper.h"
15 //------------------------------------------------------------------------------
17 void METHOD_USER_GET::execute(xmlrpc_c::paramList const & paramList,
18 xmlrpc_c::value * const retvalPtr)
20 std::string cookie = paramList.getString(0);
21 std::string login = paramList.getString(1);
23 paramList.verifyEnd(2);
25 std::map<std::string, xmlrpc_c::value> structVal;
28 if (config->GetAdminInfo(cookie, &adminInfo))
30 structVal["result"] = xmlrpc_c::value_boolean(false);
31 *retvalPtr = xmlrpc_c::value_struct(structVal);
37 if (users->FindByName(login, &u))
39 structVal["result"] = xmlrpc_c::value_boolean(false);
40 *retvalPtr = xmlrpc_c::value_struct(structVal);
44 USER_HELPER uhelper(u);
46 if (!adminInfo.priviledges.userConf || !adminInfo.priviledges.userPasswd)
48 uhelper.GetUserInfo(retvalPtr, true);
52 uhelper.GetUserInfo(retvalPtr);
55 //------------------------------------------------------------------------------
57 void METHOD_USER_ADD::execute(xmlrpc_c::paramList const & paramList,
58 xmlrpc_c::value * const retvalPtr)
60 std::string cookie = paramList.getString(0);
61 std::string login = paramList.getString(1);
63 paramList.verifyEnd(2);
67 if (config->GetAdminInfo(cookie, &adminInfo))
69 *retvalPtr = xmlrpc_c::value_boolean(false);
75 if (admins->FindAdmin(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);
107 paramList.verifyEnd(2);
109 ADMIN_INFO adminInfo;
111 if (config->GetAdminInfo(cookie, &adminInfo))
113 *retvalPtr = xmlrpc_c::value_boolean(false);
119 if (admins->FindAdmin(adminInfo.admin, &admin))
121 *retvalPtr = xmlrpc_c::value_boolean(false);
127 if (users->FindByName(login, &u))
129 users->Del(login, admin);
130 *retvalPtr = xmlrpc_c::value_boolean(true);
134 *retvalPtr = xmlrpc_c::value_boolean(false);
138 //------------------------------------------------------------------------------
140 void METHOD_USERS_GET::execute(xmlrpc_c::paramList const & paramList,
141 xmlrpc_c::value * const retvalPtr)
143 std::string cookie = paramList.getString(0);
145 paramList.verifyEnd(1);
147 std::map<std::string, xmlrpc_c::value> structVal;
148 std::vector<xmlrpc_c::value> retval;
149 ADMIN_INFO adminInfo;
151 if (config->GetAdminInfo(cookie, &adminInfo))
153 structVal["result"] = xmlrpc_c::value_boolean(false);
154 *retvalPtr = xmlrpc_c::value_struct(structVal);
158 bool hidePassword = !adminInfo.priviledges.userConf ||
159 !adminInfo.priviledges.userPasswd;
163 int h = users->OpenSearch();
166 printfd(__FILE__, "users->OpenSearch() error\n");
167 users->CloseSearch(h);
173 if (users->SearchNext(h, &u))
178 xmlrpc_c::value info;
180 USER_HELPER uhelper(u);
182 uhelper.GetUserInfo(&info, hidePassword);
184 retval.push_back(info);
187 *retvalPtr = xmlrpc_c::value_array(retval);
190 //------------------------------------------------------------------------------
192 void METHOD_USER_CHG::execute(xmlrpc_c::paramList const & paramList,
193 xmlrpc_c::value * const retvalPtr)
195 std::string cookie = paramList.getString(0);
196 std::string login = paramList.getString(1);
197 xmlrpc_c::value_struct info(paramList.getStruct(2));
199 paramList.verifyEnd(3);
201 ADMIN_INFO adminInfo;
203 if (config->GetAdminInfo(cookie, &adminInfo))
205 *retvalPtr = xmlrpc_c::value_boolean(false);
211 if (admins->FindAdmin(adminInfo.admin, &admin))
213 *retvalPtr = xmlrpc_c::value_boolean(false);
219 if (users->FindByName(login, &u))
221 *retvalPtr = xmlrpc_c::value_boolean(false);
225 USER_HELPER uhelper(u);
227 if (!adminInfo.priviledges.userConf || !adminInfo.priviledges.userPasswd)
229 uhelper.SetUserInfo(info, admin, login, *store, tariffs);
233 uhelper.SetUserInfo(info, admin, login, *store, tariffs);
239 *retvalPtr = xmlrpc_c::value_boolean(true);
242 //------------------------------------------------------------------------------
244 void METHOD_USER_CASH_ADD::execute(xmlrpc_c::paramList const & paramList,
245 xmlrpc_c::value * const retvalPtr)
247 std::string cookie = paramList.getString(0);
248 std::string login = paramList.getString(1);
249 double amount = paramList.getDouble(2);
250 std::string comment = IconvString(paramList.getString(3), "UTF-8", "KOI8-R");
252 paramList.verifyEnd(4);
254 ADMIN_INFO adminInfo;
256 if (config->GetAdminInfo(cookie, &adminInfo))
258 *retvalPtr = xmlrpc_c::value_boolean(false);
264 if (admins->FindAdmin(adminInfo.admin, &admin))
266 *retvalPtr = xmlrpc_c::value_boolean(false);
272 if (users->FindByName(login, &u))
274 *retvalPtr = xmlrpc_c::value_boolean(false);
278 double cash = u->property.cash.Get();
281 if (!u->property.cash.Set(cash, admin, login, store, comment))
283 *retvalPtr = xmlrpc_c::value_boolean(false);
289 *retvalPtr = xmlrpc_c::value_boolean(true);
292 //------------------------------------------------------------------------------
294 void METHOD_USER_CASH_SET::execute(xmlrpc_c::paramList const & paramList,
295 xmlrpc_c::value * const retvalPtr)
297 std::string cookie = paramList.getString(0);
298 std::string login = paramList.getString(1);
299 double cash = paramList.getDouble(2);
300 std::string comment = IconvString(paramList.getString(3), "UTF-8", "KOI8-R");
302 paramList.verifyEnd(4);
304 ADMIN_INFO adminInfo;
306 if (config->GetAdminInfo(cookie, &adminInfo))
308 *retvalPtr = xmlrpc_c::value_boolean(false);
314 if (admins->FindAdmin(adminInfo.admin, &admin))
316 *retvalPtr = xmlrpc_c::value_boolean(false);
322 if (users->FindByName(login, &u))
324 *retvalPtr = xmlrpc_c::value_boolean(false);
328 if (!u->property.cash.Set(cash, admin, login, store, comment))
330 *retvalPtr = xmlrpc_c::value_boolean(false);
336 *retvalPtr = xmlrpc_c::value_boolean(true);
339 //------------------------------------------------------------------------------
341 void METHOD_USER_TARIFF_CHANGE::execute(xmlrpc_c::paramList const & paramList,
342 xmlrpc_c::value * const retvalPtr)
344 std::string cookie = paramList.getString(0);
345 std::string login = paramList.getString(1);
346 std::string tariff = paramList.getString(2);
347 bool delayed = paramList.getBoolean(3);
348 std::string comment = IconvString(paramList.getString(4), "UTF-8", "KOI8-R");
350 paramList.verifyEnd(5);
352 ADMIN_INFO adminInfo;
354 if (config->GetAdminInfo(cookie, &adminInfo))
356 *retvalPtr = xmlrpc_c::value_boolean(false);
362 if (admins->FindAdmin(adminInfo.admin, &admin))
364 *retvalPtr = xmlrpc_c::value_boolean(false);
370 if (users->FindByName(login, &u))
372 *retvalPtr = xmlrpc_c::value_boolean(false);
376 if (tariffs->FindByName(tariff))
380 if (u->property.nextTariff.Set(tariff,
386 *retvalPtr = xmlrpc_c::value_boolean(true);
392 if (u->property.tariffName.Set(tariff,
398 *retvalPtr = xmlrpc_c::value_boolean(true);
404 *retvalPtr = xmlrpc_c::value_boolean(false);
407 //------------------------------------------------------------------------------
409 void METHOD_GET_ONLINE_IPS::execute(xmlrpc_c::paramList const & paramList,
410 xmlrpc_c::value * const retvalPtr)
412 std::string cookie = paramList.getString(0);
413 std::vector<xmlrpc_c::value> subnetsStr = paramList.getArray(1);
414 paramList.verifyEnd(2);
416 std::vector<IP_MASK> subnets;
418 std::vector<xmlrpc_c::value>::iterator it;
420 for (it = subnetsStr.begin(); it != subnetsStr.end(); ++it)
423 if (ParseNet(xmlrpc_c::value_string(*it), ipm))
425 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::execute(): Failed to parse subnet ('%s')\n", std::string(xmlrpc_c::value_string(*it)).c_str());
429 subnets.push_back(ipm);
433 std::map<std::string, xmlrpc_c::value> structVal;
434 ADMIN_INFO adminInfo;
436 if (config->GetAdminInfo(cookie, &adminInfo))
438 structVal["result"] = xmlrpc_c::value_boolean(false);
439 *retvalPtr = xmlrpc_c::value_struct(structVal);
443 std::vector<xmlrpc_c::value> ips;
447 int handle = users->OpenSearch();
450 printfd(__FILE__, "users->OpenSearch() error\n");
451 users->CloseSearch(handle);
457 if (users->SearchNext(handle, &u))
462 if (u->GetAuthorized())
464 uint32_t ip = u->GetCurrIP();
466 std::vector<IP_MASK>::iterator it;
467 for (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, IP_MASK & 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));