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"
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);
22 paramList.verifyEnd(2);
24 std::map<std::string, xmlrpc_c::value> structVal;
27 if (config->GetAdminInfo(cookie, &adminInfo))
29 structVal["result"] = xmlrpc_c::value_boolean(false);
30 *retvalPtr = xmlrpc_c::value_struct(structVal);
36 if (users->FindByName(login, &u))
38 structVal["result"] = xmlrpc_c::value_boolean(false);
39 *retvalPtr = xmlrpc_c::value_struct(structVal);
43 USER_HELPER uhelper(u);
45 if (!adminInfo.priviledges.userConf || !adminInfo.priviledges.userPasswd)
47 uhelper.GetUserInfo(retvalPtr, true);
51 uhelper.GetUserInfo(retvalPtr);
54 //------------------------------------------------------------------------------
56 void METHOD_USER_ADD::execute(xmlrpc_c::paramList const & paramList,
57 xmlrpc_c::value * const retvalPtr)
59 std::string cookie = paramList.getString(0);
60 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->Find(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);
104 paramList.verifyEnd(2);
106 ADMIN_INFO adminInfo;
108 if (config->GetAdminInfo(cookie, &adminInfo))
110 *retvalPtr = xmlrpc_c::value_boolean(false);
116 if (admins->Find(adminInfo.admin, &admin))
118 *retvalPtr = xmlrpc_c::value_boolean(false);
124 if (!users->FindByName(login, &u))
126 users->Del(login, admin);
127 *retvalPtr = xmlrpc_c::value_boolean(true);
131 *retvalPtr = xmlrpc_c::value_boolean(false);
135 //------------------------------------------------------------------------------
137 void METHOD_USERS_GET::execute(xmlrpc_c::paramList const & paramList,
138 xmlrpc_c::value * const retvalPtr)
140 std::string cookie = paramList.getString(0);
141 paramList.verifyEnd(1);
143 std::map<std::string, xmlrpc_c::value> structVal;
144 std::vector<xmlrpc_c::value> retval;
145 ADMIN_INFO adminInfo;
147 if (config->GetAdminInfo(cookie, &adminInfo))
149 structVal["result"] = xmlrpc_c::value_boolean(false);
150 *retvalPtr = xmlrpc_c::value_struct(structVal);
154 bool hidePassword = !adminInfo.priviledges.userConf ||
155 !adminInfo.priviledges.userPasswd;
159 int h = users->OpenSearch();
162 printfd(__FILE__, "users->OpenSearch() error\n");
163 users->CloseSearch(h);
169 if (users->SearchNext(h, &u))
174 xmlrpc_c::value info;
176 USER_HELPER uhelper(u);
178 uhelper.GetUserInfo(&info, hidePassword);
180 retval.push_back(info);
183 *retvalPtr = xmlrpc_c::value_array(retval);
186 //------------------------------------------------------------------------------
188 void METHOD_USER_CHG::execute(xmlrpc_c::paramList const & paramList,
189 xmlrpc_c::value * const retvalPtr)
191 std::string cookie = paramList.getString(0);
192 std::string login = paramList.getString(1);
193 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->Find(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, tariffs);
228 uhelper.SetUserInfo(info, admin, login, *store, tariffs);
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");
246 paramList.verifyEnd(4);
248 ADMIN_INFO adminInfo;
250 if (config->GetAdminInfo(cookie, &adminInfo))
252 *retvalPtr = xmlrpc_c::value_boolean(false);
258 if (admins->Find(adminInfo.admin, &admin))
260 *retvalPtr = xmlrpc_c::value_boolean(false);
266 if (users->FindByName(login, &u))
268 *retvalPtr = xmlrpc_c::value_boolean(false);
272 double cash = u->GetProperty().cash.Get();
275 if (!u->GetProperty().cash.Set(cash, admin, login, store, comment))
277 *retvalPtr = xmlrpc_c::value_boolean(false);
283 *retvalPtr = xmlrpc_c::value_boolean(true);
286 //------------------------------------------------------------------------------
288 void METHOD_USER_CASH_SET::execute(xmlrpc_c::paramList const & paramList,
289 xmlrpc_c::value * const retvalPtr)
291 std::string cookie = paramList.getString(0);
292 std::string login = paramList.getString(1);
293 double cash = paramList.getDouble(2);
294 std::string comment = IconvString(paramList.getString(3), "UTF-8", "KOI8-R");
295 paramList.verifyEnd(4);
297 ADMIN_INFO adminInfo;
299 if (config->GetAdminInfo(cookie, &adminInfo))
301 *retvalPtr = xmlrpc_c::value_boolean(false);
307 if (admins->Find(adminInfo.admin, &admin))
309 *retvalPtr = xmlrpc_c::value_boolean(false);
315 if (users->FindByName(login, &u))
317 *retvalPtr = xmlrpc_c::value_boolean(false);
321 if (!u->GetProperty().cash.Set(cash, admin, login, store, comment))
323 *retvalPtr = xmlrpc_c::value_boolean(false);
329 *retvalPtr = xmlrpc_c::value_boolean(true);
332 //------------------------------------------------------------------------------
334 void METHOD_USER_TARIFF_CHANGE::execute(xmlrpc_c::paramList const & paramList,
335 xmlrpc_c::value * const retvalPtr)
337 std::string cookie = paramList.getString(0);
338 std::string login = paramList.getString(1);
339 std::string tariff = paramList.getString(2);
340 bool delayed = paramList.getBoolean(3);
341 std::string comment = IconvString(paramList.getString(4), "UTF-8", "KOI8-R");
342 paramList.verifyEnd(5);
344 ADMIN_INFO adminInfo;
346 if (config->GetAdminInfo(cookie, &adminInfo))
348 *retvalPtr = xmlrpc_c::value_boolean(false);
354 if (admins->Find(adminInfo.admin, &admin))
356 *retvalPtr = xmlrpc_c::value_boolean(false);
362 if (users->FindByName(login, &u))
364 *retvalPtr = xmlrpc_c::value_boolean(false);
368 if (tariffs->FindByName(tariff))
372 if (u->GetProperty().nextTariff.Set(tariff,
379 *retvalPtr = xmlrpc_c::value_boolean(true);
385 if (u->GetProperty().tariffName.Set(tariff,
391 u->ResetNextTariff();
393 *retvalPtr = xmlrpc_c::value_boolean(true);
399 *retvalPtr = xmlrpc_c::value_boolean(false);
402 //------------------------------------------------------------------------------
404 void METHOD_GET_ONLINE_IPS::execute(xmlrpc_c::paramList const & paramList,
405 xmlrpc_c::value * const retvalPtr)
407 std::string cookie = paramList.getString(0);
408 std::vector<xmlrpc_c::value> subnetsStr = paramList.getArray(1);
409 paramList.verifyEnd(2);
411 std::vector<IP_MASK> subnets;
413 std::vector<xmlrpc_c::value>::iterator it;
415 for (it = subnetsStr.begin(); it != subnetsStr.end(); ++it)
418 if (ParseNet(xmlrpc_c::value_string(*it), ipm))
420 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::execute(): Failed to parse subnet ('%s')\n", std::string(xmlrpc_c::value_string(*it)).c_str());
424 subnets.push_back(ipm);
428 std::map<std::string, xmlrpc_c::value> structVal;
429 ADMIN_INFO adminInfo;
431 if (config->GetAdminInfo(cookie, &adminInfo))
433 structVal["result"] = xmlrpc_c::value_boolean(false);
434 *retvalPtr = xmlrpc_c::value_struct(structVal);
438 std::vector<xmlrpc_c::value> ips;
442 int handle = users->OpenSearch();
445 printfd(__FILE__, "users->OpenSearch() error\n");
446 users->CloseSearch(handle);
452 if (users->SearchNext(handle, &u))
457 if (u->GetAuthorized())
459 uint32_t ip = u->GetCurrIP();
461 std::vector<IP_MASK>::iterator it;
462 for (it = subnets.begin(); it != subnets.end(); ++it)
464 if ((it->ip & it->mask) == (ip & it->mask))
466 ips.push_back(xmlrpc_c::value_string(inet_ntostring(u->GetCurrIP())));
473 structVal["ips"] = xmlrpc_c::value_array(ips);
475 *retvalPtr = xmlrpc_c::value_struct(structVal);
478 bool METHOD_GET_ONLINE_IPS::ParseNet(const std::string & net, IP_MASK & ipm) const
480 size_t pos = net.find_first_of('/');
482 if (pos == std::string::npos)
484 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Network address is not in CIDR-notation\n");
488 int res = inet_pton(AF_INET, net.substr(0, pos).c_str(), &ipm.ip);
492 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): '%s'\n", strerror(errno));
497 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Invalid network address\n", strerror(errno));
501 if (str2x(net.substr(pos + 1, net.length() - pos - 1), ipm.mask))
503 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Invalid network mask\n");
508 printfd(__FILE__, "METHOD_GET_ONLINE_IPS::ParseNet(): Network mask is out of range\n");
511 ipm.mask = htonl(0xffFFffFF << (32 - ipm.mask));