3 $Date: 2010/09/13 05:54:43 $
7 #ifndef USER_PROPERTY_H
8 #define USER_PROPERTY_H
19 #include "stg_logger.h"
20 #include "stg_locker.h"
21 #include "script_executer.h"
23 extern const volatile time_t stgTime;
25 //-----------------------------------------------------------------------------
26 template<typename varT>
29 USER_PROPERTY(varT & val);
30 virtual ~USER_PROPERTY();
32 void Set(const varT & rvalue);
34 USER_PROPERTY<varT> & operator= (const varT & rvalue);
35 USER_PROPERTY<varT> & operator-= (const varT & rvalue);
37 const varT * operator&() const throw();
38 const varT & ConstData() const throw();
40 operator const varT&() const throw()
45 void AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
46 void DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
48 void AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
49 void DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
51 time_t ModificationTime() const throw();
52 void ModifyTime() throw();
56 time_t modificationTime;
57 std::set<PROPERTY_NOTIFIER_BASE<varT> *> beforeNotifiers;
58 std::set<PROPERTY_NOTIFIER_BASE<varT> *> afterNotifiers;
59 pthread_mutex_t mutex;
61 //-----------------------------------------------------------------------------
62 template<typename varT>
63 class USER_PROPERTY_LOGGED: public USER_PROPERTY<varT> {
65 USER_PROPERTY_LOGGED(varT & val,
70 const std::string & sd);
71 virtual ~USER_PROPERTY_LOGGED();
73 USER_PROPERTY_LOGGED<varT> * GetPointer() throw();
74 const varT & Get() const;
75 const std::string & GetName() const;
76 bool Set(const varT & val,
78 const std::string & login,
80 const std::string & msg = "");
82 void WriteAccessDenied(const std::string & login,
84 const std::string & parameter);
86 void WriteSuccessChange(const std::string & login,
88 const std::string & parameter,
89 const std::string & oldValue,
90 const std::string & newValue,
91 const std::string & msg,
94 void OnChange(const std::string & login,
95 const std::string & paramName,
96 const std::string & oldValue,
97 const std::string & newValue,
100 STG_LOGGER & stgLogger;
104 const std::string scriptsDir;
106 //-----------------------------------------------------------------------------
107 class USER_PROPERTIES {
109 В этом месте важен порядок следования приватной и открытой частей.
110 Это связано с тем, что часть которая находится в публичной секции
111 по сути является завуалированной ссылкой на закрытую часть. Т.о. нам нужно
112 чтобы конструкторы из закрытой части вызвались раньше открытой. Поэтомому в
113 начале идет закрытая секция
121 USER_PROPERTIES(const std::string & sd);
123 USER_STAT & Stat() { return stat; }
124 USER_CONF & Conf() { return conf; }
125 const USER_STAT & GetStat() const { return stat; }
126 const USER_CONF & GetConf() const { return conf; }
127 void SetStat(const USER_STAT & s) { stat = s; }
128 void SetConf(const USER_CONF & c) { conf = c; }
130 void SetProperties(const USER_PROPERTIES & p) { stat = p.stat; conf = p.conf; }
132 USER_PROPERTY_LOGGED<double> cash;
133 USER_PROPERTY_LOGGED<DIR_TRAFF> up;
134 USER_PROPERTY_LOGGED<DIR_TRAFF> down;
135 USER_PROPERTY_LOGGED<double> lastCashAdd;
136 USER_PROPERTY_LOGGED<time_t> passiveTime;
137 USER_PROPERTY_LOGGED<time_t> lastCashAddTime;
138 USER_PROPERTY_LOGGED<double> freeMb;
139 USER_PROPERTY_LOGGED<time_t> lastActivityTime;
141 USER_PROPERTY_LOGGED<std::string> password;
142 USER_PROPERTY_LOGGED<int> passive;
143 USER_PROPERTY_LOGGED<int> disabled;
144 USER_PROPERTY_LOGGED<int> disabledDetailStat;
145 USER_PROPERTY_LOGGED<int> alwaysOnline;
146 USER_PROPERTY_LOGGED<std::string> tariffName;
147 USER_PROPERTY_LOGGED<std::string> nextTariff;
148 USER_PROPERTY_LOGGED<std::string> address;
149 USER_PROPERTY_LOGGED<std::string> note;
150 USER_PROPERTY_LOGGED<std::string> group;
151 USER_PROPERTY_LOGGED<std::string> email;
152 USER_PROPERTY_LOGGED<std::string> phone;
153 USER_PROPERTY_LOGGED<std::string> realName;
154 USER_PROPERTY_LOGGED<double> credit;
155 USER_PROPERTY_LOGGED<time_t> creditExpire;
156 USER_PROPERTY_LOGGED<USER_IPS> ips;
157 USER_PROPERTY_LOGGED<std::string> userdata0;
158 USER_PROPERTY_LOGGED<std::string> userdata1;
159 USER_PROPERTY_LOGGED<std::string> userdata2;
160 USER_PROPERTY_LOGGED<std::string> userdata3;
161 USER_PROPERTY_LOGGED<std::string> userdata4;
162 USER_PROPERTY_LOGGED<std::string> userdata5;
163 USER_PROPERTY_LOGGED<std::string> userdata6;
164 USER_PROPERTY_LOGGED<std::string> userdata7;
165 USER_PROPERTY_LOGGED<std::string> userdata8;
166 USER_PROPERTY_LOGGED<std::string> userdata9;
168 //=============================================================================
170 //-----------------------------------------------------------------------------
171 //-----------------------------------------------------------------------------
172 //-----------------------------------------------------------------------------
173 template <typename varT>
174 USER_PROPERTY<varT>::USER_PROPERTY(varT & val)
177 pthread_mutex_init(&mutex, NULL);
178 modificationTime = stgTime;
180 //-----------------------------------------------------------------------------
181 template <typename varT>
182 USER_PROPERTY<varT>::~USER_PROPERTY()
185 //-----------------------------------------------------------------------------
186 template <typename varT>
187 void USER_PROPERTY<varT>::ModifyTime() throw()
189 modificationTime = stgTime;
191 //-----------------------------------------------------------------------------
192 template <typename varT>
193 void USER_PROPERTY<varT>::Set(const varT & rvalue)
195 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
197 typename std::set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
201 ni = beforeNotifiers.begin();
202 while (ni != beforeNotifiers.end())
203 (*ni++)->Notify(oldVal, rvalue);
206 modificationTime = stgTime;
208 ni = afterNotifiers.begin();
209 while (ni != afterNotifiers.end())
210 (*ni++)->Notify(oldVal, rvalue);
212 //-----------------------------------------------------------------------------
213 template <typename varT>
214 USER_PROPERTY<varT> & USER_PROPERTY<varT>::operator= (const varT & newValue)
219 //-----------------------------------------------------------------------------
220 template <typename varT>
221 USER_PROPERTY<varT>& USER_PROPERTY<varT>::operator-= (const varT & delta)
223 varT newValue = ConstData() - delta;
227 //-----------------------------------------------------------------------------
228 template <typename varT>
229 const varT * USER_PROPERTY<varT>::operator&() const throw()
233 //-----------------------------------------------------------------------------
234 template <typename varT>
235 const varT & USER_PROPERTY<varT>::ConstData() const throw()
239 //-----------------------------------------------------------------------------
240 template <typename varT>
241 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
243 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
244 beforeNotifiers.insert(n);
246 //-----------------------------------------------------------------------------
247 template <typename varT>
248 void USER_PROPERTY<varT>::DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
250 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
251 beforeNotifiers.erase(n);
253 //-----------------------------------------------------------------------------
254 template <typename varT>
255 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
257 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
258 afterNotifiers.insert(n);
260 //-----------------------------------------------------------------------------
261 template <typename varT>
262 void USER_PROPERTY<varT>::DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
264 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
265 afterNotifiers.erase(n);
267 //-----------------------------------------------------------------------------
268 template <typename varT>
269 time_t USER_PROPERTY<varT>::ModificationTime() const throw()
271 return modificationTime;
273 //-----------------------------------------------------------------------------
274 //-----------------------------------------------------------------------------
275 //-----------------------------------------------------------------------------
276 template <typename varT>
277 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(varT & val,
282 const std::string & sd)
284 : USER_PROPERTY<varT>(val),
292 //-----------------------------------------------------------------------------
293 template <typename varT>
294 USER_PROPERTY_LOGGED<varT>::~USER_PROPERTY_LOGGED()
297 //-----------------------------------------------------------------------------
298 template <typename varT>
299 USER_PROPERTY_LOGGED<varT> * USER_PROPERTY_LOGGED<varT>::GetPointer() throw()
303 //-----------------------------------------------------------------------------
304 template <typename varT>
305 const varT & USER_PROPERTY_LOGGED<varT>::Get() const
307 return USER_PROPERTY<varT>::ConstData();
309 //-------------------------------------------------------------------------
310 template <typename varT>
311 const std::string & USER_PROPERTY_LOGGED<varT>::GetName() const
315 //-------------------------------------------------------------------------
316 template <typename varT>
317 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
319 const std::string & login,
321 const std::string & msg)
323 const PRIV * priv = admin->GetPriv();
324 std::string adm_login = admin->GetLogin();
325 std::string adm_ip = admin->GetIPStr();
327 if ((priv->userConf && !isStat) ||
328 (priv->userStat && isStat) ||
329 (priv->userPasswd && isPassword) ||
330 (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 std::string & login,
365 const std::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 std::string & login,
374 const std::string & parameter,
375 const std::string & oldValue,
376 const std::string & newValue,
377 const std::string & msg,
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 std::string & login,
393 const std::string & paramName,
394 const std::string & oldValue,
395 const std::string & newValue,
398 std::string filePath = scriptsDir + "/OnChange";
400 if (access(filePath.c_str(), X_OK) == 0)
402 std::string execString("\"" + filePath + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin->GetLogin() + "\" \"" + admin->GetIPStr() + "\"");
403 ScriptExec(execString);
407 stgLogger("Script OnChange cannot be executed. File %s not found.", filePath.c_str());
410 //-------------------------------------------------------------------------
411 //-------------------------------------------------------------------------
412 //-------------------------------------------------------------------------
413 template<typename varT>
414 ostream & operator<< (ostream & stream, const USER_PROPERTY<varT> & value)
416 return stream << value.ConstData();
418 //-----------------------------------------------------------------------------
420 #endif // USER_PROPERTY_H