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();
54 std::string ToString() const;
57 time_t modificationTime;
58 std::set<PROPERTY_NOTIFIER_BASE<varT> *> beforeNotifiers;
59 std::set<PROPERTY_NOTIFIER_BASE<varT> *> afterNotifiers;
60 pthread_mutex_t mutex;
62 //-----------------------------------------------------------------------------
63 template<typename varT>
64 class USER_PROPERTY_LOGGED: public USER_PROPERTY<varT> {
66 USER_PROPERTY_LOGGED(varT & val,
67 const std::string & n,
71 const std::string & sd);
72 virtual ~USER_PROPERTY_LOGGED() {}
74 USER_PROPERTY_LOGGED<varT> * GetPointer() throw() { return this; }
75 const varT & Get() const { return USER_PROPERTY<varT>::ConstData(); }
76 const std::string & GetName() const { return name; }
77 bool Set(const varT & val,
79 const std::string & login,
81 const std::string & msg = "");
83 void WriteAccessDenied(const std::string & login,
85 const std::string & parameter);
87 void WriteSuccessChange(const std::string & login,
89 const std::string & parameter,
90 const std::string & oldValue,
91 const std::string & newValue,
92 const std::string & msg,
95 void OnChange(const std::string & login,
96 const std::string & paramName,
97 const std::string & oldValue,
98 const std::string & newValue,
101 STG_LOGGER & stgLogger;
105 const std::string scriptsDir;
107 //-----------------------------------------------------------------------------
108 class USER_PROPERTIES : private NONCOPYABLE {
110 В этом месте важен порядок следования приватной и открытой частей.
111 Это связано с тем, что часть которая находится в публичной секции
112 по сути является завуалированной ссылкой на закрытую часть. Т.о. нам нужно
113 чтобы конструкторы из закрытой части вызвались раньше открытой. Поэтомому в
114 начале идет закрытая секция
122 USER_PROPERTIES(const std::string & sd);
124 USER_STAT & Stat() { return stat; }
125 USER_CONF & Conf() { return conf; }
126 const USER_STAT & GetStat() const { return stat; }
127 const USER_CONF & GetConf() const { return conf; }
128 void SetStat(const USER_STAT & s) { stat = s; }
129 void SetConf(const USER_CONF & c) { conf = c; }
131 void SetProperties(const USER_PROPERTIES & p) { stat = p.stat; conf = p.conf; }
133 USER_PROPERTY_LOGGED<double> cash;
134 USER_PROPERTY_LOGGED<DIR_TRAFF> up;
135 USER_PROPERTY_LOGGED<DIR_TRAFF> down;
136 USER_PROPERTY_LOGGED<double> lastCashAdd;
137 USER_PROPERTY_LOGGED<time_t> passiveTime;
138 USER_PROPERTY_LOGGED<time_t> lastCashAddTime;
139 USER_PROPERTY_LOGGED<double> freeMb;
140 USER_PROPERTY_LOGGED<time_t> lastActivityTime;
142 USER_PROPERTY_LOGGED<std::string> password;
143 USER_PROPERTY_LOGGED<int> passive;
144 USER_PROPERTY_LOGGED<int> disabled;
145 USER_PROPERTY_LOGGED<int> disabledDetailStat;
146 USER_PROPERTY_LOGGED<int> alwaysOnline;
147 USER_PROPERTY_LOGGED<std::string> tariffName;
148 USER_PROPERTY_LOGGED<std::string> nextTariff;
149 USER_PROPERTY_LOGGED<std::string> address;
150 USER_PROPERTY_LOGGED<std::string> note;
151 USER_PROPERTY_LOGGED<std::string> group;
152 USER_PROPERTY_LOGGED<std::string> email;
153 USER_PROPERTY_LOGGED<std::string> phone;
154 USER_PROPERTY_LOGGED<std::string> realName;
155 USER_PROPERTY_LOGGED<double> credit;
156 USER_PROPERTY_LOGGED<time_t> creditExpire;
157 USER_PROPERTY_LOGGED<USER_IPS> ips;
158 USER_PROPERTY_LOGGED<std::string> userdata0;
159 USER_PROPERTY_LOGGED<std::string> userdata1;
160 USER_PROPERTY_LOGGED<std::string> userdata2;
161 USER_PROPERTY_LOGGED<std::string> userdata3;
162 USER_PROPERTY_LOGGED<std::string> userdata4;
163 USER_PROPERTY_LOGGED<std::string> userdata5;
164 USER_PROPERTY_LOGGED<std::string> userdata6;
165 USER_PROPERTY_LOGGED<std::string> userdata7;
166 USER_PROPERTY_LOGGED<std::string> userdata8;
167 USER_PROPERTY_LOGGED<std::string> userdata9;
169 //=============================================================================
171 //-----------------------------------------------------------------------------
172 //-----------------------------------------------------------------------------
173 //-----------------------------------------------------------------------------
174 template <typename varT>
176 USER_PROPERTY<varT>::USER_PROPERTY(varT & val)
178 modificationTime(stgTime),
183 pthread_mutex_init(&mutex, NULL);
185 //-----------------------------------------------------------------------------
186 template <typename varT>
188 USER_PROPERTY<varT>::~USER_PROPERTY()
190 pthread_mutex_destroy(&mutex);
192 //-----------------------------------------------------------------------------
193 template <typename varT>
195 void USER_PROPERTY<varT>::ModifyTime() throw()
197 modificationTime = stgTime;
199 //-----------------------------------------------------------------------------
200 template <typename varT>
202 void USER_PROPERTY<varT>::Set(const varT & rvalue)
204 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
206 typename std::set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
210 ni = beforeNotifiers.begin();
211 while (ni != beforeNotifiers.end())
212 (*ni++)->Notify(oldVal, rvalue);
215 modificationTime = stgTime;
217 ni = afterNotifiers.begin();
218 while (ni != afterNotifiers.end())
219 (*ni++)->Notify(oldVal, rvalue);
221 //-----------------------------------------------------------------------------
222 template <typename varT>
224 USER_PROPERTY<varT> & USER_PROPERTY<varT>::operator= (const varT & newValue)
229 //-----------------------------------------------------------------------------
230 template <typename varT>
232 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
234 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
235 beforeNotifiers.insert(n);
237 //-----------------------------------------------------------------------------
238 template <typename varT>
240 void USER_PROPERTY<varT>::DelBeforeNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n)
242 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
243 beforeNotifiers.erase(const_cast<PROPERTY_NOTIFIER_BASE<varT> *>(n));
245 //-----------------------------------------------------------------------------
246 template <typename varT>
248 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
250 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
251 afterNotifiers.insert(n);
253 //-----------------------------------------------------------------------------
254 template <typename varT>
256 void USER_PROPERTY<varT>::DelAfterNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n)
258 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
259 afterNotifiers.erase(const_cast<PROPERTY_NOTIFIER_BASE<varT> *>(n));
261 //-----------------------------------------------------------------------------
262 //-----------------------------------------------------------------------------
263 //-----------------------------------------------------------------------------
264 template <typename varT>
266 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(varT & val,
267 const std::string & n,
271 const std::string & sd)
273 : USER_PROPERTY<varT>(val),
281 //-------------------------------------------------------------------------
282 template <typename varT>
283 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
285 const std::string & login,
287 const std::string & msg)
289 const PRIV * priv = admin->GetPriv();
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() | std::ios::fixed);
300 newVal.flags(newVal.flags() | std::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 std::ostream & operator<< (std::ostream & stream, const USER_PROPERTY<varT> & value)
383 return stream << value.ConstData();
385 //-----------------------------------------------------------------------------
386 template<typename varT>
387 std::string USER_PROPERTY<varT>::ToString() const
389 std::stringstream stream;
391 std::string stringValue = stream.str();
394 #endif // USER_PROPERTY_H