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,
73 const std::string & sd);
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 STG_LOGGER & stgLogger; // server's logger
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 string name; // parameter name. needed for logging
107 const std::string scriptsDir;
109 //-----------------------------------------------------------------------------
110 class USER_PROPERTIES {
112 В этом месте важен порядок следования приватной и открытой частей.
113 Это связано с тем, что часть которая находится в публичной секции
114 по сути является завуалированной ссылкой на закрытую часть. Т.о. нам нужно
115 чтобы конструкторы из закрытой части вызвались раньше открытой. Поэтомому в
116 начале идет закрытая секция
124 USER_PROPERTIES(const std::string & sd);
126 USER_STAT & Stat() { return stat; }
127 USER_CONF & Conf() { return conf; }
128 const USER_STAT & GetStat() const { return stat; }
129 const USER_CONF & GetConf() const { return conf; }
130 void SetStat(const USER_STAT & s) { stat = s; }
131 void SetConf(const USER_CONF & c) { conf = c; }
133 void SetProperties(const USER_PROPERTIES & p) { stat = p.stat; conf = p.conf; }
135 USER_PROPERTY_LOGGED<double> cash;
136 USER_PROPERTY_LOGGED<DIR_TRAFF> up;
137 USER_PROPERTY_LOGGED<DIR_TRAFF> down;
138 USER_PROPERTY_LOGGED<double> lastCashAdd;
139 USER_PROPERTY_LOGGED<time_t> passiveTime;
140 USER_PROPERTY_LOGGED<time_t> lastCashAddTime;
141 USER_PROPERTY_LOGGED<double> freeMb;
142 USER_PROPERTY_LOGGED<time_t> lastActivityTime;
144 USER_PROPERTY_LOGGED<string> password;
145 USER_PROPERTY_LOGGED<int> passive;
146 USER_PROPERTY_LOGGED<int> disabled;
147 USER_PROPERTY_LOGGED<int> disabledDetailStat;
148 USER_PROPERTY_LOGGED<int> alwaysOnline;
149 USER_PROPERTY_LOGGED<string> tariffName;
150 USER_PROPERTY_LOGGED<string> nextTariff;
151 USER_PROPERTY_LOGGED<string> address;
152 USER_PROPERTY_LOGGED<string> note;
153 USER_PROPERTY_LOGGED<string> group;
154 USER_PROPERTY_LOGGED<string> email;
155 USER_PROPERTY_LOGGED<string> phone;
156 USER_PROPERTY_LOGGED<string> realName;
157 USER_PROPERTY_LOGGED<double> credit;
158 USER_PROPERTY_LOGGED<time_t> creditExpire;
159 USER_PROPERTY_LOGGED<USER_IPS> ips;
160 USER_PROPERTY_LOGGED<string> userdata0;
161 USER_PROPERTY_LOGGED<string> userdata1;
162 USER_PROPERTY_LOGGED<string> userdata2;
163 USER_PROPERTY_LOGGED<string> userdata3;
164 USER_PROPERTY_LOGGED<string> userdata4;
165 USER_PROPERTY_LOGGED<string> userdata5;
166 USER_PROPERTY_LOGGED<string> userdata6;
167 USER_PROPERTY_LOGGED<string> userdata7;
168 USER_PROPERTY_LOGGED<string> userdata8;
169 USER_PROPERTY_LOGGED<string> userdata9;
171 //=============================================================================
173 //-----------------------------------------------------------------------------
174 //-----------------------------------------------------------------------------
175 //-----------------------------------------------------------------------------
176 template <typename varT>
177 USER_PROPERTY<varT>::USER_PROPERTY(varT& val)
180 pthread_mutex_init(&mutex, NULL);
181 modificationTime = stgTime;
183 //-----------------------------------------------------------------------------
184 template <typename varT>
185 USER_PROPERTY<varT>::~USER_PROPERTY()
188 //-----------------------------------------------------------------------------
189 template <typename varT>
190 void USER_PROPERTY<varT>::ModifyTime() throw()
192 modificationTime = stgTime;
194 //-----------------------------------------------------------------------------
195 template <typename varT>
196 void USER_PROPERTY<varT>::Set(const varT & rvalue)
198 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
200 typename set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
204 ni = beforeNotifiers.begin();
205 while (ni != beforeNotifiers.end())
206 (*ni++)->Notify(oldVal, rvalue);
209 modificationTime = stgTime;
211 ni = afterNotifiers.begin();
212 while (ni != afterNotifiers.end())
213 (*ni++)->Notify(oldVal, rvalue);
215 //-----------------------------------------------------------------------------
216 template <typename varT>
217 USER_PROPERTY<varT>& USER_PROPERTY<varT>::operator= (const varT & newValue)
222 //-----------------------------------------------------------------------------
223 template <typename varT>
224 USER_PROPERTY<varT>& USER_PROPERTY<varT>::operator-= (const varT & delta)
226 varT newValue = ConstData() - delta;
230 //-----------------------------------------------------------------------------
231 template <typename varT>
232 const varT * USER_PROPERTY<varT>::operator&() const throw()
236 //-----------------------------------------------------------------------------
237 template <typename varT>
238 const varT& USER_PROPERTY<varT>::ConstData() const throw()
242 //-----------------------------------------------------------------------------
243 template <typename varT>
244 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
246 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
247 beforeNotifiers.insert(n);
249 //-----------------------------------------------------------------------------
250 template <typename varT>
251 void USER_PROPERTY<varT>::DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
253 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
254 beforeNotifiers.erase(n);
256 //-----------------------------------------------------------------------------
257 template <typename varT>
258 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
260 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
261 afterNotifiers.insert(n);
263 //-----------------------------------------------------------------------------
264 template <typename varT>
265 void USER_PROPERTY<varT>::DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
267 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
268 afterNotifiers.erase(n);
270 //-----------------------------------------------------------------------------
271 template <typename varT>
272 time_t USER_PROPERTY<varT>::ModificationTime() const throw()
274 return modificationTime;
276 //-----------------------------------------------------------------------------
277 //-----------------------------------------------------------------------------
278 //-----------------------------------------------------------------------------
279 template <typename varT>
280 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(varT& val,
285 const std::string & sd)
287 : USER_PROPERTY<varT>(val),
295 //-----------------------------------------------------------------------------
296 template <typename varT>
297 USER_PROPERTY_LOGGED<varT>::~USER_PROPERTY_LOGGED()
300 //-----------------------------------------------------------------------------
301 template <typename varT>
302 USER_PROPERTY_LOGGED<varT> * USER_PROPERTY_LOGGED<varT>::GetPointer() throw()
306 //-----------------------------------------------------------------------------
307 template <typename varT>
308 const varT & USER_PROPERTY_LOGGED<varT>::Get() const
310 return USER_PROPERTY<varT>::ConstData();
312 //-------------------------------------------------------------------------
313 template <typename varT>
314 const string & USER_PROPERTY_LOGGED<varT>::GetName() const
318 //-------------------------------------------------------------------------
319 template <typename varT>
320 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
322 const string & login,
326 const PRIV * priv = admin->GetPriv();
327 string adm_login = admin->GetLogin();
328 string adm_ip = admin->GetIPStr();
330 if ((priv->userConf && !isStat) || (priv->userStat && isStat) || (priv->userPasswd && isPassword) || (priv->userCash && name == "cash"))
335 oldVal.flags(oldVal.flags() | ios::fixed);
336 newVal.flags(newVal.flags() | ios::fixed);
338 oldVal << USER_PROPERTY<varT>::ConstData();
341 OnChange(login, name, oldVal.str(), newVal.str(), admin);
345 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
349 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
351 USER_PROPERTY<varT>::Set(val);
356 WriteAccessDenied(login, admin, name);
361 //-------------------------------------------------------------------------
362 template <typename varT>
363 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const string & login,
365 const string & parameter)
367 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
368 admin->GetLogStr().c_str(), login.c_str(), parameter.c_str());
370 //-------------------------------------------------------------------------
371 template <typename varT>
372 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const string & login,
374 const string & parameter,
375 const string & oldValue,
376 const string & newValue,
380 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
381 admin->GetLogStr().c_str(),
388 store->WriteUserChgLog(login, admin->GetLogin(), admin->GetIP(), parameter, oldValue, newValue, msg);
390 //-------------------------------------------------------------------------
391 template <typename varT>
392 void USER_PROPERTY_LOGGED<varT>::OnChange(const string & login,
393 const string & paramName,
394 const string & oldValue,
395 const string & newValue,
400 str1 = scriptsDir + "/OnChange";
402 if (access(str1.c_str(), X_OK) == 0)
404 string str2("\"" + str1 + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin->GetLogin() + "\" \"" + admin->GetIPStr() + "\"");
409 stgLogger("Script OnChange cannot be executed. File %s not found.", str1.c_str());
412 //-------------------------------------------------------------------------
413 //-------------------------------------------------------------------------
414 //-------------------------------------------------------------------------
415 template<typename varT>
416 ostream & operator<< (ostream & stream, const USER_PROPERTY<varT> & value)
418 return stream << value.ConstData();
420 //-----------------------------------------------------------------------------
422 #endif // USER_PROPERTY_H