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