]> git.stg.codes - stg.git/blob - include/stg/user_property.h
optimization method USER_IMPL::GetParamValue
[stg.git] / include / stg / user_property.h
1 /*
2 $Revision: 1.44 $
3 $Date: 2010/09/13 05:54:43 $
4 $Author: faust $
5 */
6
7 #ifndef USER_PROPERTY_H
8 #define USER_PROPERTY_H
9
10 #include <unistd.h> // access
11
12 #include <ctime>
13 #include <string>
14 #include <set>
15 #include <sstream>
16 #include <iostream>
17
18 #include "stg/logger.h"
19 #include "stg/locker.h"
20 #include "stg/scriptexecuter.h"
21
22 #include "store.h"
23 #include "admin.h"
24 #include "notifer.h"
25 #include "noncopyable.h"
26
27 extern volatile time_t stgTime;
28 //-----------------------------------------------------------------------------
29 class USER_PROPERTY_BASE {
30 public:
31     virtual std::string ToString() const = 0;
32 };
33 //-----------------------------------------------------------------------------
34 template<typename varT>
35 class USER_PROPERTY : USER_PROPERTY_BASE {
36 public:
37     USER_PROPERTY(varT & val);
38     virtual ~USER_PROPERTY();
39
40     void Set(const varT & rvalue);
41
42     USER_PROPERTY<varT> & operator= (const varT & rvalue);
43
44     const varT * operator&() const throw() { return &value; }
45     const varT & ConstData() const throw() { return value; }
46
47     operator const varT&() const throw() { return value; }
48
49     void    AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
50     void    DelBeforeNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n);
51
52     void    AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
53     void    DelAfterNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n);
54
55     time_t  ModificationTime() const throw() { return modificationTime; }
56     void    ModifyTime() throw();
57
58     std::string ToString() const;
59 private:
60     varT & value;
61     time_t modificationTime;
62     std::set<PROPERTY_NOTIFIER_BASE<varT> *> beforeNotifiers;
63     std::set<PROPERTY_NOTIFIER_BASE<varT> *> afterNotifiers;
64     pthread_mutex_t mutex;
65 };
66 //-----------------------------------------------------------------------------
67 template<typename varT>
68 class USER_PROPERTY_LOGGED: public USER_PROPERTY<varT> {
69 public:
70     USER_PROPERTY_LOGGED(varT & val,
71                          const std::string & n,
72                          bool isPassword,
73                          bool isStat,
74                          STG_LOGGER & logger,
75                          const std::string & sd);
76     virtual ~USER_PROPERTY_LOGGED() {}
77
78     USER_PROPERTY_LOGGED<varT> * GetPointer() throw() { return this; }
79     const varT & Get() const { return USER_PROPERTY<varT>::ConstData(); }
80     const std::string & GetName() const { return name; }
81     bool Set(const varT & val,
82              const ADMIN * admin,
83              const std::string & login,
84              const STORE * store,
85              const std::string & msg = "");
86 private:
87     void WriteAccessDenied(const std::string & login,
88                            const ADMIN * admin,
89                            const std::string & parameter);
90
91     void WriteSuccessChange(const std::string & login,
92                             const ADMIN * admin,
93                             const std::string & parameter,
94                             const std::string & oldValue,
95                             const std::string & newValue,
96                             const std::string & msg,
97                             const STORE * store);
98
99     void OnChange(const std::string & login,
100                   const std::string & paramName,
101                   const std::string & oldValue,
102                   const std::string & newValue,
103                   const ADMIN  * admin);
104
105     STG_LOGGER &      stgLogger;
106     bool              isPassword;
107     bool              isStat;
108     std::string       name;
109     const std::string scriptsDir;
110 };
111 //-----------------------------------------------------------------------------
112 class USER_PROPERTIES : private NONCOPYABLE {
113 /*
114  В этом месте важен порядок следования приватной и открытой частей.
115  Это связано с тем, что часть которая находится в публичной секции
116  по сути является завуалированной ссылкой на закрытую часть. Т.о. нам нужно
117  чтобы конструкторы из закрытой части вызвались раньше открытой. Поэтомому в
118  начале идет закрытая секция
119  * */
120
121 private:
122     USER_STAT stat;
123     USER_CONF conf;
124
125 public:
126     USER_PROPERTIES(const std::string & sd);
127
128     USER_STAT & Stat() { return stat; }
129     USER_CONF & Conf() { return conf; }
130     const USER_STAT & GetStat() const { return stat; }
131     const USER_CONF & GetConf() const { return conf; }
132     void SetStat(const USER_STAT & s) { stat = s; }
133     void SetConf(const USER_CONF & c) { conf = c; }
134
135     void SetProperties(const USER_PROPERTIES & p) { stat = p.stat; conf = p.conf; }
136
137     std::string GetPropertyValue(const std::string & name) const;
138
139     USER_PROPERTY_LOGGED<double>            cash;
140     USER_PROPERTY_LOGGED<DIR_TRAFF>         up;
141     USER_PROPERTY_LOGGED<DIR_TRAFF>         down;
142     USER_PROPERTY_LOGGED<double>            lastCashAdd;
143     USER_PROPERTY_LOGGED<time_t>            passiveTime;
144     USER_PROPERTY_LOGGED<time_t>            lastCashAddTime;
145     USER_PROPERTY_LOGGED<double>            freeMb;
146     USER_PROPERTY_LOGGED<time_t>            lastActivityTime;
147
148     USER_PROPERTY_LOGGED<std::string>       password;
149     USER_PROPERTY_LOGGED<int>               passive;
150     USER_PROPERTY_LOGGED<int>               disabled;
151     USER_PROPERTY_LOGGED<int>               disabledDetailStat;
152     USER_PROPERTY_LOGGED<int>               alwaysOnline;
153     USER_PROPERTY_LOGGED<std::string>       tariffName;
154     USER_PROPERTY_LOGGED<std::string>       nextTariff;
155     USER_PROPERTY_LOGGED<std::string>       address;
156     USER_PROPERTY_LOGGED<std::string>       note;
157     USER_PROPERTY_LOGGED<std::string>       group;
158     USER_PROPERTY_LOGGED<std::string>       email;
159     USER_PROPERTY_LOGGED<std::string>       phone;
160     USER_PROPERTY_LOGGED<std::string>       realName;
161     USER_PROPERTY_LOGGED<double>            credit;
162     USER_PROPERTY_LOGGED<time_t>            creditExpire;
163     USER_PROPERTY_LOGGED<USER_IPS>          ips;
164     USER_PROPERTY_LOGGED<std::string>       userdata0;
165     USER_PROPERTY_LOGGED<std::string>       userdata1;
166     USER_PROPERTY_LOGGED<std::string>       userdata2;
167     USER_PROPERTY_LOGGED<std::string>       userdata3;
168     USER_PROPERTY_LOGGED<std::string>       userdata4;
169     USER_PROPERTY_LOGGED<std::string>       userdata5;
170     USER_PROPERTY_LOGGED<std::string>       userdata6;
171     USER_PROPERTY_LOGGED<std::string>       userdata7;
172     USER_PROPERTY_LOGGED<std::string>       userdata8;
173     USER_PROPERTY_LOGGED<std::string>       userdata9;
174
175     std::map<std::string, USER_PROPERTY_BASE*> params;
176 };
177 //=============================================================================
178
179 //-----------------------------------------------------------------------------
180 //-----------------------------------------------------------------------------
181 //-----------------------------------------------------------------------------
182 template <typename varT>
183 inline
184 USER_PROPERTY<varT>::USER_PROPERTY(varT & val)
185     : value(val),
186       modificationTime(stgTime),
187       beforeNotifiers(),
188       afterNotifiers(),
189       mutex()
190 {
191 pthread_mutex_init(&mutex, NULL);
192 }
193 //-----------------------------------------------------------------------------
194 template <typename varT>
195 inline
196 USER_PROPERTY<varT>::~USER_PROPERTY()
197 {
198 pthread_mutex_destroy(&mutex);
199 }
200 //-----------------------------------------------------------------------------
201 template <typename varT>
202 inline
203 void USER_PROPERTY<varT>::ModifyTime() throw()
204 {
205 modificationTime = stgTime;
206 }
207 //-----------------------------------------------------------------------------
208 template <typename varT>
209 inline
210 void USER_PROPERTY<varT>::Set(const varT & rvalue)
211 {
212 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
213
214 typename std::set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
215
216 varT oldVal = value;
217
218 ni = beforeNotifiers.begin();
219 while (ni != beforeNotifiers.end())
220     (*ni++)->Notify(oldVal, rvalue);
221
222 value = rvalue;
223 modificationTime = stgTime;
224
225 ni = afterNotifiers.begin();
226 while (ni != afterNotifiers.end())
227     (*ni++)->Notify(oldVal, rvalue);
228 }
229 //-----------------------------------------------------------------------------
230 template <typename varT>
231 inline
232 USER_PROPERTY<varT> & USER_PROPERTY<varT>::operator= (const varT & newValue)
233 {
234 Set(newValue);
235 return *this;
236 }
237 //-----------------------------------------------------------------------------
238 template <typename varT>
239 inline
240 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
241 {
242 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
243 beforeNotifiers.insert(n);
244 }
245 //-----------------------------------------------------------------------------
246 template <typename varT>
247 inline
248 void USER_PROPERTY<varT>::DelBeforeNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n)
249 {
250 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
251 beforeNotifiers.erase(const_cast<PROPERTY_NOTIFIER_BASE<varT> *>(n));
252 }
253 //-----------------------------------------------------------------------------
254 template <typename varT>
255 inline
256 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
257 {
258 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
259 afterNotifiers.insert(n);
260 }
261 //-----------------------------------------------------------------------------
262 template <typename varT>
263 inline
264 void USER_PROPERTY<varT>::DelAfterNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n)
265 {
266 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
267 afterNotifiers.erase(const_cast<PROPERTY_NOTIFIER_BASE<varT> *>(n));
268 }
269 //-----------------------------------------------------------------------------
270 //-----------------------------------------------------------------------------
271 //-----------------------------------------------------------------------------
272 template <typename varT>
273 inline
274 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(varT & val,
275                                                  const std::string & n,
276                                                  bool isPass,
277                                                  bool isSt,
278                                                  STG_LOGGER & logger,
279                                                  const std::string & sd)
280
281     : USER_PROPERTY<varT>(val),
282       stgLogger(logger),
283       isPassword(isPass),
284       isStat(isSt),
285       name(n),
286       scriptsDir(sd)
287 {
288 }
289 //-------------------------------------------------------------------------
290 template <typename varT>
291 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
292                                      const ADMIN * admin,
293                                      const std::string & login,
294                                      const STORE * store,
295                                      const std::string & msg)
296 {
297 const PRIV * priv = admin->GetPriv();
298
299 if ((priv->userConf && !isStat) ||
300     (priv->userStat && isStat) ||
301     (priv->userPasswd && isPassword) ||
302     (priv->userCash && name == "cash"))
303     {
304     std::stringstream oldVal;
305     std::stringstream newVal;
306
307     oldVal.flags(oldVal.flags() | std::ios::fixed);
308     newVal.flags(newVal.flags() | std::ios::fixed);
309
310     oldVal << USER_PROPERTY<varT>::ConstData();
311     newVal << val;
312
313     OnChange(login, name, oldVal.str(), newVal.str(), admin);
314
315     if (isPassword)
316         {
317         WriteSuccessChange(login, admin, name, "******", "******", msg, store);
318         }
319     else
320         {
321         WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
322         }
323     USER_PROPERTY<varT>::Set(val);
324     return true;
325     }
326 else
327     {
328     WriteAccessDenied(login, admin, name);
329     return false;
330     }
331 return true;
332 }
333 //-------------------------------------------------------------------------
334 template <typename varT>
335 inline
336 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const std::string & login,
337                                                    const ADMIN * admin,
338                                                    const std::string & parameter)
339 {
340 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
341           admin->GetLogStr().c_str(), login.c_str(), parameter.c_str());
342 }
343 //-------------------------------------------------------------------------
344 template <typename varT>
345 inline
346 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const std::string & login,
347                                                     const ADMIN * admin,
348                                                     const std::string & parameter,
349                                                     const std::string & oldValue,
350                                                     const std::string & newValue,
351                                                     const std::string & msg,
352                                                     const STORE * store)
353 {
354 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
355           admin->GetLogStr().c_str(),
356           login.c_str(),
357           parameter.c_str(),
358           oldValue.c_str(),
359           newValue.c_str(),
360           msg.c_str());
361
362 store->WriteUserChgLog(login, admin->GetLogin(), admin->GetIP(), parameter, oldValue, newValue, msg);
363 }
364 //-------------------------------------------------------------------------
365 template <typename varT>
366 void USER_PROPERTY_LOGGED<varT>::OnChange(const std::string & login,
367                                           const std::string & paramName,
368                                           const std::string & oldValue,
369                                           const std::string & newValue,
370                                           const ADMIN * admin)
371 {
372 std::string filePath = scriptsDir + "/OnChange";
373
374 if (access(filePath.c_str(), X_OK) == 0)
375     {
376     std::string execString("\"" + filePath + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin->GetLogin() + "\" \"" + admin->GetIPStr() + "\"");
377     ScriptExec(execString.c_str());
378     }
379 else
380     {
381     stgLogger("Script OnChange cannot be executed. File %s not found.", filePath.c_str());
382     }
383 }
384 //-------------------------------------------------------------------------
385 //-------------------------------------------------------------------------
386 //-------------------------------------------------------------------------
387 std::string USER_PROPERTIES::GetPropertyValue(const std::string & name) const
388 {
389     std::map<std::string, USER_PROPERTY_BASE*>::iterator it = params.find(name);
390     if (it != params.end()) return it->second.ToString();
391     else return "";
392 }
393 //-------------------------------------------------------------------------
394 //-------------------------------------------------------------------------
395 //-------------------------------------------------------------------------
396 template<typename varT>
397 inline
398 std::ostream & operator<< (std::ostream & stream, const USER_PROPERTY<varT> & value)
399 {
400 return stream << value.ConstData();
401 }
402 //-----------------------------------------------------------------------------
403 template<typename varT>
404 std::string USER_PROPERTY<varT>::ToString() const
405 {
406 std::stringstream stream;
407 stream << value;
408 return stream.str();
409 }
410 #endif // USER_PROPERTY_H