3 $Date: 2010/09/13 05:54:43 $
7 #ifndef USER_PROPERTY_H
8 #define USER_PROPERTY_H
10 #include <unistd.h> // access
18 #include "stg/logger.h"
19 #include "stg/locker.h"
20 #include "stg/scriptexecuter.h"
25 #include "noncopyable.h"
27 extern const volatile time_t stgTime;
29 //-----------------------------------------------------------------------------
30 template<typename varT>
33 USER_PROPERTY(varT & val);
34 virtual ~USER_PROPERTY();
36 void Set(const varT & rvalue);
38 USER_PROPERTY<varT> & operator= (const varT & rvalue);
40 const varT * operator&() const throw() { return &value; }
41 const varT & ConstData() const throw() { return value; }
43 operator const varT&() const throw() { return value; }
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() { return modificationTime; }
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,
66 const std::string & n,
70 const std::string & sd);
71 virtual ~USER_PROPERTY_LOGGED() {}
73 USER_PROPERTY_LOGGED<varT> * GetPointer() throw() { return this; }
74 const varT & Get() const { return USER_PROPERTY<varT>::ConstData(); }
75 const std::string & GetName() const { return name; }
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 : private NONCOPYABLE {
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>
175 USER_PROPERTY<varT>::USER_PROPERTY(varT & val)
177 modificationTime(stgTime),
182 pthread_mutex_init(&mutex, NULL);
184 //-----------------------------------------------------------------------------
185 template <typename varT>
187 USER_PROPERTY<varT>::~USER_PROPERTY()
189 pthread_mutex_destroy(&mutex);
191 //-----------------------------------------------------------------------------
192 template <typename varT>
194 void USER_PROPERTY<varT>::ModifyTime() throw()
196 modificationTime = stgTime;
198 //-----------------------------------------------------------------------------
199 template <typename varT>
201 void USER_PROPERTY<varT>::Set(const varT & rvalue)
203 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
205 typename std::set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
209 ni = beforeNotifiers.begin();
210 while (ni != beforeNotifiers.end())
211 (*ni++)->Notify(oldVal, rvalue);
214 modificationTime = stgTime;
216 ni = afterNotifiers.begin();
217 while (ni != afterNotifiers.end())
218 (*ni++)->Notify(oldVal, rvalue);
220 //-----------------------------------------------------------------------------
221 template <typename varT>
223 USER_PROPERTY<varT> & USER_PROPERTY<varT>::operator= (const varT & newValue)
228 //-----------------------------------------------------------------------------
229 template <typename varT>
231 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
233 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
234 beforeNotifiers.insert(n);
236 //-----------------------------------------------------------------------------
237 template <typename varT>
239 void USER_PROPERTY<varT>::DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
241 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
242 beforeNotifiers.erase(n);
244 //-----------------------------------------------------------------------------
245 template <typename varT>
247 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
249 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
250 afterNotifiers.insert(n);
252 //-----------------------------------------------------------------------------
253 template <typename varT>
255 void USER_PROPERTY<varT>::DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
257 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
258 afterNotifiers.erase(n);
260 //-----------------------------------------------------------------------------
261 //-----------------------------------------------------------------------------
262 //-----------------------------------------------------------------------------
263 template <typename varT>
265 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(varT & val,
266 const std::string & n,
270 const std::string & sd)
272 : USER_PROPERTY<varT>(val),
280 //-------------------------------------------------------------------------
281 template <typename varT>
282 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
284 const std::string & login,
286 const std::string & msg)
288 const PRIV * priv = admin->GetPriv();
289 std::string adm_login = admin->GetLogin();
290 std::string adm_ip = admin->GetIPStr();
292 if ((priv->userConf && !isStat) ||
293 (priv->userStat && isStat) ||
294 (priv->userPasswd && isPassword) ||
295 (priv->userCash && name == "cash"))
297 std::stringstream oldVal;
298 std::stringstream newVal;
300 oldVal.flags(oldVal.flags() | ios::fixed);
301 newVal.flags(newVal.flags() | ios::fixed);
303 oldVal << USER_PROPERTY<varT>::ConstData();
306 OnChange(login, name, oldVal.str(), newVal.str(), admin);
310 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
314 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
316 USER_PROPERTY<varT>::Set(val);
321 WriteAccessDenied(login, admin, name);
326 //-------------------------------------------------------------------------
327 template <typename varT>
329 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const std::string & login,
331 const std::string & parameter)
333 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
334 admin->GetLogStr().c_str(), login.c_str(), parameter.c_str());
336 //-------------------------------------------------------------------------
337 template <typename varT>
339 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const std::string & login,
341 const std::string & parameter,
342 const std::string & oldValue,
343 const std::string & newValue,
344 const std::string & msg,
347 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
348 admin->GetLogStr().c_str(),
355 store->WriteUserChgLog(login, admin->GetLogin(), admin->GetIP(), parameter, oldValue, newValue, msg);
357 //-------------------------------------------------------------------------
358 template <typename varT>
359 void USER_PROPERTY_LOGGED<varT>::OnChange(const std::string & login,
360 const std::string & paramName,
361 const std::string & oldValue,
362 const std::string & newValue,
365 std::string filePath = scriptsDir + "/OnChange";
367 if (access(filePath.c_str(), X_OK) == 0)
369 std::string execString("\"" + filePath + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin->GetLogin() + "\" \"" + admin->GetIPStr() + "\"");
370 ScriptExec(execString.c_str());
374 stgLogger("Script OnChange cannot be executed. File %s not found.", filePath.c_str());
377 //-------------------------------------------------------------------------
378 //-------------------------------------------------------------------------
379 //-------------------------------------------------------------------------
380 template<typename varT>
382 ostream & operator<< (ostream & stream, const USER_PROPERTY<varT> & value)
384 return stream << value.ConstData();
386 //-----------------------------------------------------------------------------
388 #endif // USER_PROPERTY_H