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 const PRIV * priv = admin->GetPriv();
332 string adm_login = admin->GetLogin();
333 string adm_ip = admin->GetIPStr();
335 if ((priv->userConf && !isStat) || (priv->userStat && isStat) || (priv->userPasswd && isPassword) || (priv->userCash && name == "cash"))
340 oldVal.flags(oldVal.flags() | ios::fixed);
341 newVal.flags(newVal.flags() | ios::fixed);
343 oldVal << USER_PROPERTY<varT>::ConstData();
346 OnChange(login, name, oldVal.str(), newVal.str(), admin);
350 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
354 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
356 USER_PROPERTY<varT>::Set(val);
361 WriteAccessDenied(login, admin, name);
366 //-------------------------------------------------------------------------
367 template <typename varT>
368 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const string & login,
370 const string & parameter)
372 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
373 admin->GetLogStr().c_str(), login.c_str(), parameter.c_str());
375 //-------------------------------------------------------------------------
376 template <typename varT>
377 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const string & login,
379 const string & parameter,
380 const string & oldValue,
381 const string & newValue,
385 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
386 admin->GetLogStr().c_str(),
393 store->WriteUserChgLog(login, admin->GetLogin(), admin->GetIP(), parameter, oldValue, newValue, msg);
395 //-------------------------------------------------------------------------
396 template <typename varT>
397 void USER_PROPERTY_LOGGED<varT>::OnChange(const string & login,
398 const string & paramName,
399 const string & oldValue,
400 const string & newValue,
405 str1 = settings->GetConfDir() + "/OnChange";
407 if (access(str1.c_str(), X_OK) == 0)
409 string str2("\"" + str1 + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin->GetLogin() + "\" \"" + admin->GetIPStr() + "\"");
414 stgLogger("Script OnChange cannot be executed. File %s not found.", str1.c_str());
417 //-------------------------------------------------------------------------
418 //-------------------------------------------------------------------------
419 //-------------------------------------------------------------------------
420 /*template<typename varT>
421 stringstream & operator<< (stringstream & s, const USER_PROPERTY<varT> & v)
426 //-----------------------------------------------------------------------------
427 template<typename varT>
428 ostream & operator<< (ostream & stream, const USER_PROPERTY<varT> & value)
430 return stream << value.ConstData();
432 //-----------------------------------------------------------------------------
434 #endif // USER_PROPERTY_H