1 #include "users_methods.h"
4 #include "user_helper.h"
10 //------------------------------------------------------------------------------
12 void METHOD_USER_GET::execute(xmlrpc_c::paramList const & paramList,
13 xmlrpc_c::value * const retvalPtr)
15 std::string cookie = paramList.getString(0);
16 std::string login = paramList.getString(1);
18 paramList.verifyEnd(2);
20 std::map<std::string, xmlrpc_c::value> structVal;
23 if (config->GetAdminInfo(cookie, &adminInfo))
25 structVal["result"] = xmlrpc_c::value_boolean(false);
26 *retvalPtr = xmlrpc_c::value_struct(structVal);
32 if (users->FindByName(login, &u))
34 structVal["result"] = xmlrpc_c::value_boolean(false);
35 *retvalPtr = xmlrpc_c::value_struct(structVal);
39 USER_HELPER uhelper(u);
41 if (!adminInfo.priviledges.userConf || !adminInfo.priviledges.userPasswd)
43 uhelper.GetUserInfo(retvalPtr, true);
47 uhelper.GetUserInfo(retvalPtr);
50 //------------------------------------------------------------------------------
52 void METHOD_USER_ADD::execute(xmlrpc_c::paramList const & paramList,
53 xmlrpc_c::value * const retvalPtr)
55 std::string cookie = paramList.getString(0);
56 std::string login = paramList.getString(1);
58 paramList.verifyEnd(2);
62 if (config->GetAdminInfo(cookie, &adminInfo))
64 *retvalPtr = xmlrpc_c::value_boolean(false);
70 if (admins->FindAdmin(adminInfo.admin, &admin))
72 *retvalPtr = xmlrpc_c::value_boolean(false);
78 if (users->FindByName(login, &u))
80 if (users->Add(login, admin))
82 *retvalPtr = xmlrpc_c::value_boolean(false);
86 *retvalPtr = xmlrpc_c::value_boolean(true);
90 *retvalPtr = xmlrpc_c::value_boolean(false);
94 //------------------------------------------------------------------------------
96 void METHOD_USER_DEL::execute(xmlrpc_c::paramList const & paramList,
97 xmlrpc_c::value * const retvalPtr)
99 std::string cookie = paramList.getString(0);
100 std::string login = paramList.getString(1);
102 paramList.verifyEnd(2);
104 ADMIN_INFO adminInfo;
106 if (config->GetAdminInfo(cookie, &adminInfo))
108 *retvalPtr = xmlrpc_c::value_boolean(false);
114 if (admins->FindAdmin(adminInfo.admin, &admin))
116 *retvalPtr = xmlrpc_c::value_boolean(false);
122 if (users->FindByName(login, &u))
124 users->Del(login, admin);
125 *retvalPtr = xmlrpc_c::value_boolean(true);
129 *retvalPtr = xmlrpc_c::value_boolean(false);
133 //------------------------------------------------------------------------------
135 void METHOD_USERS_GET::execute(xmlrpc_c::paramList const & paramList,
136 xmlrpc_c::value * const retvalPtr)
138 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);
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));
194 paramList.verifyEnd(3);
196 ADMIN_INFO adminInfo;
198 if (config->GetAdminInfo(cookie, &adminInfo))
200 *retvalPtr = xmlrpc_c::value_boolean(false);
206 if (admins->FindAdmin(adminInfo.admin, &admin))
208 *retvalPtr = xmlrpc_c::value_boolean(false);
214 if (users->FindByName(login, &u))
216 *retvalPtr = xmlrpc_c::value_boolean(false);
220 USER_HELPER uhelper(u);
222 if (!adminInfo.priviledges.userConf || !adminInfo.priviledges.userPasswd)
224 uhelper.SetUserInfo(info, admin, login, *store);
228 uhelper.SetUserInfo(info, admin, login, *store);
234 *retvalPtr = xmlrpc_c::value_boolean(true);
237 //------------------------------------------------------------------------------
239 void METHOD_USER_CASH_ADD::execute(xmlrpc_c::paramList const & paramList,
240 xmlrpc_c::value * const retvalPtr)
242 std::string cookie = paramList.getString(0);
243 std::string login = paramList.getString(1);
244 double amount = paramList.getDouble(2);
245 std::string comment = IconvString(paramList.getString(3), "UTF-8", "KOI8-R");
247 paramList.verifyEnd(4);
249 ADMIN_INFO adminInfo;
251 if (config->GetAdminInfo(cookie, &adminInfo))
253 *retvalPtr = xmlrpc_c::value_boolean(false);
259 if (admins->FindAdmin(adminInfo.admin, &admin))
261 *retvalPtr = xmlrpc_c::value_boolean(false);
267 if (users->FindByName(login, &u))
269 *retvalPtr = xmlrpc_c::value_boolean(false);
273 double cash = u->property.cash.Get();
276 if (!u->property.cash.Set(cash, admin, login, store, comment))
278 *retvalPtr = xmlrpc_c::value_boolean(false);
284 *retvalPtr = xmlrpc_c::value_boolean(true);
287 //------------------------------------------------------------------------------
289 void METHOD_USER_CASH_SET::execute(xmlrpc_c::paramList const & paramList,
290 xmlrpc_c::value * const retvalPtr)
292 std::string cookie = paramList.getString(0);
293 std::string login = paramList.getString(1);
294 double cash = paramList.getDouble(2);
295 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->FindAdmin(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->property.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");
345 paramList.verifyEnd(5);
347 ADMIN_INFO adminInfo;
349 if (config->GetAdminInfo(cookie, &adminInfo))
351 *retvalPtr = xmlrpc_c::value_boolean(false);
357 if (admins->FindAdmin(adminInfo.admin, &admin))
359 *retvalPtr = xmlrpc_c::value_boolean(false);
365 if (users->FindByName(login, &u))
367 *retvalPtr = xmlrpc_c::value_boolean(false);
371 if (tariffs->FindByName(tariff))
375 if (u->property.nextTariff.Set(tariff,
381 *retvalPtr = xmlrpc_c::value_boolean(true);
385 if (u->property.tariffName.Set(tariff,
391 *retvalPtr = xmlrpc_c::value_boolean(true);
396 *retvalPtr = xmlrpc_c::value_boolean(false);
399 //------------------------------------------------------------------------------
401 void METHOD_GET_ONLINE_IPS::execute(xmlrpc_c::paramList const & paramList,
402 xmlrpc_c::value * const retvalPtr)
404 std::string cookie = paramList.getString(0);
405 std::vector<xmlrpc_c::value> subnetsStr = paramList.getArray(1);
406 paramList.verifyEnd(2);
408 std::vector<IP_MASK> subnets;
410 std::vector<xmlrpc_c::value>::iterator it;
412 for (it = subnetsStr.begin(); it != subnetsStr.end(); ++it)
415 if (ParseNet(xmlrpc_c::value_string(*it), ipm))
417 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::execute(): Failed to parse subnet ('%s')\n", std::string(xmlrpc_c::value_string(*it)).c_str());
421 subnets.push_back(ipm);
425 std::map<std::string, xmlrpc_c::value> structVal;
426 ADMIN_INFO adminInfo;
428 if (config->GetAdminInfo(cookie, &adminInfo))
430 structVal["result"] = xmlrpc_c::value_boolean(false);
431 *retvalPtr = xmlrpc_c::value_struct(structVal);
435 std::vector<xmlrpc_c::value> ips;
439 int handle = users->OpenSearch();
442 printfd(__FILE__, "users->OpenSearch() error\n");
443 users->CloseSearch(handle);
449 if (users->SearchNext(handle, &u))
454 if (u->GetAuthorized())
456 uint32_t ip = u->GetCurrIP();
458 std::vector<IP_MASK>::iterator it;
459 for (it = subnets.begin(); it != subnets.end(); ++it)
461 if ((it->ip & it->mask) == (ip & it->mask))
463 ips.push_back(xmlrpc_c::value_string(inet_ntostring(u->GetCurrIP())));
470 structVal["ips"] = xmlrpc_c::value_array(ips);
472 *retvalPtr = xmlrpc_c::value_struct(structVal);
475 bool METHOD_GET_ONLINE_IPS::ParseNet(const std::string & net, IP_MASK & ipm) const
477 size_t pos = net.find_first_of('/');
479 if (pos == std::string::npos)
481 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Network address is not in CIDR-notation\n");
485 int res = inet_pton(AF_INET, net.substr(0, pos).c_str(), &ipm.ip);
489 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): '%s'\n", strerror(errno));
494 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Invalid network address\n", strerror(errno));
498 if (str2x(net.substr(pos + 1, net.length() - pos - 1), ipm.mask))
500 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Invalid network mask\n");
505 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Network mask is out of range\n");
508 ipm.mask = htonl(0xffFFffFF << (32 - ipm.mask));