3 $Date: 2010/09/13 05:54:43 $
8 #ifndef USER_PROPERTY_H
9 #define USER_PROPERTY_H
18 #include "base_store.h"
19 #include "stg_logger.h"
23 #include "stg_logger.h"
24 #include "stg_locker.h"
25 #include "script_executer.h"
29 extern const volatile time_t stgTime;
31 //-----------------------------------------------------------------------------
32 template<typename varT>
36 USER_PROPERTY(varT& val);
37 USER_PROPERTY<varT>& operator= (const varT&);
38 USER_PROPERTY<varT>& operator-= (const varT&);
39 virtual ~USER_PROPERTY();
41 const varT * operator&() const throw();
42 const varT& ConstData() const throw();
44 operator const varT&() const throw()
49 //bool IsEmpty() const throw();
51 void AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
52 void DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
54 void AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
55 void DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
57 time_t ModificationTime() const throw();
58 void ModifyTime() throw();
62 time_t modificationTime;
63 //typedef set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator notifier_iter_t;
64 mutable set<PROPERTY_NOTIFIER_BASE<varT> *> beforeNotifiers;
65 mutable set<PROPERTY_NOTIFIER_BASE<varT> *> afterNotifiers;
66 mutable pthread_mutex_t mutex;
68 //-----------------------------------------------------------------------------
69 template<typename varT>
70 class USER_PROPERTY_LOGGED: public USER_PROPERTY<varT>
73 USER_PROPERTY_LOGGED(varT& val,
79 virtual ~USER_PROPERTY_LOGGED();
81 //operator const varT&() const throw();;
82 USER_PROPERTY_LOGGED<varT> * GetPointer() throw();
83 const varT & Get() const;
84 const string & GetName() const;
85 bool Set(const varT & val,
88 const BASE_STORE * store,
89 const string & msg = "");
91 void WriteAccessDenied(const string & login,
93 const string & parameter);
95 void WriteSuccessChange(const string & login,
97 const string & parameter,
98 const string & oldValue,
99 const string & newValue,
101 const BASE_STORE * store);
103 void OnChange(const string & login,
104 const string & paramName,
105 const string & oldValue,
106 const string & newValue,
107 const ADMIN & admin);
109 string name; // parameter name. needed for logging
110 bool isPassword; // is parameter password. when true, it will be logged as *******
111 bool isStat; // is parameter a stat data or conf data?
112 mutable pthread_mutex_t mutex;
113 STG_LOGGER & stgLogger; // server's logger
114 const SETTINGS * settings;
116 //-----------------------------------------------------------------------------
117 class USER_PROPERTIES
121 В этом месте важен порядок следования приватной и открытой частей.
122 Это связано с тем, что часть которая находится в публичной секции
123 по сути является завуалированной ссылкой на закрытую часть. Т.о. нам нужно
124 чтобы конструкторы из закрытой части вызвались раньше открытой. Поэтомому в
125 начале идет закрытая секция
133 USER_PROPERTIES(const SETTINGS * settings);
135 const USER_STAT & GetStat() const { return stat; }
136 const USER_CONF & GetConf() const { return conf; }
137 void SetStat(const USER_STAT & s) { stat = s; }
138 void SetConf(const USER_CONF & c) { conf = c; }
140 void SetProperties(const USER_PROPERTIES & p) { stat = p.stat; conf = p.conf; }
142 USER_PROPERTY_LOGGED<double> cash;
143 USER_PROPERTY_LOGGED<DIR_TRAFF> up;
144 USER_PROPERTY_LOGGED<DIR_TRAFF> down;
145 USER_PROPERTY_LOGGED<double> lastCashAdd;
146 USER_PROPERTY_LOGGED<time_t> passiveTime;
147 USER_PROPERTY_LOGGED<time_t> lastCashAddTime;
148 USER_PROPERTY_LOGGED<double> freeMb;
149 USER_PROPERTY_LOGGED<time_t> lastActivityTime;
151 USER_PROPERTY_LOGGED<string> password;
152 USER_PROPERTY_LOGGED<int> passive;
153 USER_PROPERTY_LOGGED<int> disabled;
154 USER_PROPERTY_LOGGED<int> disabledDetailStat;
155 USER_PROPERTY_LOGGED<int> alwaysOnline;
156 USER_PROPERTY_LOGGED<string> tariffName;
157 USER_PROPERTY_LOGGED<string> nextTariff;
158 USER_PROPERTY_LOGGED<string> address;
159 USER_PROPERTY_LOGGED<string> note;
160 USER_PROPERTY_LOGGED<string> group;
161 USER_PROPERTY_LOGGED<string> email;
162 USER_PROPERTY_LOGGED<string> phone;
163 USER_PROPERTY_LOGGED<string> realName;
164 USER_PROPERTY_LOGGED<double> credit;
165 USER_PROPERTY_LOGGED<time_t> creditExpire;
166 USER_PROPERTY_LOGGED<USER_IPS> ips;
167 USER_PROPERTY_LOGGED<string> userdata0;
168 USER_PROPERTY_LOGGED<string> userdata1;
169 USER_PROPERTY_LOGGED<string> userdata2;
170 USER_PROPERTY_LOGGED<string> userdata3;
171 USER_PROPERTY_LOGGED<string> userdata4;
172 USER_PROPERTY_LOGGED<string> userdata5;
173 USER_PROPERTY_LOGGED<string> userdata6;
174 USER_PROPERTY_LOGGED<string> userdata7;
175 USER_PROPERTY_LOGGED<string> userdata8;
176 USER_PROPERTY_LOGGED<string> userdata9;
179 //=============================================================================
181 //-----------------------------------------------------------------------------
182 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
184 template <typename varT>
185 USER_PROPERTY<varT>::USER_PROPERTY(varT& val)
189 pthread_mutex_init(&mutex, NULL);
190 modificationTime = stgTime;
192 //-----------------------------------------------------------------------------
193 template <typename varT>
194 USER_PROPERTY<varT>::~USER_PROPERTY()
197 //-----------------------------------------------------------------------------
198 template <typename varT>
199 void USER_PROPERTY<varT>::ModifyTime() throw()
201 modificationTime = stgTime;
203 //-----------------------------------------------------------------------------
204 template <typename varT>
205 USER_PROPERTY<varT>& USER_PROPERTY<varT>::operator= (const varT& newValue)
207 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
211 if (value == newValue)
214 typename set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
216 //printf("USER_PROPERTY<varT>::operator= (const varT& rhs)\n");
220 ni = beforeNotifiers.begin();
221 while (ni != beforeNotifiers.end())
222 (*ni++)->Notify(oldVal, newValue);
225 modificationTime = stgTime;
227 ni = afterNotifiers.begin();
228 while (ni != afterNotifiers.end())
229 (*ni++)->Notify(oldVal, newValue);
233 //-----------------------------------------------------------------------------
234 template <typename varT>
235 USER_PROPERTY<varT>& USER_PROPERTY<varT>::operator-= (const varT& delta)
237 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
239 typename set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
243 ni = beforeNotifiers.begin();
244 while (ni != beforeNotifiers.end())
245 (*ni++)->Notify(oldVal, oldVal - delta);
248 modificationTime = stgTime;
250 ni = afterNotifiers.begin();
251 while (ni != afterNotifiers.end())
252 (*ni++)->Notify(oldVal, value);
256 //-----------------------------------------------------------------------------
257 template <typename varT>
258 const varT * USER_PROPERTY<varT>::operator&() const throw()
260 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
263 //-----------------------------------------------------------------------------
264 template <typename varT>
265 const varT& USER_PROPERTY<varT>::ConstData() const throw()
267 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
270 //-----------------------------------------------------------------------------
271 /*template <typename varT>
272 bool USER_PROPERTY<varT>::IsEmpty() const throw()
274 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
277 //-----------------------------------------------------------------------------
278 template <typename varT>
279 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
281 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
282 beforeNotifiers.insert(n);
284 //-----------------------------------------------------------------------------
285 template <typename varT>
286 void USER_PROPERTY<varT>::DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
288 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
289 beforeNotifiers.erase(n);
291 //-----------------------------------------------------------------------------
292 template <typename varT>
293 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
295 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
296 afterNotifiers.insert(n);
298 //-----------------------------------------------------------------------------
299 template <typename varT>
300 void USER_PROPERTY<varT>::DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
302 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
303 afterNotifiers.erase(n);
305 //-----------------------------------------------------------------------------
306 template <typename varT>
307 time_t USER_PROPERTY<varT>::ModificationTime() const throw()
309 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
310 return modificationTime;
312 //-----------------------------------------------------------------------------
313 //-----------------------------------------------------------------------------
314 //-----------------------------------------------------------------------------
315 template <typename varT>
316 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(
324 :USER_PROPERTY<varT>(val),
327 pthread_mutex_init(&mutex, NULL);
328 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
329 USER_PROPERTY<varT>::value = val;
335 //-----------------------------------------------------------------------------
336 template <typename varT>
337 USER_PROPERTY_LOGGED<varT>::~USER_PROPERTY_LOGGED()
339 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
341 //-----------------------------------------------------------------------------
342 template <typename varT>
343 USER_PROPERTY_LOGGED<varT> * USER_PROPERTY_LOGGED<varT>::GetPointer() throw()
345 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
348 //-----------------------------------------------------------------------------
349 template <typename varT>
350 const varT & USER_PROPERTY_LOGGED<varT>::Get() const
352 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
353 return USER_PROPERTY<varT>::value;
355 //-------------------------------------------------------------------------
356 template <typename varT>
357 const string & USER_PROPERTY_LOGGED<varT>::GetName() const
359 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
362 //-------------------------------------------------------------------------
363 template <typename varT>
364 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
366 const string & login,
367 const BASE_STORE * store,
370 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
372 //cout << "USER_PROPERTY_LOGGED " << val << endl;
374 //modificationTime = stgTime;
376 const PRIV * priv = admin.GetPriv();
377 string adm_login = admin.GetLogin();
378 string adm_ip = admin.GetAdminIPStr();
380 if ((priv->userConf && !isStat) || (priv->userStat && isStat) || (priv->userPasswd && isPassword) || (priv->userCash && name == "cash"))
385 oldVal.flags(oldVal.flags() | ios::fixed);
386 newVal.flags(newVal.flags() | ios::fixed);
388 oldVal << USER_PROPERTY<varT>::value;
391 OnChange(login, name, oldVal.str(), newVal.str(), admin);
395 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
399 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
401 USER_PROPERTY<varT>::operator =(val);
406 WriteAccessDenied(login, admin, name);
411 //-------------------------------------------------------------------------
412 template <typename varT>
413 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const string & login,
415 const string & parameter)
417 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
418 admin.GetLogStr().c_str(), login.c_str(), parameter.c_str());
420 //-------------------------------------------------------------------------
421 template <typename varT>
422 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const string & login,
424 const string & parameter,
425 const string & oldValue,
426 const string & newValue,
428 const BASE_STORE * store)
430 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
431 admin.GetLogStr().c_str(),
439 /*char userLogMsg[2048];
440 sprintf(userLogMsg, "\'%s\' parameter changed from \'%s\' to \'%s\'. %s",
441 parameter.c_str(), oldValue.c_str(),
442 newValue.c_str(), msg.c_str());*/
443 store->WriteUserChgLog(login, admin.GetLogin(), admin.GetAdminIP(), parameter, oldValue, newValue, msg);
444 //store->WriteLogString(userLogMsg, login);
446 //-------------------------------------------------------------------------
447 template <typename varT>
448 void USER_PROPERTY_LOGGED<varT>::OnChange(const string & login,
449 const string & paramName,
450 const string & oldValue,
451 const string & newValue,
456 str1 = settings->GetConfDir() + "/OnChange";
458 if (access(str1.c_str(), X_OK) == 0)
460 string str2("\"" + str1 + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\"");
465 stgLogger("Script OnChange cannot be executed. File %s not found.", str1.c_str());
468 //-------------------------------------------------------------------------
469 //-------------------------------------------------------------------------
470 //-------------------------------------------------------------------------
471 template<typename varT>
472 stringstream & operator<< (stringstream & s, const USER_PROPERTY<varT> & v)
477 //-----------------------------------------------------------------------------
478 template<typename varT>
479 ostream & operator<< (ostream & o, const USER_PROPERTY<varT> & v)
481 return o << v.ConstData();
483 //-----------------------------------------------------------------------------
486 #endif // USER_PROPERTY_H