3 $Date: 2010/09/13 05:54:43 $
7 #ifndef USER_PROPERTY_H
8 #define USER_PROPERTY_H
10 #include <unistd.h> // access
23 #include "scriptexecuter.h"
24 #include "noncopyable.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);
39 const varT * operator&() const throw() { return &value; }
40 const varT & ConstData() const throw() { return value; }
42 operator const varT&() const throw() { return value; }
44 void AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
45 void DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
47 void AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
48 void DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
50 time_t ModificationTime() const throw() { return modificationTime; }
51 void ModifyTime() throw();
55 time_t modificationTime;
56 std::set<PROPERTY_NOTIFIER_BASE<varT> *> beforeNotifiers;
57 std::set<PROPERTY_NOTIFIER_BASE<varT> *> afterNotifiers;
58 pthread_mutex_t mutex;
60 //-----------------------------------------------------------------------------
61 template<typename varT>
62 class USER_PROPERTY_LOGGED: public USER_PROPERTY<varT> {
64 USER_PROPERTY_LOGGED(varT & val,
65 const std::string & n,
69 const std::string & sd);
70 virtual ~USER_PROPERTY_LOGGED() {}
72 USER_PROPERTY_LOGGED<varT> * GetPointer() throw() { return this; }
73 const varT & Get() const { return USER_PROPERTY<varT>::ConstData(); }
74 const std::string & GetName() const { return name; }
75 bool Set(const varT & val,
77 const std::string & login,
79 const std::string & msg = "");
81 void WriteAccessDenied(const std::string & login,
83 const std::string & parameter);
85 void WriteSuccessChange(const std::string & login,
87 const std::string & parameter,
88 const std::string & oldValue,
89 const std::string & newValue,
90 const std::string & msg,
93 void OnChange(const std::string & login,
94 const std::string & paramName,
95 const std::string & oldValue,
96 const std::string & newValue,
99 STG_LOGGER & stgLogger;
103 const std::string scriptsDir;
105 //-----------------------------------------------------------------------------
106 class USER_PROPERTIES : private NONCOPYABLE {
108 В этом месте важен порядок следования приватной и открытой частей.
109 Это связано с тем, что часть которая находится в публичной секции
110 по сути является завуалированной ссылкой на закрытую часть. Т.о. нам нужно
111 чтобы конструкторы из закрытой части вызвались раньше открытой. Поэтомому в
112 начале идет закрытая секция
120 USER_PROPERTIES(const std::string & sd);
122 USER_STAT & Stat() { return stat; }
123 USER_CONF & Conf() { return conf; }
124 const USER_STAT & GetStat() const { return stat; }
125 const USER_CONF & GetConf() const { return conf; }
126 void SetStat(const USER_STAT & s) { stat = s; }
127 void SetConf(const USER_CONF & c) { conf = c; }
129 void SetProperties(const USER_PROPERTIES & p) { stat = p.stat; conf = p.conf; }
131 USER_PROPERTY_LOGGED<double> cash;
132 USER_PROPERTY_LOGGED<DIR_TRAFF> up;
133 USER_PROPERTY_LOGGED<DIR_TRAFF> down;
134 USER_PROPERTY_LOGGED<double> lastCashAdd;
135 USER_PROPERTY_LOGGED<time_t> passiveTime;
136 USER_PROPERTY_LOGGED<time_t> lastCashAddTime;
137 USER_PROPERTY_LOGGED<double> freeMb;
138 USER_PROPERTY_LOGGED<time_t> lastActivityTime;
140 USER_PROPERTY_LOGGED<std::string> password;
141 USER_PROPERTY_LOGGED<int> passive;
142 USER_PROPERTY_LOGGED<int> disabled;
143 USER_PROPERTY_LOGGED<int> disabledDetailStat;
144 USER_PROPERTY_LOGGED<int> alwaysOnline;
145 USER_PROPERTY_LOGGED<std::string> tariffName;
146 USER_PROPERTY_LOGGED<std::string> nextTariff;
147 USER_PROPERTY_LOGGED<std::string> address;
148 USER_PROPERTY_LOGGED<std::string> note;
149 USER_PROPERTY_LOGGED<std::string> group;
150 USER_PROPERTY_LOGGED<std::string> email;
151 USER_PROPERTY_LOGGED<std::string> phone;
152 USER_PROPERTY_LOGGED<std::string> realName;
153 USER_PROPERTY_LOGGED<double> credit;
154 USER_PROPERTY_LOGGED<time_t> creditExpire;
155 USER_PROPERTY_LOGGED<USER_IPS> ips;
156 USER_PROPERTY_LOGGED<std::string> userdata0;
157 USER_PROPERTY_LOGGED<std::string> userdata1;
158 USER_PROPERTY_LOGGED<std::string> userdata2;
159 USER_PROPERTY_LOGGED<std::string> userdata3;
160 USER_PROPERTY_LOGGED<std::string> userdata4;
161 USER_PROPERTY_LOGGED<std::string> userdata5;
162 USER_PROPERTY_LOGGED<std::string> userdata6;
163 USER_PROPERTY_LOGGED<std::string> userdata7;
164 USER_PROPERTY_LOGGED<std::string> userdata8;
165 USER_PROPERTY_LOGGED<std::string> userdata9;
167 //=============================================================================
169 //-----------------------------------------------------------------------------
170 //-----------------------------------------------------------------------------
171 //-----------------------------------------------------------------------------
172 template <typename varT>
174 USER_PROPERTY<varT>::USER_PROPERTY(varT & val)
176 modificationTime(stgTime),
181 pthread_mutex_init(&mutex, NULL);
183 //-----------------------------------------------------------------------------
184 template <typename varT>
186 USER_PROPERTY<varT>::~USER_PROPERTY()
188 pthread_mutex_destroy(&mutex);
190 //-----------------------------------------------------------------------------
191 template <typename varT>
193 void USER_PROPERTY<varT>::ModifyTime() throw()
195 modificationTime = stgTime;
197 //-----------------------------------------------------------------------------
198 template <typename varT>
200 void USER_PROPERTY<varT>::Set(const varT & rvalue)
202 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
204 typename std::set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
208 ni = beforeNotifiers.begin();
209 while (ni != beforeNotifiers.end())
210 (*ni++)->Notify(oldVal, rvalue);
213 modificationTime = stgTime;
215 ni = afterNotifiers.begin();
216 while (ni != afterNotifiers.end())
217 (*ni++)->Notify(oldVal, rvalue);
219 //-----------------------------------------------------------------------------
220 template <typename varT>
222 USER_PROPERTY<varT> & USER_PROPERTY<varT>::operator= (const varT & newValue)
227 //-----------------------------------------------------------------------------
228 template <typename varT>
230 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
232 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
233 beforeNotifiers.insert(n);
235 //-----------------------------------------------------------------------------
236 template <typename varT>
238 void USER_PROPERTY<varT>::DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
240 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
241 beforeNotifiers.erase(n);
243 //-----------------------------------------------------------------------------
244 template <typename varT>
246 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
248 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
249 afterNotifiers.insert(n);
251 //-----------------------------------------------------------------------------
252 template <typename varT>
254 void USER_PROPERTY<varT>::DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
256 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
257 afterNotifiers.erase(n);
259 //-----------------------------------------------------------------------------
260 //-----------------------------------------------------------------------------
261 //-----------------------------------------------------------------------------
262 template <typename varT>
264 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(varT & val,
265 const std::string & n,
269 const std::string & sd)
271 : USER_PROPERTY<varT>(val),
279 //-------------------------------------------------------------------------
280 template <typename varT>
281 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
283 const std::string & login,
285 const std::string & msg)
287 const PRIV * priv = admin->GetPriv();
288 std::string adm_login = admin->GetLogin();
289 std::string adm_ip = admin->GetIPStr();
291 if ((priv->userConf && !isStat) ||
292 (priv->userStat && isStat) ||
293 (priv->userPasswd && isPassword) ||
294 (priv->userCash && name == "cash"))
296 std::stringstream oldVal;
297 std::stringstream newVal;
299 oldVal.flags(oldVal.flags() | ios::fixed);
300 newVal.flags(newVal.flags() | ios::fixed);
302 oldVal << USER_PROPERTY<varT>::ConstData();
305 OnChange(login, name, oldVal.str(), newVal.str(), admin);
309 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
313 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
315 USER_PROPERTY<varT>::Set(val);
320 WriteAccessDenied(login, admin, name);
325 //-------------------------------------------------------------------------
326 template <typename varT>
328 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const std::string & login,
330 const std::string & parameter)
332 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
333 admin->GetLogStr().c_str(), login.c_str(), parameter.c_str());
335 //-------------------------------------------------------------------------
336 template <typename varT>
338 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const std::string & login,
340 const std::string & parameter,
341 const std::string & oldValue,
342 const std::string & newValue,
343 const std::string & msg,
346 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
347 admin->GetLogStr().c_str(),
354 store->WriteUserChgLog(login, admin->GetLogin(), admin->GetIP(), parameter, oldValue, newValue, msg);
356 //-------------------------------------------------------------------------
357 template <typename varT>
358 void USER_PROPERTY_LOGGED<varT>::OnChange(const std::string & login,
359 const std::string & paramName,
360 const std::string & oldValue,
361 const std::string & newValue,
364 std::string filePath = scriptsDir + "/OnChange";
366 if (access(filePath.c_str(), X_OK) == 0)
368 std::string execString("\"" + filePath + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin->GetLogin() + "\" \"" + admin->GetIPStr() + "\"");
369 ScriptExec(execString.c_str());
373 stgLogger("Script OnChange cannot be executed. File %s not found.", filePath.c_str());
376 //-------------------------------------------------------------------------
377 //-------------------------------------------------------------------------
378 //-------------------------------------------------------------------------
379 template<typename varT>
381 ostream & operator<< (ostream & stream, const USER_PROPERTY<varT> & value)
383 return stream << value.ConstData();
385 //-----------------------------------------------------------------------------
387 #endif // USER_PROPERTY_H