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