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);
134 USER_PROPERTY_LOGGED<double> cash;
135 USER_PROPERTY_LOGGED<DIR_TRAFF> up;
136 USER_PROPERTY_LOGGED<DIR_TRAFF> down;
137 USER_PROPERTY_LOGGED<double> lastCashAdd;
138 USER_PROPERTY_LOGGED<time_t> passiveTime;
139 USER_PROPERTY_LOGGED<time_t> lastCashAddTime;
140 USER_PROPERTY_LOGGED<double> freeMb;
141 USER_PROPERTY_LOGGED<time_t> lastActivityTime;
143 USER_PROPERTY_LOGGED<string> password;
144 USER_PROPERTY_LOGGED<int> passive;
145 USER_PROPERTY_LOGGED<int> disabled;
146 USER_PROPERTY_LOGGED<int> disabledDetailStat;
147 USER_PROPERTY_LOGGED<int> alwaysOnline;
148 USER_PROPERTY_LOGGED<string> tariffName;
149 USER_PROPERTY_LOGGED<string> nextTariff;
150 USER_PROPERTY_LOGGED<string> address;
151 USER_PROPERTY_LOGGED<string> note;
152 USER_PROPERTY_LOGGED<string> group;
153 USER_PROPERTY_LOGGED<string> email;
154 USER_PROPERTY_LOGGED<string> phone;
155 USER_PROPERTY_LOGGED<string> realName;
156 USER_PROPERTY_LOGGED<double> credit;
157 USER_PROPERTY_LOGGED<time_t> creditExpire;
158 USER_PROPERTY_LOGGED<USER_IPS> ips;
159 USER_PROPERTY_LOGGED<string> userdata0;
160 USER_PROPERTY_LOGGED<string> userdata1;
161 USER_PROPERTY_LOGGED<string> userdata2;
162 USER_PROPERTY_LOGGED<string> userdata3;
163 USER_PROPERTY_LOGGED<string> userdata4;
164 USER_PROPERTY_LOGGED<string> userdata5;
165 USER_PROPERTY_LOGGED<string> userdata6;
166 USER_PROPERTY_LOGGED<string> userdata7;
167 USER_PROPERTY_LOGGED<string> userdata8;
168 USER_PROPERTY_LOGGED<string> userdata9;
171 //=============================================================================
173 //-----------------------------------------------------------------------------
174 //-----------------------------------------------------------------------------
175 //-----------------------------------------------------------------------------
176 template <typename varT>
177 USER_PROPERTY<varT>::USER_PROPERTY(varT& val)
181 pthread_mutex_init(&mutex, NULL);
182 modificationTime = stgTime;
184 //-----------------------------------------------------------------------------
185 template <typename varT>
186 USER_PROPERTY<varT>::~USER_PROPERTY()
189 //-----------------------------------------------------------------------------
190 template <typename varT>
191 void USER_PROPERTY<varT>::ModifyTime() throw()
193 modificationTime = stgTime;
195 //-----------------------------------------------------------------------------
196 template <typename varT>
197 USER_PROPERTY<varT>& USER_PROPERTY<varT>::operator= (const varT& newValue)
199 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
203 if (value == newValue)
206 typename set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
208 //printf("USER_PROPERTY<varT>::operator= (const varT& rhs)\n");
212 ni = beforeNotifiers.begin();
213 while (ni != beforeNotifiers.end())
214 (*ni++)->Notify(oldVal, newValue);
217 modificationTime = stgTime;
219 ni = afterNotifiers.begin();
220 while (ni != afterNotifiers.end())
221 (*ni++)->Notify(oldVal, newValue);
225 //-----------------------------------------------------------------------------
226 template <typename varT>
227 USER_PROPERTY<varT>& USER_PROPERTY<varT>::operator-= (const varT& delta)
229 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
231 typename set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
235 ni = beforeNotifiers.begin();
236 while (ni != beforeNotifiers.end())
237 (*ni++)->Notify(oldVal, oldVal - delta);
240 modificationTime = stgTime;
242 ni = afterNotifiers.begin();
243 while (ni != afterNotifiers.end())
244 (*ni++)->Notify(oldVal, value);
248 //-----------------------------------------------------------------------------
249 template <typename varT>
250 const varT * USER_PROPERTY<varT>::operator&() const throw()
252 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
255 //-----------------------------------------------------------------------------
256 template <typename varT>
257 const varT& USER_PROPERTY<varT>::ConstData() const throw()
259 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
262 //-----------------------------------------------------------------------------
263 /*template <typename varT>
264 bool USER_PROPERTY<varT>::IsEmpty() const throw()
266 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
269 //-----------------------------------------------------------------------------
270 template <typename varT>
271 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
273 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
274 beforeNotifiers.insert(n);
276 //-----------------------------------------------------------------------------
277 template <typename varT>
278 void USER_PROPERTY<varT>::DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
280 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
281 beforeNotifiers.erase(n);
283 //-----------------------------------------------------------------------------
284 template <typename varT>
285 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
287 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
288 afterNotifiers.insert(n);
290 //-----------------------------------------------------------------------------
291 template <typename varT>
292 void USER_PROPERTY<varT>::DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
294 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
295 afterNotifiers.erase(n);
297 //-----------------------------------------------------------------------------
298 template <typename varT>
299 time_t USER_PROPERTY<varT>::ModificationTime() const throw()
301 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
302 return modificationTime;
304 //-----------------------------------------------------------------------------
305 //-----------------------------------------------------------------------------
306 //-----------------------------------------------------------------------------
307 template <typename varT>
308 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(
316 :USER_PROPERTY<varT>(val),
319 pthread_mutex_init(&mutex, NULL);
320 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
321 USER_PROPERTY<varT>::value = val;
327 //-----------------------------------------------------------------------------
328 template <typename varT>
329 USER_PROPERTY_LOGGED<varT>::~USER_PROPERTY_LOGGED()
331 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
333 //-----------------------------------------------------------------------------
334 template <typename varT>
335 USER_PROPERTY_LOGGED<varT> * USER_PROPERTY_LOGGED<varT>::GetPointer() throw()
337 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
340 //-----------------------------------------------------------------------------
341 template <typename varT>
342 const varT & USER_PROPERTY_LOGGED<varT>::Get() const
344 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
345 return USER_PROPERTY<varT>::value;
347 //-------------------------------------------------------------------------
348 template <typename varT>
349 const string & USER_PROPERTY_LOGGED<varT>::GetName() const
351 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
354 //-------------------------------------------------------------------------
355 template <typename varT>
356 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
358 const string & login,
359 const BASE_STORE * store,
362 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
364 //cout << "USER_PROPERTY_LOGGED " << val << endl;
366 //modificationTime = stgTime;
368 const PRIV * priv = admin.GetPriv();
369 string adm_login = admin.GetLogin();
370 string adm_ip = admin.GetAdminIPStr();
372 if ((priv->userConf && !isStat) || (priv->userStat && isStat) || (priv->userPasswd && isPassword) || (priv->userCash && name == "cash"))
377 oldVal.flags(oldVal.flags() | ios::fixed);
378 newVal.flags(newVal.flags() | ios::fixed);
380 oldVal << USER_PROPERTY<varT>::value;
383 OnChange(login, name, oldVal.str(), newVal.str(), admin);
387 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
391 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
393 USER_PROPERTY<varT>::operator =(val);
398 WriteAccessDenied(login, admin, name);
403 //-------------------------------------------------------------------------
404 template <typename varT>
405 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const string & login,
407 const string & parameter)
409 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
410 admin.GetLogStr().c_str(), login.c_str(), parameter.c_str());
412 //-------------------------------------------------------------------------
413 template <typename varT>
414 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const string & login,
416 const string & parameter,
417 const string & oldValue,
418 const string & newValue,
420 const BASE_STORE * store)
422 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
423 admin.GetLogStr().c_str(),
431 /*char userLogMsg[2048];
432 sprintf(userLogMsg, "\'%s\' parameter changed from \'%s\' to \'%s\'. %s",
433 parameter.c_str(), oldValue.c_str(),
434 newValue.c_str(), msg.c_str());*/
435 store->WriteUserChgLog(login, admin.GetLogin(), admin.GetAdminIP(), parameter, oldValue, newValue, msg);
436 //store->WriteLogString(userLogMsg, login);
438 //-------------------------------------------------------------------------
439 template <typename varT>
440 void USER_PROPERTY_LOGGED<varT>::OnChange(const string & login,
441 const string & paramName,
442 const string & oldValue,
443 const string & newValue,
448 str1 = settings->GetConfDir() + "/OnChange";
450 if (access(str1.c_str(), X_OK) == 0)
452 string str2("\"" + str1 + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\"");
457 stgLogger("Script OnChange cannot be executed. File %s not found.", str1.c_str());
460 //-------------------------------------------------------------------------
461 //-------------------------------------------------------------------------
462 //-------------------------------------------------------------------------
463 template<typename varT>
464 stringstream & operator<< (stringstream & s, const USER_PROPERTY<varT> & v)
469 //-----------------------------------------------------------------------------
470 template<typename varT>
471 ostream & operator<< (ostream & o, const USER_PROPERTY<varT> & v)
473 return o << v.ConstData();
475 //-----------------------------------------------------------------------------
478 #endif // USER_PROPERTY_H