]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/rpcconfig/user_helper.cpp
Ticket 37. Int2TS() method changed to formatTime() function, TS2Int() method
[stg.git] / projects / stargazer / plugins / configuration / rpcconfig / user_helper.cpp
1 #include <cmath>
2
3 #include "stg/tariffs.h"
4 #include "stg/admin.h"
5 #include "stg/store.h"
6 #include "stg/user_ips.h"
7 #include "stg/common.h"
8 #include "stg/user_property.h"
9 #include "user_helper.h"
10
11 //------------------------------------------------------------------------------
12
13 void USER_HELPER::GetUserInfo(xmlrpc_c::value * info,
14                               bool hidePassword)
15 {
16 std::map<std::string, xmlrpc_c::value> structVal;
17
18 structVal["result"] = xmlrpc_c::value_boolean(true);
19 structVal["login"] = xmlrpc_c::value_string(ptr->GetLogin());
20
21 if (!hidePassword)
22     {
23     structVal["password"] = xmlrpc_c::value_string(ptr->GetProperty().password.Get());
24     }
25 else
26     {
27     structVal["password"] = xmlrpc_c::value_string("++++++++");
28     }
29
30 structVal["cash"] = xmlrpc_c::value_double(ptr->GetProperty().cash.Get());
31 structVal["freemb"] = xmlrpc_c::value_double(ptr->GetProperty().freeMb.Get());
32 structVal["credit"] = xmlrpc_c::value_double(ptr->GetProperty().credit.Get());
33
34 if (ptr->GetProperty().nextTariff.Get() != "")
35     {
36     structVal["tariff"] = xmlrpc_c::value_string(
37             ptr->GetProperty().tariffName.Get() +
38             "/" +
39             ptr->GetProperty().nextTariff.Get()
40             );
41     }
42 else
43     {
44     structVal["tariff"] = xmlrpc_c::value_string(ptr->GetProperty().tariffName.Get());
45     }
46
47 structVal["note"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().note, "KOI8-RU", "UTF-8"));
48
49 structVal["phone"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().phone, "KOI8-RU", "UTF-8"));
50
51 structVal["address"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().address, "KOI8-RU", "UTF-8"));
52
53 structVal["email"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().email, "KOI8-RU", "UTF-8"));
54
55 std::vector<xmlrpc_c::value> userdata;
56
57 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata0.Get(), "KOI8-RU", "UTF-8")));
58 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata1.Get(), "KOI8-RU", "UTF-8")));
59 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata2.Get(), "KOI8-RU", "UTF-8")));
60 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata3.Get(), "KOI8-RU", "UTF-8")));
61 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata4.Get(), "KOI8-RU", "UTF-8")));
62 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata5.Get(), "KOI8-RU", "UTF-8")));
63 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata6.Get(), "KOI8-RU", "UTF-8")));
64 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata7.Get(), "KOI8-RU", "UTF-8")));
65 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata8.Get(), "KOI8-RU", "UTF-8")));
66 userdata.push_back(xmlrpc_c::value_string(IconvString(ptr->GetProperty().userdata9.Get(), "KOI8-RU", "UTF-8")));
67
68 structVal["userdata"] = xmlrpc_c::value_array(userdata);
69
70 structVal["name"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().realName, "KOI8-RU", "UTF-8"));
71
72 structVal["group"] = xmlrpc_c::value_string(IconvString(ptr->GetProperty().group, "KOI8-RU", "UTF-8"));
73
74 structVal["status"] = xmlrpc_c::value_boolean(ptr->GetConnected());
75 structVal["aonline"] = xmlrpc_c::value_boolean(ptr->GetProperty().alwaysOnline.Get());
76 structVal["currip"] = xmlrpc_c::value_string(inet_ntostring(ptr->GetCurrIP()));
77 structVal["pingtime"] = xmlrpc_c::value_int(static_cast<int>(ptr->GetPingTime()));
78 structVal["ips"] = xmlrpc_c::value_string(ptr->GetProperty().ips.Get().GetIpStr());
79
80 std::map<std::string, xmlrpc_c::value> traffInfo;
81 std::vector<xmlrpc_c::value> mu(DIR_NUM);
82 std::vector<xmlrpc_c::value> md(DIR_NUM);
83 std::vector<xmlrpc_c::value> su(DIR_NUM);
84 std::vector<xmlrpc_c::value> sd(DIR_NUM);
85
86 DIR_TRAFF upload;
87 DIR_TRAFF download;
88 DIR_TRAFF supload;
89 DIR_TRAFF sdownload;
90 download = ptr->GetProperty().down.Get();
91 upload = ptr->GetProperty().up.Get();
92 sdownload = ptr->GetSessionUpload();
93 supload = ptr->GetSessionDownload();
94
95 for (int j = 0; j < DIR_NUM; j++)
96     {
97     std::string value;
98     x2str(upload[j], value);
99     mu[j] = xmlrpc_c::value_string(value);
100     x2str(download[j], value);
101     md[j] = xmlrpc_c::value_string(value);
102     x2str(supload[j], value);
103     su[j] = xmlrpc_c::value_string(value);
104     x2str(sdownload[j], value);
105     sd[j] = xmlrpc_c::value_string(value);
106     }
107
108 traffInfo["mu"] = xmlrpc_c::value_array(mu);
109 traffInfo["md"] = xmlrpc_c::value_array(md);
110 traffInfo["su"] = xmlrpc_c::value_array(su);
111 traffInfo["sd"] = xmlrpc_c::value_array(sd);
112
113 structVal["traff"] = xmlrpc_c::value_struct(traffInfo);
114
115 structVal["down"] = xmlrpc_c::value_boolean(ptr->GetProperty().disabled.Get());
116 structVal["disableddetailstat"] = xmlrpc_c::value_boolean(ptr->GetProperty().disabledDetailStat.Get());
117 structVal["passive"] = xmlrpc_c::value_boolean(ptr->GetProperty().passive.Get());
118 structVal["lastcash"] = xmlrpc_c::value_double(ptr->GetProperty().lastCashAdd.Get());
119 structVal["lasttimecash"] = xmlrpc_c::value_int(static_cast<int>(ptr->GetProperty().lastCashAddTime.Get()));
120 structVal["lastactivitytime"] = xmlrpc_c::value_int(static_cast<int>(ptr->GetProperty().lastActivityTime.Get()));
121 structVal["creditexpire"] = xmlrpc_c::value_int(static_cast<int>(ptr->GetProperty().creditExpire.Get()));
122
123 *info = xmlrpc_c::value_struct(structVal);
124 }
125
126 //------------------------------------------------------------------------------
127
128 bool USER_HELPER::SetUserInfo(const xmlrpc_c::value & info,
129                               const ADMIN * admin,
130                               const std::string & login,
131                               const STORE & store,
132                               TARIFFS * tariffs)
133 {
134 std::map<std::string, xmlrpc_c::value> structVal(
135     static_cast<std::map<std::string, xmlrpc_c::value> >(xmlrpc_c::value_struct(info))
136     );
137
138 std::map<std::string, xmlrpc_c::value>::iterator it;
139
140 bool check = false;
141 bool alwaysOnline = ptr->GetProperty().alwaysOnline;
142 if ((it = structVal.find("aonline")) != structVal.end())
143     {
144     check = true;
145     alwaysOnline = xmlrpc_c::value_boolean(it->second);
146     }
147 bool onlyOneIP = ptr->GetProperty().ips.ConstData().OnlyOneIP();
148 if ((it = structVal.find("ips")) != structVal.end())
149     {
150     check = true;
151     onlyOneIP = StrToIPS(xmlrpc_c::value_string(it->second)).OnlyOneIP();
152     }
153
154 if (check && alwaysOnline && !onlyOneIP)
155     {
156     printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n");
157     return true;
158     }
159
160 if ((it = structVal.find("ips")) != structVal.end())
161     {
162     USER_IPS ips;
163     ips = StrToIPS(xmlrpc_c::value_string(it->second));
164
165     for (size_t i = 0; i < ips.Count(); ++i)
166         {
167         CONST_USER_PTR user;
168         uint32_t ip = ips[i].ip;
169         if (users.IsIPInUse(ip, login, &user))
170             {
171             printfd(__FILE__, "Trying to assign an IP %s to '%s' that is already in use by '%s'\n", inet_ntostring(ip).c_str(), login.c_str(), user->GetLogin().c_str());
172             return true;
173             }
174         }
175
176     if (!ptr->GetProperty().ips.Set(ips,
177                                 admin,
178                                 login,
179                                 &store))
180         return true;
181     }
182
183 if ((it = structVal.find("aonline")) != structVal.end())
184     {
185     bool value(xmlrpc_c::value_boolean(it->second));
186     if (ptr->GetProperty().alwaysOnline.Get() != value)
187         if (!ptr->GetProperty().alwaysOnline.Set(value,
188                                              admin,
189                                              login,
190                                              &store))
191             return true;
192     }
193
194 if ((it = structVal.find("password")) != structVal.end())
195     {
196     std::string value(xmlrpc_c::value_string(it->second));
197     if (ptr->GetProperty().password.Get() != value)
198         if (!ptr->GetProperty().password.Set(value,
199                                          admin,
200                                          login,
201                                          &store))
202             return true;
203     }
204
205 if ((it = structVal.find("address")) != structVal.end())
206     {
207     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
208     if (ptr->GetProperty().address.Get() != value)
209         if (!ptr->GetProperty().address.Set(value,
210                                         admin,
211                                         login,
212                                         &store))
213             return true;
214     }
215
216 if ((it = structVal.find("phone")) != structVal.end())
217     {
218     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
219     if (ptr->GetProperty().phone.Get() != value)
220         if (!ptr->GetProperty().phone.Set(value,
221                                       admin,
222                                       login,
223                                       &store))
224             return true;
225     }
226
227 if ((it = structVal.find("email")) != structVal.end())
228     {
229     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
230     if (ptr->GetProperty().email.Get() != value)
231         if (!ptr->GetProperty().email.Set(value,
232                                       admin,
233                                       login,
234                                       &store))
235             return true;
236     }
237
238 if ((it = structVal.find("cash")) != structVal.end())
239     {
240     double value(xmlrpc_c::value_double(it->second));
241     if (std::fabs(ptr->GetProperty().cash.Get() - value) > 1.0e-3)
242         if (!ptr->GetProperty().cash.Set(value,
243                                      admin,
244                                      login,
245                                      &store))
246             return true;
247     }
248
249 if ((it = structVal.find("creditexpire")) != structVal.end())
250     {
251     time_t value(xmlrpc_c::value_int(it->second));
252     if (ptr->GetProperty().creditExpire.Get() != value)
253         if (!ptr->GetProperty().creditExpire.Set(value,
254                                              admin,
255                                              login,
256                                              &store))
257             return true;
258     }
259
260 if ((it = structVal.find("credit")) != structVal.end())
261     {
262     double value(xmlrpc_c::value_double(it->second));
263     if (std::fabs(ptr->GetProperty().credit.Get() - value) > 1.0e-3)
264         if (!ptr->GetProperty().credit.Set(value,
265                                        admin,
266                                        login,
267                                        &store))
268             return true;
269     }
270
271 if ((it = structVal.find("freemb")) != structVal.end())
272     {
273     double value(xmlrpc_c::value_double(it->second));
274     if (std::fabs(ptr->GetProperty().freeMb.Get() - value) > 1.0e-3)
275         if (!ptr->GetProperty().freeMb.Set(value,
276                                        admin,
277                                        login,
278                                        &store))
279             return true;
280     }
281
282 if ((it = structVal.find("down")) != structVal.end())
283     {
284     bool value(xmlrpc_c::value_boolean(it->second));
285     if (ptr->GetProperty().disabled.Get() != value)
286         if (!ptr->GetProperty().disabled.Set(value,
287                                          admin,
288                                          login,
289                                          &store))
290             return true;
291     }
292
293 if ((it = structVal.find("passive")) != structVal.end())
294     {
295     bool value(xmlrpc_c::value_boolean(it->second));
296     if (ptr->GetProperty().passive.Get() != value)
297         if (!ptr->GetProperty().passive.Set(value,
298                                         admin,
299                                         login,
300                                         &store))
301             return true;
302     }
303
304 if ((it = structVal.find("disableddetailstat")) != structVal.end())
305     {
306     bool value(xmlrpc_c::value_boolean(it->second));
307     if (ptr->GetProperty().disabledDetailStat.Get() != value)
308         if (!ptr->GetProperty().disabledDetailStat.Set(value,
309                                                    admin,
310                                                    login,
311                                                    &store))
312             return true;
313     }
314
315 if ((it = structVal.find("name")) != structVal.end())
316     {
317     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
318     if (ptr->GetProperty().realName.Get() != value)
319         if (!ptr->GetProperty().realName.Set(value,
320                                          admin,
321                                          login,
322                                          &store))
323             return true;
324     }
325
326 if ((it = structVal.find("group")) != structVal.end())
327     {
328     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
329     if (ptr->GetProperty().group.Get() != value)
330         if (!ptr->GetProperty().group.Set(value,
331                                       admin,
332                                       login,
333                                       &store))
334             return true;
335     }
336
337 if ((it = structVal.find("note")) != structVal.end())
338     {
339     std::string value(IconvString(xmlrpc_c::value_string(it->second), "UTF-8", "KOI8-RU"));
340     if (ptr->GetProperty().note.Get() != value)
341         if (!ptr->GetProperty().note.Set(value,
342                                      admin,
343                                      login,
344                                      &store))
345             return true;
346     }
347
348 if ((it = structVal.find("userdata")) != structVal.end())
349     {
350     std::vector<USER_PROPERTY_LOGGED<std::string> *> userdata;
351     userdata.push_back(ptr->GetProperty().userdata0.GetPointer());
352     userdata.push_back(ptr->GetProperty().userdata1.GetPointer());
353     userdata.push_back(ptr->GetProperty().userdata2.GetPointer());
354     userdata.push_back(ptr->GetProperty().userdata3.GetPointer());
355     userdata.push_back(ptr->GetProperty().userdata4.GetPointer());
356     userdata.push_back(ptr->GetProperty().userdata5.GetPointer());
357     userdata.push_back(ptr->GetProperty().userdata6.GetPointer());
358     userdata.push_back(ptr->GetProperty().userdata7.GetPointer());
359     userdata.push_back(ptr->GetProperty().userdata8.GetPointer());
360     userdata.push_back(ptr->GetProperty().userdata9.GetPointer());
361
362     std::vector<xmlrpc_c::value> udata(
363         xmlrpc_c::value_array(it->second).vectorValueValue()
364         );
365
366     for (unsigned i = 0; i < userdata.size(); ++i)
367         {
368         std::string value(IconvString(xmlrpc_c::value_string(udata[i]), "UTF-8", "KOI8-RU"));
369         if (userdata[i]->Get() != value)
370             if (!userdata[i]->Set(value,
371                                   admin,
372                                   login,
373                                   &store))
374                 return true;
375         }
376     }
377
378 if ((it = structVal.find("traff")) != structVal.end())
379     {
380     std::map<std::string, xmlrpc_c::value> traff(
381         static_cast<std::map<std::string, xmlrpc_c::value> >(xmlrpc_c::value_struct(it->second))
382         );
383
384     DIR_TRAFF dtData;
385     dtData = ptr->GetProperty().up.Get();
386     if ((it = traff.find("mu")) != traff.end())
387         {
388         std::vector<xmlrpc_c::value> data(xmlrpc_c::value_array(it->second).vectorValueValue());
389
390         for (int i = 0; i < std::min(DIR_NUM, static_cast<int>(data.size())); ++i)
391             {
392             int64_t value;
393             if (str2x(xmlrpc_c::value_string(data[i]), value))
394                 printfd(__FILE__, "USER_HELPER::SetUserInfo(): 'Invalid month upload value'\n");
395             else
396                 dtData[i] = value;
397             }
398         if (!ptr->GetProperty().up.Set(dtData,
399                                    admin,
400                                    login,
401                                    &store))
402             return true;
403         }
404     dtData = ptr->GetProperty().down.Get();
405     if ((it = traff.find("md")) != traff.end())
406         {
407         std::vector<xmlrpc_c::value> data(xmlrpc_c::value_array(it->second).vectorValueValue());
408
409         for (int i = 0; i < std::min(DIR_NUM, static_cast<int>(data.size())); ++i)
410             {
411             int64_t value;
412             if (str2x(xmlrpc_c::value_string(data[i]), value))
413                 printfd(__FILE__, "USER_HELPER::SetUserInfo(): 'Invalid month download value'\n");
414             else
415                 dtData[i] = value;
416             }
417         if (!ptr->GetProperty().down.Set(dtData,
418                                      admin,
419                                      login,
420                                      &store))
421             return true;
422         }
423     }
424
425 if ((it = structVal.find("tariff")) != structVal.end())
426     {
427     std::string tariff(xmlrpc_c::value_string(it->second));
428     size_t pos = tariff.find('/');
429     std::string nextTariff;
430     if (pos != std::string::npos)
431         {
432         nextTariff = tariff.substr(pos + 1);
433         tariff = tariff.substr(0, pos);
434         }
435
436     const TARIFF * newTariff = tariffs->FindByName(tariff);
437     if (newTariff)
438         {
439         const TARIFF * currentTariff = ptr->GetTariff();
440         std::string message = currentTariff->TariffChangeIsAllowed(*newTariff);
441         if (message.empty())
442             {
443             if (ptr->GetProperty().tariffName.Get() != tariff)
444                 {
445                 if (!ptr->GetProperty().tariffName.Set(tariff,
446                                                    admin,
447                                                    login,
448                                                    &store))
449                     return true;
450                 }
451             }
452         else
453             {
454             GetStgLogger()("Tariff change is prohibited for user %s. %s", ptr->GetLogin().c_str(), message.c_str());
455             }
456         }
457
458     if (nextTariff != "" &&
459         tariffs->FindByName(nextTariff))
460         if (ptr->GetProperty().nextTariff.Get() != nextTariff)
461             if (!ptr->GetProperty().nextTariff.Set(tariff,
462                                                admin,
463                                                login,
464                                                &store))
465                 return true;
466     }
467
468 return false;
469 }