3 $Date: 2010/09/13 05:54:43 $
7 #ifndef USER_PROPERTY_H
8 #define USER_PROPERTY_H
18 #include "stg_logger.h"
22 #include "stg_logger.h"
23 #include "stg_locker.h"
24 #include "script_executer.h"
26 extern const volatile time_t stgTime;
28 //-----------------------------------------------------------------------------
29 template<typename varT>
32 USER_PROPERTY(varT& val);
33 virtual ~USER_PROPERTY();
35 void Set(const varT & rvalue);
37 USER_PROPERTY<varT>& operator= (const varT & rvalue);
38 USER_PROPERTY<varT>& operator-= (const varT & rvalue);
40 const varT * operator&() const throw();
41 const varT& ConstData() const throw();
43 operator const varT&() const throw()
48 void AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
49 void DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
51 void AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
52 void DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
54 time_t ModificationTime() const throw();
55 void ModifyTime() throw();
59 time_t modificationTime;
60 set<PROPERTY_NOTIFIER_BASE<varT> *> beforeNotifiers;
61 set<PROPERTY_NOTIFIER_BASE<varT> *> afterNotifiers;
62 mutable pthread_mutex_t mutex;
64 //-----------------------------------------------------------------------------
65 template<typename varT>
66 class USER_PROPERTY_LOGGED: public USER_PROPERTY<varT> {
68 USER_PROPERTY_LOGGED(varT & val,
74 virtual ~USER_PROPERTY_LOGGED();
76 USER_PROPERTY_LOGGED<varT> * GetPointer() throw();
77 const varT & Get() const;
78 const string & GetName() const;
79 bool Set(const varT & val,
83 const string & msg = "");
85 void WriteAccessDenied(const string & login,
87 const string & parameter);
89 void WriteSuccessChange(const string & login,
91 const string & parameter,
92 const string & oldValue,
93 const string & newValue,
97 void OnChange(const string & login,
98 const string & paramName,
99 const string & oldValue,
100 const string & newValue,
101 const ADMIN & admin);
103 string name; // parameter name. needed for logging
104 bool isPassword; // is parameter password. when true, it will be logged as *******
105 bool isStat; // is parameter a stat data or conf data?
106 mutable pthread_mutex_t mutex;
107 STG_LOGGER & stgLogger; // server's logger
108 const SETTINGS * settings;
110 //-----------------------------------------------------------------------------
111 class USER_PROPERTIES {
113 В этом месте важен порядок следования приватной и открытой частей.
114 Это связано с тем, что часть которая находится в публичной секции
115 по сути является завуалированной ссылкой на закрытую часть. Т.о. нам нужно
116 чтобы конструкторы из закрытой части вызвались раньше открытой. Поэтомому в
117 начале идет закрытая секция
125 USER_PROPERTIES(const SETTINGS * settings);
127 USER_STAT & Stat() { return stat; }
128 USER_CONF & Conf() { return conf; }
129 const USER_STAT & GetStat() const { return stat; }
130 const USER_CONF & GetConf() const { return conf; }
131 void SetStat(const USER_STAT & s) { stat = s; }
132 void SetConf(const USER_CONF & c) { conf = c; }
134 void SetProperties(const USER_PROPERTIES & p) { stat = p.stat; conf = p.conf; }
136 USER_PROPERTY_LOGGED<double> cash;
137 USER_PROPERTY_LOGGED<DIR_TRAFF> up;
138 USER_PROPERTY_LOGGED<DIR_TRAFF> down;
139 USER_PROPERTY_LOGGED<double> lastCashAdd;
140 USER_PROPERTY_LOGGED<time_t> passiveTime;
141 USER_PROPERTY_LOGGED<time_t> lastCashAddTime;
142 USER_PROPERTY_LOGGED<double> freeMb;
143 USER_PROPERTY_LOGGED<time_t> lastActivityTime;
145 USER_PROPERTY_LOGGED<string> password;
146 USER_PROPERTY_LOGGED<int> passive;
147 USER_PROPERTY_LOGGED<int> disabled;
148 USER_PROPERTY_LOGGED<int> disabledDetailStat;
149 USER_PROPERTY_LOGGED<int> alwaysOnline;
150 USER_PROPERTY_LOGGED<string> tariffName;
151 USER_PROPERTY_LOGGED<string> nextTariff;
152 USER_PROPERTY_LOGGED<string> address;
153 USER_PROPERTY_LOGGED<string> note;
154 USER_PROPERTY_LOGGED<string> group;
155 USER_PROPERTY_LOGGED<string> email;
156 USER_PROPERTY_LOGGED<string> phone;
157 USER_PROPERTY_LOGGED<string> realName;
158 USER_PROPERTY_LOGGED<double> credit;
159 USER_PROPERTY_LOGGED<time_t> creditExpire;
160 USER_PROPERTY_LOGGED<USER_IPS> ips;
161 USER_PROPERTY_LOGGED<string> userdata0;
162 USER_PROPERTY_LOGGED<string> userdata1;
163 USER_PROPERTY_LOGGED<string> userdata2;
164 USER_PROPERTY_LOGGED<string> userdata3;
165 USER_PROPERTY_LOGGED<string> userdata4;
166 USER_PROPERTY_LOGGED<string> userdata5;
167 USER_PROPERTY_LOGGED<string> userdata6;
168 USER_PROPERTY_LOGGED<string> userdata7;
169 USER_PROPERTY_LOGGED<string> userdata8;
170 USER_PROPERTY_LOGGED<string> userdata9;
172 //=============================================================================
174 //-----------------------------------------------------------------------------
175 //-----------------------------------------------------------------------------
176 //-----------------------------------------------------------------------------
177 template <typename varT>
178 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 void USER_PROPERTY<varT>::Set(const varT & rvalue)
199 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
201 typename set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
205 ni = beforeNotifiers.begin();
206 while (ni != beforeNotifiers.end())
207 (*ni++)->Notify(oldVal, rvalue);
210 modificationTime = stgTime;
212 ni = afterNotifiers.begin();
213 while (ni != afterNotifiers.end())
214 (*ni++)->Notify(oldVal, rvalue);
216 //-----------------------------------------------------------------------------
217 template <typename varT>
218 USER_PROPERTY<varT>& USER_PROPERTY<varT>::operator= (const varT & newValue)
223 //-----------------------------------------------------------------------------
224 template <typename varT>
225 USER_PROPERTY<varT>& USER_PROPERTY<varT>::operator-= (const varT & delta)
227 varT newValue = ConstData() - delta;
231 //-----------------------------------------------------------------------------
232 template <typename varT>
233 const varT * USER_PROPERTY<varT>::operator&() const throw()
237 //-----------------------------------------------------------------------------
238 template <typename varT>
239 const varT& USER_PROPERTY<varT>::ConstData() const throw()
243 //-----------------------------------------------------------------------------
244 template <typename varT>
245 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
247 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
248 beforeNotifiers.insert(n);
250 //-----------------------------------------------------------------------------
251 template <typename varT>
252 void USER_PROPERTY<varT>::DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
254 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
255 beforeNotifiers.erase(n);
257 //-----------------------------------------------------------------------------
258 template <typename varT>
259 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
261 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
262 afterNotifiers.insert(n);
264 //-----------------------------------------------------------------------------
265 template <typename varT>
266 void USER_PROPERTY<varT>::DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
268 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
269 afterNotifiers.erase(n);
271 //-----------------------------------------------------------------------------
272 template <typename varT>
273 time_t USER_PROPERTY<varT>::ModificationTime() const throw()
275 return modificationTime;
277 //-----------------------------------------------------------------------------
278 //-----------------------------------------------------------------------------
279 //-----------------------------------------------------------------------------
280 template <typename varT>
281 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(varT& val,
288 : USER_PROPERTY<varT>(val),
291 pthread_mutex_init(&mutex, NULL);
292 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
298 //-----------------------------------------------------------------------------
299 template <typename varT>
300 USER_PROPERTY_LOGGED<varT>::~USER_PROPERTY_LOGGED()
303 //-----------------------------------------------------------------------------
304 template <typename varT>
305 USER_PROPERTY_LOGGED<varT> * USER_PROPERTY_LOGGED<varT>::GetPointer() throw()
309 //-----------------------------------------------------------------------------
310 template <typename varT>
311 const varT & USER_PROPERTY_LOGGED<varT>::Get() const
313 return USER_PROPERTY<varT>::ConstData();
315 //-------------------------------------------------------------------------
316 template <typename varT>
317 const string & USER_PROPERTY_LOGGED<varT>::GetName() const
321 //-------------------------------------------------------------------------
322 template <typename varT>
323 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
325 const string & login,
329 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
331 //cout << "USER_PROPERTY_LOGGED " << val << endl;
333 //modificationTime = stgTime;
335 const PRIV * priv = admin.GetPriv();
336 string adm_login = admin.GetLogin();
337 string adm_ip = admin.GetIPStr();
339 if ((priv->userConf && !isStat) || (priv->userStat && isStat) || (priv->userPasswd && isPassword) || (priv->userCash && name == "cash"))
344 oldVal.flags(oldVal.flags() | ios::fixed);
345 newVal.flags(newVal.flags() | ios::fixed);
347 oldVal << USER_PROPERTY<varT>::ConstData();
350 OnChange(login, name, oldVal.str(), newVal.str(), admin);
354 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
358 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
360 USER_PROPERTY<varT>::Set(val);
365 WriteAccessDenied(login, admin, name);
370 //-------------------------------------------------------------------------
371 template <typename varT>
372 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const string & login,
374 const string & parameter)
376 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
377 admin.GetLogStr().c_str(), login.c_str(), parameter.c_str());
379 //-------------------------------------------------------------------------
380 template <typename varT>
381 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const string & login,
383 const string & parameter,
384 const string & oldValue,
385 const string & newValue,
389 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
390 admin.GetLogStr().c_str(),
397 store->WriteUserChgLog(login, admin.GetLogin(), admin.GetIP(), parameter, oldValue, newValue, msg);
399 //-------------------------------------------------------------------------
400 template <typename varT>
401 void USER_PROPERTY_LOGGED<varT>::OnChange(const string & login,
402 const string & paramName,
403 const string & oldValue,
404 const string & newValue,
409 str1 = settings->GetConfDir() + "/OnChange";
411 if (access(str1.c_str(), X_OK) == 0)
413 string str2("\"" + str1 + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin.GetLogin() + "\" \"" + admin.GetIPStr() + "\"");
418 stgLogger("Script OnChange cannot be executed. File %s not found.", str1.c_str());
421 //-------------------------------------------------------------------------
422 //-------------------------------------------------------------------------
423 //-------------------------------------------------------------------------
424 /*template<typename varT>
425 stringstream & operator<< (stringstream & s, const USER_PROPERTY<varT> & v)
430 //-----------------------------------------------------------------------------
431 template<typename varT>
432 ostream & operator<< (ostream & stream, const USER_PROPERTY<varT> & value)
434 return stream << value.ConstData();
436 //-----------------------------------------------------------------------------
438 #endif // USER_PROPERTY_H