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;
218 ni = beforeNotifiers.begin();
219 while (ni != beforeNotifiers.end())
220 (*ni++)->Notify(oldVal, newValue);
223 modificationTime = stgTime;
225 ni = afterNotifiers.begin();
226 while (ni != afterNotifiers.end())
227 (*ni++)->Notify(oldVal, newValue);
231 //-----------------------------------------------------------------------------
232 template <typename varT>
233 USER_PROPERTY<varT>& USER_PROPERTY<varT>::operator-= (const varT& delta)
235 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
237 typename set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
241 ni = beforeNotifiers.begin();
242 while (ni != beforeNotifiers.end())
243 (*ni++)->Notify(oldVal, oldVal - delta);
246 modificationTime = stgTime;
248 ni = afterNotifiers.begin();
249 while (ni != afterNotifiers.end())
250 (*ni++)->Notify(oldVal, value);
254 //-----------------------------------------------------------------------------
255 template <typename varT>
256 const varT * USER_PROPERTY<varT>::operator&() const throw()
258 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
261 //-----------------------------------------------------------------------------
262 template <typename varT>
263 const varT& USER_PROPERTY<varT>::ConstData() const throw()
265 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
268 //-----------------------------------------------------------------------------
269 /*template <typename varT>
270 bool USER_PROPERTY<varT>::IsEmpty() const throw()
272 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
275 //-----------------------------------------------------------------------------
276 template <typename varT>
277 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
279 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
280 beforeNotifiers.insert(n);
282 //-----------------------------------------------------------------------------
283 template <typename varT>
284 void USER_PROPERTY<varT>::DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
286 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
287 beforeNotifiers.erase(n);
289 //-----------------------------------------------------------------------------
290 template <typename varT>
291 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
293 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
294 afterNotifiers.insert(n);
296 //-----------------------------------------------------------------------------
297 template <typename varT>
298 void USER_PROPERTY<varT>::DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
300 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
301 afterNotifiers.erase(n);
303 //-----------------------------------------------------------------------------
304 template <typename varT>
305 time_t USER_PROPERTY<varT>::ModificationTime() const throw()
307 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
308 return modificationTime;
310 //-----------------------------------------------------------------------------
311 //-----------------------------------------------------------------------------
312 //-----------------------------------------------------------------------------
313 template <typename varT>
314 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(
322 :USER_PROPERTY<varT>(val),
325 pthread_mutex_init(&mutex, NULL);
326 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
327 USER_PROPERTY<varT>::value = val;
333 //-----------------------------------------------------------------------------
334 template <typename varT>
335 USER_PROPERTY_LOGGED<varT>::~USER_PROPERTY_LOGGED()
337 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
339 //-----------------------------------------------------------------------------
340 template <typename varT>
341 USER_PROPERTY_LOGGED<varT> * USER_PROPERTY_LOGGED<varT>::GetPointer() throw()
343 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
346 //-----------------------------------------------------------------------------
347 template <typename varT>
348 const varT & USER_PROPERTY_LOGGED<varT>::Get() const
350 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
351 return USER_PROPERTY<varT>::value;
353 //-------------------------------------------------------------------------
354 template <typename varT>
355 const string & USER_PROPERTY_LOGGED<varT>::GetName() const
357 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
360 //-------------------------------------------------------------------------
361 template <typename varT>
362 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
364 const string & login,
365 const BASE_STORE * store,
368 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
370 //cout << "USER_PROPERTY_LOGGED " << val << endl;
372 //modificationTime = stgTime;
374 const PRIV * priv = admin.GetPriv();
375 string adm_login = admin.GetLogin();
376 string adm_ip = admin.GetAdminIPStr();
378 if ((priv->userConf && !isStat) || (priv->userStat && isStat) || (priv->userPasswd && isPassword) || (priv->userCash && name == "cash"))
383 oldVal.flags(oldVal.flags() | ios::fixed);
384 newVal.flags(newVal.flags() | ios::fixed);
386 oldVal << USER_PROPERTY<varT>::value;
389 OnChange(login, name, oldVal.str(), newVal.str(), admin);
393 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
397 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
399 USER_PROPERTY<varT>::operator =(val);
404 WriteAccessDenied(login, admin, name);
409 //-------------------------------------------------------------------------
410 template <typename varT>
411 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const string & login,
413 const string & parameter)
415 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
416 admin.GetLogStr().c_str(), login.c_str(), parameter.c_str());
418 //-------------------------------------------------------------------------
419 template <typename varT>
420 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const string & login,
422 const string & parameter,
423 const string & oldValue,
424 const string & newValue,
426 const BASE_STORE * store)
428 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
429 admin.GetLogStr().c_str(),
436 store->WriteUserChgLog(login, admin.GetLogin(), admin.GetAdminIP(), parameter, oldValue, newValue, msg);
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 + "\" \"" + admin.GetLogin() + "\" \"" + admin.GetAdminIPStr() + "\"");
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