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;
28 //-----------------------------------------------------------------------------
29 class USER_PROPERTY_BASE {
31 virtual std::string ToString() const = 0;
33 //-----------------------------------------------------------------------------
34 template<typename varT>
35 class USER_PROPERTY : USER_PROPERTY_BASE {
37 USER_PROPERTY(varT & val);
38 virtual ~USER_PROPERTY();
40 void Set(const varT & rvalue);
42 USER_PROPERTY<varT> & operator= (const varT & rvalue);
44 const varT * operator&() const throw() { return &value; }
45 const varT & ConstData() const throw() { return value; }
47 operator const varT&() const throw() { return value; }
49 void AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
50 void DelBeforeNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n);
52 void AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
53 void DelAfterNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n);
55 time_t ModificationTime() const throw() { return modificationTime; }
56 void ModifyTime() throw();
58 std::string ToString() const;
61 time_t modificationTime;
62 std::set<PROPERTY_NOTIFIER_BASE<varT> *> beforeNotifiers;
63 std::set<PROPERTY_NOTIFIER_BASE<varT> *> afterNotifiers;
64 pthread_mutex_t mutex;
66 //-----------------------------------------------------------------------------
67 template<typename varT>
68 class USER_PROPERTY_LOGGED: public USER_PROPERTY<varT> {
70 USER_PROPERTY_LOGGED(varT & val,
71 const std::string & n,
75 const std::string & sd);
76 virtual ~USER_PROPERTY_LOGGED() {}
78 USER_PROPERTY_LOGGED<varT> * GetPointer() throw() { return this; }
79 const varT & Get() const { return USER_PROPERTY<varT>::ConstData(); }
80 const std::string & GetName() const { return name; }
81 bool Set(const varT & val,
83 const std::string & login,
85 const std::string & msg = "");
87 void WriteAccessDenied(const std::string & login,
89 const std::string & parameter);
91 void WriteSuccessChange(const std::string & login,
93 const std::string & parameter,
94 const std::string & oldValue,
95 const std::string & newValue,
96 const std::string & msg,
99 void OnChange(const std::string & login,
100 const std::string & paramName,
101 const std::string & oldValue,
102 const std::string & newValue,
103 const ADMIN * admin);
105 STG_LOGGER & stgLogger;
109 const std::string scriptsDir;
111 //-----------------------------------------------------------------------------
112 class USER_PROPERTIES : private NONCOPYABLE {
114 В этом месте важен порядок следования приватной и открытой частей.
115 Это связано с тем, что часть которая находится в публичной секции
116 по сути является завуалированной ссылкой на закрытую часть. Т.о. нам нужно
117 чтобы конструкторы из закрытой части вызвались раньше открытой. Поэтомому в
118 начале идет закрытая секция
126 USER_PROPERTIES(const std::string & sd);
128 USER_STAT & Stat() { return stat; }
129 USER_CONF & Conf() { return conf; }
130 const USER_STAT & GetStat() const { return stat; }
131 const USER_CONF & GetConf() const { return conf; }
132 void SetStat(const USER_STAT & s) { stat = s; }
133 void SetConf(const USER_CONF & c) { conf = c; }
135 void SetProperties(const USER_PROPERTIES & p) { stat = p.stat; conf = p.conf; }
137 std::string GetPropertyValue(const std::string & name) const;
139 USER_PROPERTY_LOGGED<double> cash;
140 USER_PROPERTY_LOGGED<DIR_TRAFF> up;
141 USER_PROPERTY_LOGGED<DIR_TRAFF> down;
142 USER_PROPERTY_LOGGED<double> lastCashAdd;
143 USER_PROPERTY_LOGGED<time_t> passiveTime;
144 USER_PROPERTY_LOGGED<time_t> lastCashAddTime;
145 USER_PROPERTY_LOGGED<double> freeMb;
146 USER_PROPERTY_LOGGED<time_t> lastActivityTime;
148 USER_PROPERTY_LOGGED<std::string> password;
149 USER_PROPERTY_LOGGED<int> passive;
150 USER_PROPERTY_LOGGED<int> disabled;
151 USER_PROPERTY_LOGGED<int> disabledDetailStat;
152 USER_PROPERTY_LOGGED<int> alwaysOnline;
153 USER_PROPERTY_LOGGED<std::string> tariffName;
154 USER_PROPERTY_LOGGED<std::string> nextTariff;
155 USER_PROPERTY_LOGGED<std::string> address;
156 USER_PROPERTY_LOGGED<std::string> note;
157 USER_PROPERTY_LOGGED<std::string> group;
158 USER_PROPERTY_LOGGED<std::string> email;
159 USER_PROPERTY_LOGGED<std::string> phone;
160 USER_PROPERTY_LOGGED<std::string> realName;
161 USER_PROPERTY_LOGGED<double> credit;
162 USER_PROPERTY_LOGGED<time_t> creditExpire;
163 USER_PROPERTY_LOGGED<USER_IPS> ips;
164 USER_PROPERTY_LOGGED<std::string> userdata0;
165 USER_PROPERTY_LOGGED<std::string> userdata1;
166 USER_PROPERTY_LOGGED<std::string> userdata2;
167 USER_PROPERTY_LOGGED<std::string> userdata3;
168 USER_PROPERTY_LOGGED<std::string> userdata4;
169 USER_PROPERTY_LOGGED<std::string> userdata5;
170 USER_PROPERTY_LOGGED<std::string> userdata6;
171 USER_PROPERTY_LOGGED<std::string> userdata7;
172 USER_PROPERTY_LOGGED<std::string> userdata8;
173 USER_PROPERTY_LOGGED<std::string> userdata9;
175 std::map<std::string, USER_PROPERTY_BASE*> params;
177 //=============================================================================
179 //-----------------------------------------------------------------------------
180 //-----------------------------------------------------------------------------
181 //-----------------------------------------------------------------------------
182 template <typename varT>
184 USER_PROPERTY<varT>::USER_PROPERTY(varT & val)
186 modificationTime(stgTime),
191 pthread_mutex_init(&mutex, NULL);
193 //-----------------------------------------------------------------------------
194 template <typename varT>
196 USER_PROPERTY<varT>::~USER_PROPERTY()
198 pthread_mutex_destroy(&mutex);
200 //-----------------------------------------------------------------------------
201 template <typename varT>
203 void USER_PROPERTY<varT>::ModifyTime() throw()
205 modificationTime = stgTime;
207 //-----------------------------------------------------------------------------
208 template <typename varT>
210 void USER_PROPERTY<varT>::Set(const varT & rvalue)
212 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
214 typename std::set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
218 ni = beforeNotifiers.begin();
219 while (ni != beforeNotifiers.end())
220 (*ni++)->Notify(oldVal, rvalue);
223 modificationTime = stgTime;
225 ni = afterNotifiers.begin();
226 while (ni != afterNotifiers.end())
227 (*ni++)->Notify(oldVal, rvalue);
229 //-----------------------------------------------------------------------------
230 template <typename varT>
232 USER_PROPERTY<varT> & USER_PROPERTY<varT>::operator= (const varT & newValue)
237 //-----------------------------------------------------------------------------
238 template <typename varT>
240 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
242 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
243 beforeNotifiers.insert(n);
245 //-----------------------------------------------------------------------------
246 template <typename varT>
248 void USER_PROPERTY<varT>::DelBeforeNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n)
250 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
251 beforeNotifiers.erase(const_cast<PROPERTY_NOTIFIER_BASE<varT> *>(n));
253 //-----------------------------------------------------------------------------
254 template <typename varT>
256 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
258 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
259 afterNotifiers.insert(n);
261 //-----------------------------------------------------------------------------
262 template <typename varT>
264 void USER_PROPERTY<varT>::DelAfterNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n)
266 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
267 afterNotifiers.erase(const_cast<PROPERTY_NOTIFIER_BASE<varT> *>(n));
269 //-----------------------------------------------------------------------------
270 //-----------------------------------------------------------------------------
271 //-----------------------------------------------------------------------------
272 template <typename varT>
274 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(varT & val,
275 const std::string & n,
279 const std::string & sd)
281 : USER_PROPERTY<varT>(val),
289 //-------------------------------------------------------------------------
290 template <typename varT>
291 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
293 const std::string & login,
295 const std::string & msg)
297 const PRIV * priv = admin->GetPriv();
299 if ((priv->userConf && !isStat) ||
300 (priv->userStat && isStat) ||
301 (priv->userPasswd && isPassword) ||
302 (priv->userCash && name == "cash"))
304 std::stringstream oldVal;
305 std::stringstream newVal;
307 oldVal.flags(oldVal.flags() | std::ios::fixed);
308 newVal.flags(newVal.flags() | std::ios::fixed);
310 oldVal << USER_PROPERTY<varT>::ConstData();
313 OnChange(login, name, oldVal.str(), newVal.str(), admin);
317 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
321 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
323 USER_PROPERTY<varT>::Set(val);
328 WriteAccessDenied(login, admin, name);
333 //-------------------------------------------------------------------------
334 template <typename varT>
336 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const std::string & login,
338 const std::string & parameter)
340 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
341 admin->GetLogStr().c_str(), login.c_str(), parameter.c_str());
343 //-------------------------------------------------------------------------
344 template <typename varT>
346 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const std::string & login,
348 const std::string & parameter,
349 const std::string & oldValue,
350 const std::string & newValue,
351 const std::string & msg,
354 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
355 admin->GetLogStr().c_str(),
362 store->WriteUserChgLog(login, admin->GetLogin(), admin->GetIP(), parameter, oldValue, newValue, msg);
364 //-------------------------------------------------------------------------
365 template <typename varT>
366 void USER_PROPERTY_LOGGED<varT>::OnChange(const std::string & login,
367 const std::string & paramName,
368 const std::string & oldValue,
369 const std::string & newValue,
372 std::string filePath = scriptsDir + "/OnChange";
374 if (access(filePath.c_str(), X_OK) == 0)
376 std::string execString("\"" + filePath + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin->GetLogin() + "\" \"" + admin->GetIPStr() + "\"");
377 ScriptExec(execString.c_str());
381 stgLogger("Script OnChange cannot be executed. File %s not found.", filePath.c_str());
384 //-------------------------------------------------------------------------
385 //-------------------------------------------------------------------------
386 //-------------------------------------------------------------------------
387 std::string USER_PROPERTIES::GetPropertyValue(const std::string & name) const
389 std::map<std::string, USER_PROPERTY_BASE*>::iterator it = params.find(name);
390 if (it != params.end()) return it->second.ToString();
393 //-------------------------------------------------------------------------
394 //-------------------------------------------------------------------------
395 //-------------------------------------------------------------------------
396 template<typename varT>
398 std::ostream & operator<< (std::ostream & stream, const USER_PROPERTY<varT> & value)
400 return stream << value.ConstData();
402 //-----------------------------------------------------------------------------
403 template<typename varT>
404 std::string USER_PROPERTY<varT>::ToString() const
406 std::stringstream stream;
410 #endif // USER_PROPERTY_H