3 $Date: 2010/09/13 05:54:43 $
7 #ifndef USER_PROPERTY_H
8 #define USER_PROPERTY_H
10 #include <unistd.h> // access
21 #include "stg_logger.h"
22 #include "stg_locker.h"
23 #include "script_executer.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,
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)
178 pthread_mutex_init(&mutex, NULL);
180 //-----------------------------------------------------------------------------
181 template <typename varT>
183 USER_PROPERTY<varT>::~USER_PROPERTY()
185 pthread_mutex_destroy(&mutex);
187 //-----------------------------------------------------------------------------
188 template <typename varT>
190 void USER_PROPERTY<varT>::ModifyTime() throw()
192 modificationTime = stgTime;
194 //-----------------------------------------------------------------------------
195 template <typename varT>
197 void USER_PROPERTY<varT>::Set(const varT & rvalue)
199 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
201 typename std::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>
219 USER_PROPERTY<varT> & USER_PROPERTY<varT>::operator= (const varT & newValue)
224 //-----------------------------------------------------------------------------
225 template <typename varT>
227 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
229 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
230 beforeNotifiers.insert(n);
232 //-----------------------------------------------------------------------------
233 template <typename varT>
235 void USER_PROPERTY<varT>::DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
237 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
238 beforeNotifiers.erase(n);
240 //-----------------------------------------------------------------------------
241 template <typename varT>
243 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
245 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
246 afterNotifiers.insert(n);
248 //-----------------------------------------------------------------------------
249 template <typename varT>
251 void USER_PROPERTY<varT>::DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
253 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
254 afterNotifiers.erase(n);
256 //-----------------------------------------------------------------------------
257 //-----------------------------------------------------------------------------
258 //-----------------------------------------------------------------------------
259 template <typename varT>
261 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(varT & val,
266 const std::string & sd)
268 : USER_PROPERTY<varT>(val),
276 //-------------------------------------------------------------------------
277 template <typename varT>
278 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
280 const std::string & login,
282 const std::string & msg)
284 const PRIV * priv = admin->GetPriv();
285 std::string adm_login = admin->GetLogin();
286 std::string adm_ip = admin->GetIPStr();
288 if ((priv->userConf && !isStat) ||
289 (priv->userStat && isStat) ||
290 (priv->userPasswd && isPassword) ||
291 (priv->userCash && name == "cash"))
293 std::stringstream oldVal;
294 std::stringstream newVal;
296 oldVal.flags(oldVal.flags() | ios::fixed);
297 newVal.flags(newVal.flags() | ios::fixed);
299 oldVal << USER_PROPERTY<varT>::ConstData();
302 OnChange(login, name, oldVal.str(), newVal.str(), admin);
306 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
310 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
312 USER_PROPERTY<varT>::Set(val);
317 WriteAccessDenied(login, admin, name);
322 //-------------------------------------------------------------------------
323 template <typename varT>
325 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const std::string & login,
327 const std::string & parameter)
329 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
330 admin->GetLogStr().c_str(), login.c_str(), parameter.c_str());
332 //-------------------------------------------------------------------------
333 template <typename varT>
335 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const std::string & login,
337 const std::string & parameter,
338 const std::string & oldValue,
339 const std::string & newValue,
340 const std::string & msg,
343 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
344 admin->GetLogStr().c_str(),
351 store->WriteUserChgLog(login, admin->GetLogin(), admin->GetIP(), parameter, oldValue, newValue, msg);
353 //-------------------------------------------------------------------------
354 template <typename varT>
355 void USER_PROPERTY_LOGGED<varT>::OnChange(const std::string & login,
356 const std::string & paramName,
357 const std::string & oldValue,
358 const std::string & newValue,
361 std::string filePath = scriptsDir + "/OnChange";
363 if (access(filePath.c_str(), X_OK) == 0)
365 std::string execString("\"" + filePath + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin->GetLogin() + "\" \"" + admin->GetIPStr() + "\"");
366 ScriptExec(execString);
370 stgLogger("Script OnChange cannot be executed. File %s not found.", filePath.c_str());
373 //-------------------------------------------------------------------------
374 //-------------------------------------------------------------------------
375 //-------------------------------------------------------------------------
376 template<typename varT>
378 ostream & operator<< (ostream & stream, const USER_PROPERTY<varT> & value)
380 return stream << value.ConstData();
382 //-----------------------------------------------------------------------------
384 #endif // USER_PROPERTY_H