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