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"
22 #include "noncopyable.h"
24 extern const volatile time_t stgTime;
26 //-----------------------------------------------------------------------------
27 template<typename varT>
30 USER_PROPERTY(varT & val);
31 virtual ~USER_PROPERTY();
33 void Set(const varT & rvalue);
35 USER_PROPERTY<varT> & operator= (const varT & rvalue);
37 const varT * operator&() const throw() { return &value; }
38 const varT & ConstData() const throw() { return value; }
40 operator const varT&() const throw() { return value; }
42 void AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
43 void DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
45 void AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
46 void DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
48 time_t ModificationTime() const throw() { return modificationTime; }
49 void ModifyTime() throw();
53 time_t modificationTime;
54 std::set<PROPERTY_NOTIFIER_BASE<varT> *> beforeNotifiers;
55 std::set<PROPERTY_NOTIFIER_BASE<varT> *> afterNotifiers;
56 pthread_mutex_t mutex;
58 //-----------------------------------------------------------------------------
59 template<typename varT>
60 class USER_PROPERTY_LOGGED: public USER_PROPERTY<varT> {
62 USER_PROPERTY_LOGGED(varT & val,
67 const std::string & sd);
68 virtual ~USER_PROPERTY_LOGGED() {}
70 USER_PROPERTY_LOGGED<varT> * GetPointer() throw() { return this; }
71 const varT & Get() const { return USER_PROPERTY<varT>::ConstData(); }
72 const std::string & GetName() const { return name; }
73 bool Set(const varT & val,
75 const std::string & login,
77 const std::string & msg = "");
79 void WriteAccessDenied(const std::string & login,
81 const std::string & parameter);
83 void WriteSuccessChange(const std::string & login,
85 const std::string & parameter,
86 const std::string & oldValue,
87 const std::string & newValue,
88 const std::string & msg,
91 void OnChange(const std::string & login,
92 const std::string & paramName,
93 const std::string & oldValue,
94 const std::string & newValue,
97 STG_LOGGER & stgLogger;
101 const std::string scriptsDir;
103 //-----------------------------------------------------------------------------
104 class USER_PROPERTIES : private NONCOPYABLE {
106 В этом месте важен порядок следования приватной и открытой частей.
107 Это связано с тем, что часть которая находится в публичной секции
108 по сути является завуалированной ссылкой на закрытую часть. Т.о. нам нужно
109 чтобы конструкторы из закрытой части вызвались раньше открытой. Поэтомому в
110 начале идет закрытая секция
118 USER_PROPERTIES(const std::string & sd);
120 USER_STAT & Stat() { return stat; }
121 USER_CONF & Conf() { return conf; }
122 const USER_STAT & GetStat() const { return stat; }
123 const USER_CONF & GetConf() const { return conf; }
124 void SetStat(const USER_STAT & s) { stat = s; }
125 void SetConf(const USER_CONF & c) { conf = c; }
127 void SetProperties(const USER_PROPERTIES & p) { stat = p.stat; conf = p.conf; }
129 USER_PROPERTY_LOGGED<double> cash;
130 USER_PROPERTY_LOGGED<DIR_TRAFF> up;
131 USER_PROPERTY_LOGGED<DIR_TRAFF> down;
132 USER_PROPERTY_LOGGED<double> lastCashAdd;
133 USER_PROPERTY_LOGGED<time_t> passiveTime;
134 USER_PROPERTY_LOGGED<time_t> lastCashAddTime;
135 USER_PROPERTY_LOGGED<double> freeMb;
136 USER_PROPERTY_LOGGED<time_t> lastActivityTime;
138 USER_PROPERTY_LOGGED<std::string> password;
139 USER_PROPERTY_LOGGED<int> passive;
140 USER_PROPERTY_LOGGED<int> disabled;
141 USER_PROPERTY_LOGGED<int> disabledDetailStat;
142 USER_PROPERTY_LOGGED<int> alwaysOnline;
143 USER_PROPERTY_LOGGED<std::string> tariffName;
144 USER_PROPERTY_LOGGED<std::string> nextTariff;
145 USER_PROPERTY_LOGGED<std::string> address;
146 USER_PROPERTY_LOGGED<std::string> note;
147 USER_PROPERTY_LOGGED<std::string> group;
148 USER_PROPERTY_LOGGED<std::string> email;
149 USER_PROPERTY_LOGGED<std::string> phone;
150 USER_PROPERTY_LOGGED<std::string> realName;
151 USER_PROPERTY_LOGGED<double> credit;
152 USER_PROPERTY_LOGGED<time_t> creditExpire;
153 USER_PROPERTY_LOGGED<USER_IPS> ips;
154 USER_PROPERTY_LOGGED<std::string> userdata0;
155 USER_PROPERTY_LOGGED<std::string> userdata1;
156 USER_PROPERTY_LOGGED<std::string> userdata2;
157 USER_PROPERTY_LOGGED<std::string> userdata3;
158 USER_PROPERTY_LOGGED<std::string> userdata4;
159 USER_PROPERTY_LOGGED<std::string> userdata5;
160 USER_PROPERTY_LOGGED<std::string> userdata6;
161 USER_PROPERTY_LOGGED<std::string> userdata7;
162 USER_PROPERTY_LOGGED<std::string> userdata8;
163 USER_PROPERTY_LOGGED<std::string> userdata9;
165 //=============================================================================
167 //-----------------------------------------------------------------------------
168 //-----------------------------------------------------------------------------
169 //-----------------------------------------------------------------------------
170 template <typename varT>
172 USER_PROPERTY<varT>::USER_PROPERTY(varT & val)
174 modificationTime(stgTime)
176 pthread_mutex_init(&mutex, NULL);
178 //-----------------------------------------------------------------------------
179 template <typename varT>
181 USER_PROPERTY<varT>::~USER_PROPERTY()
183 pthread_mutex_destroy(&mutex);
185 //-----------------------------------------------------------------------------
186 template <typename varT>
188 void USER_PROPERTY<varT>::ModifyTime() throw()
190 modificationTime = stgTime;
192 //-----------------------------------------------------------------------------
193 template <typename varT>
195 void USER_PROPERTY<varT>::Set(const varT & rvalue)
197 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
199 typename std::set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
203 ni = beforeNotifiers.begin();
204 while (ni != beforeNotifiers.end())
205 (*ni++)->Notify(oldVal, rvalue);
208 modificationTime = stgTime;
210 ni = afterNotifiers.begin();
211 while (ni != afterNotifiers.end())
212 (*ni++)->Notify(oldVal, rvalue);
214 //-----------------------------------------------------------------------------
215 template <typename varT>
217 USER_PROPERTY<varT> & USER_PROPERTY<varT>::operator= (const varT & newValue)
222 //-----------------------------------------------------------------------------
223 template <typename varT>
225 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
227 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
228 beforeNotifiers.insert(n);
230 //-----------------------------------------------------------------------------
231 template <typename varT>
233 void USER_PROPERTY<varT>::DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
235 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
236 beforeNotifiers.erase(n);
238 //-----------------------------------------------------------------------------
239 template <typename varT>
241 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
243 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
244 afterNotifiers.insert(n);
246 //-----------------------------------------------------------------------------
247 template <typename varT>
249 void USER_PROPERTY<varT>::DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
251 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
252 afterNotifiers.erase(n);
254 //-----------------------------------------------------------------------------
255 //-----------------------------------------------------------------------------
256 //-----------------------------------------------------------------------------
257 template <typename varT>
259 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(varT & val,
264 const std::string & sd)
266 : USER_PROPERTY<varT>(val),
274 //-------------------------------------------------------------------------
275 template <typename varT>
276 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
278 const std::string & login,
280 const std::string & msg)
282 const PRIV * priv = admin->GetPriv();
283 std::string adm_login = admin->GetLogin();
284 std::string adm_ip = admin->GetIPStr();
286 if ((priv->userConf && !isStat) ||
287 (priv->userStat && isStat) ||
288 (priv->userPasswd && isPassword) ||
289 (priv->userCash && name == "cash"))
291 std::stringstream oldVal;
292 std::stringstream newVal;
294 oldVal.flags(oldVal.flags() | ios::fixed);
295 newVal.flags(newVal.flags() | ios::fixed);
297 oldVal << USER_PROPERTY<varT>::ConstData();
300 OnChange(login, name, oldVal.str(), newVal.str(), admin);
304 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
308 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
310 USER_PROPERTY<varT>::Set(val);
315 WriteAccessDenied(login, admin, name);
320 //-------------------------------------------------------------------------
321 template <typename varT>
323 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const std::string & login,
325 const std::string & parameter)
327 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
328 admin->GetLogStr().c_str(), login.c_str(), parameter.c_str());
330 //-------------------------------------------------------------------------
331 template <typename varT>
333 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const std::string & login,
335 const std::string & parameter,
336 const std::string & oldValue,
337 const std::string & newValue,
338 const std::string & msg,
341 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
342 admin->GetLogStr().c_str(),
349 store->WriteUserChgLog(login, admin->GetLogin(), admin->GetIP(), parameter, oldValue, newValue, msg);
351 //-------------------------------------------------------------------------
352 template <typename varT>
353 void USER_PROPERTY_LOGGED<varT>::OnChange(const std::string & login,
354 const std::string & paramName,
355 const std::string & oldValue,
356 const std::string & newValue,
359 std::string filePath = scriptsDir + "/OnChange";
361 if (access(filePath.c_str(), X_OK) == 0)
363 std::string execString("\"" + filePath + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin->GetLogin() + "\" \"" + admin->GetIPStr() + "\"");
364 ScriptExec(execString);
368 stgLogger("Script OnChange cannot be executed. File %s not found.", filePath.c_str());
371 //-------------------------------------------------------------------------
372 //-------------------------------------------------------------------------
373 //-------------------------------------------------------------------------
374 template<typename varT>
376 ostream & operator<< (ostream & stream, const USER_PROPERTY<varT> & value)
378 return stream << value.ConstData();
380 //-----------------------------------------------------------------------------
382 #endif // USER_PROPERTY_H