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 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(const PROPERTY_NOTIFIER_BASE<varT> * n);
48 void AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
49 void DelAfterNotifier(const 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(const PROPERTY_NOTIFIER_BASE<varT> * n)
241 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
242 beforeNotifiers.erase(const_cast<PROPERTY_NOTIFIER_BASE<varT> *>(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(const PROPERTY_NOTIFIER_BASE<varT> * n)
257 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
258 afterNotifiers.erase(const_cast<PROPERTY_NOTIFIER_BASE<varT> *>(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();
290 if ((priv->userConf && !isStat) ||
291 (priv->userStat && isStat) ||
292 (priv->userPasswd && isPassword) ||
293 (priv->userCash && name == "cash"))
295 std::stringstream oldVal;
296 std::stringstream newVal;
298 oldVal.flags(oldVal.flags() | std::ios::fixed);
299 newVal.flags(newVal.flags() | std::ios::fixed);
301 oldVal << USER_PROPERTY<varT>::ConstData();
304 OnChange(login, name, oldVal.str(), newVal.str(), admin);
308 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
312 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
314 USER_PROPERTY<varT>::Set(val);
319 WriteAccessDenied(login, admin, name);
324 //-------------------------------------------------------------------------
325 template <typename varT>
327 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const std::string & login,
329 const std::string & parameter)
331 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
332 admin->GetLogStr().c_str(), login.c_str(), parameter.c_str());
334 //-------------------------------------------------------------------------
335 template <typename varT>
337 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const std::string & login,
339 const std::string & parameter,
340 const std::string & oldValue,
341 const std::string & newValue,
342 const std::string & msg,
345 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
346 admin->GetLogStr().c_str(),
353 store->WriteUserChgLog(login, admin->GetLogin(), admin->GetIP(), parameter, oldValue, newValue, msg);
355 //-------------------------------------------------------------------------
356 template <typename varT>
357 void USER_PROPERTY_LOGGED<varT>::OnChange(const std::string & login,
358 const std::string & paramName,
359 const std::string & oldValue,
360 const std::string & newValue,
363 std::string filePath = scriptsDir + "/OnChange";
365 if (access(filePath.c_str(), X_OK) == 0)
367 std::string execString("\"" + filePath + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin->GetLogin() + "\" \"" + admin->GetIPStr() + "\"");
368 ScriptExec(execString.c_str());
372 stgLogger("Script OnChange cannot be executed. File %s not found.", filePath.c_str());
375 //-------------------------------------------------------------------------
376 //-------------------------------------------------------------------------
377 //-------------------------------------------------------------------------
378 template<typename varT>
380 std::ostream & operator<< (std::ostream & stream, const USER_PROPERTY<varT> & value)
382 return stream << value.ConstData();
384 //-----------------------------------------------------------------------------
386 #endif // USER_PROPERTY_H