3 $Date: 2010/09/13 05:54:43 $
7 #ifndef USER_PROPERTY_H
8 #define USER_PROPERTY_H
10 #include <unistd.h> // access
19 #include "stg/logger.h"
20 #include "stg/locker.h"
21 #include "stg/scriptexecuter.h"
22 #include "stg/common.h"
27 #include "noncopyable.h"
29 extern volatile time_t stgTime;
30 //-----------------------------------------------------------------------------
31 class USER_PROPERTY_BASE {
33 virtual std::string ToString() const = 0;
35 //-----------------------------------------------------------------------------
36 template<typename varT>
37 class USER_PROPERTY : public USER_PROPERTY_BASE {
39 USER_PROPERTY(varT & val);
40 virtual ~USER_PROPERTY();
42 void Set(const varT & rvalue);
44 USER_PROPERTY<varT> & operator= (const varT & rvalue);
46 const varT * operator&() const throw() { return &value; }
47 const varT & ConstData() const throw() { return value; }
49 operator const varT&() const throw() { return value; }
51 void AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
52 void DelBeforeNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n);
54 void AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
55 void DelAfterNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n);
57 time_t ModificationTime() const throw() { return modificationTime; }
58 void ModifyTime() throw();
60 std::string ToString() const;
63 time_t modificationTime;
64 std::set<PROPERTY_NOTIFIER_BASE<varT> *> beforeNotifiers;
65 std::set<PROPERTY_NOTIFIER_BASE<varT> *> afterNotifiers;
66 pthread_mutex_t mutex;
68 //-----------------------------------------------------------------------------
69 template<typename varT>
70 class USER_PROPERTY_LOGGED: public USER_PROPERTY<varT> {
72 USER_PROPERTY_LOGGED(varT & val,
73 const std::string & n,
77 const std::string & sd,
78 std::map<std::string, USER_PROPERTY_BASE*> & properties);
79 virtual ~USER_PROPERTY_LOGGED() {}
81 USER_PROPERTY_LOGGED<varT> * GetPointer() throw() { return this; }
82 const varT & Get() const { return USER_PROPERTY<varT>::ConstData(); }
83 const std::string & GetName() const { return name; }
84 bool Set(const varT & val,
86 const std::string & login,
88 const std::string & msg = "");
90 void WriteAccessDenied(const std::string & login,
92 const std::string & parameter);
94 void WriteSuccessChange(const std::string & login,
96 const std::string & parameter,
97 const std::string & oldValue,
98 const std::string & newValue,
99 const std::string & msg,
100 const STORE * store);
102 void OnChange(const std::string & login,
103 const std::string & paramName,
104 const std::string & oldValue,
105 const std::string & newValue,
106 const ADMIN * admin);
108 STG_LOGGER & stgLogger;
112 const std::string scriptsDir;
114 //-----------------------------------------------------------------------------
115 class USER_PROPERTIES : private NONCOPYABLE {
117 В этом месте важен порядок следования приватной и открытой частей.
118 Это связано с тем, что часть которая находится в публичной секции
119 по сути является завуалированной ссылкой на закрытую часть. Т.о. нам нужно
120 чтобы конструкторы из закрытой части вызвались раньше открытой. Поэтомому в
121 начале идет закрытая секция
128 std::map<std::string, USER_PROPERTY_BASE *> properties;
130 USER_PROPERTIES(const std::string & sd);
132 USER_STAT & Stat() { return stat; }
133 USER_CONF & Conf() { return conf; }
134 const USER_STAT & GetStat() const { return stat; }
135 const USER_CONF & GetConf() const { return conf; }
136 void SetStat(const USER_STAT & s) { stat = s; }
137 void SetConf(const USER_CONF & c) { conf = c; }
139 void SetProperties(const USER_PROPERTIES & p) { stat = p.stat; conf = p.conf; }
141 std::string GetPropertyValue(const std::string & name) const;
142 bool Exists(const std::string & name) const;
144 USER_PROPERTY_LOGGED<double> cash;
145 USER_PROPERTY_LOGGED<DIR_TRAFF> up;
146 USER_PROPERTY_LOGGED<DIR_TRAFF> down;
147 USER_PROPERTY_LOGGED<double> lastCashAdd;
148 USER_PROPERTY_LOGGED<time_t> passiveTime;
149 USER_PROPERTY_LOGGED<time_t> lastCashAddTime;
150 USER_PROPERTY_LOGGED<double> freeMb;
151 USER_PROPERTY_LOGGED<time_t> lastActivityTime;
153 USER_PROPERTY_LOGGED<std::string> password;
154 USER_PROPERTY_LOGGED<int> passive;
155 USER_PROPERTY_LOGGED<int> disabled;
156 USER_PROPERTY_LOGGED<int> disabledDetailStat;
157 USER_PROPERTY_LOGGED<int> alwaysOnline;
158 USER_PROPERTY_LOGGED<std::string> tariffName;
159 USER_PROPERTY_LOGGED<std::string> nextTariff;
160 USER_PROPERTY_LOGGED<std::string> address;
161 USER_PROPERTY_LOGGED<std::string> note;
162 USER_PROPERTY_LOGGED<std::string> group;
163 USER_PROPERTY_LOGGED<std::string> email;
164 USER_PROPERTY_LOGGED<std::string> phone;
165 USER_PROPERTY_LOGGED<std::string> realName;
166 USER_PROPERTY_LOGGED<double> credit;
167 USER_PROPERTY_LOGGED<time_t> creditExpire;
168 USER_PROPERTY_LOGGED<USER_IPS> ips;
169 USER_PROPERTY_LOGGED<std::string> userdata0;
170 USER_PROPERTY_LOGGED<std::string> userdata1;
171 USER_PROPERTY_LOGGED<std::string> userdata2;
172 USER_PROPERTY_LOGGED<std::string> userdata3;
173 USER_PROPERTY_LOGGED<std::string> userdata4;
174 USER_PROPERTY_LOGGED<std::string> userdata5;
175 USER_PROPERTY_LOGGED<std::string> userdata6;
176 USER_PROPERTY_LOGGED<std::string> userdata7;
177 USER_PROPERTY_LOGGED<std::string> userdata8;
178 USER_PROPERTY_LOGGED<std::string> userdata9;
180 //=============================================================================
182 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
184 //-----------------------------------------------------------------------------
185 template <typename varT>
187 USER_PROPERTY<varT>::USER_PROPERTY(varT & val)
189 modificationTime(stgTime),
194 pthread_mutex_init(&mutex, NULL);
196 //-----------------------------------------------------------------------------
197 template <typename varT>
199 USER_PROPERTY<varT>::~USER_PROPERTY()
201 pthread_mutex_destroy(&mutex);
203 //-----------------------------------------------------------------------------
204 template <typename varT>
206 void USER_PROPERTY<varT>::ModifyTime() throw()
208 modificationTime = stgTime;
210 //-----------------------------------------------------------------------------
211 template <typename varT>
213 void USER_PROPERTY<varT>::Set(const varT & rvalue)
215 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
217 typename std::set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
221 ni = beforeNotifiers.begin();
222 while (ni != beforeNotifiers.end())
223 (*ni++)->Notify(oldVal, rvalue);
226 modificationTime = stgTime;
228 ni = afterNotifiers.begin();
229 while (ni != afterNotifiers.end())
230 (*ni++)->Notify(oldVal, rvalue);
232 //-----------------------------------------------------------------------------
233 template <typename varT>
235 USER_PROPERTY<varT> & USER_PROPERTY<varT>::operator= (const varT & newValue)
240 //-----------------------------------------------------------------------------
241 template <typename varT>
243 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
245 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
246 beforeNotifiers.insert(n);
248 //-----------------------------------------------------------------------------
249 template <typename varT>
251 void USER_PROPERTY<varT>::DelBeforeNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n)
253 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
254 beforeNotifiers.erase(const_cast<PROPERTY_NOTIFIER_BASE<varT> *>(n));
256 //-----------------------------------------------------------------------------
257 template <typename varT>
259 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
261 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
262 afterNotifiers.insert(n);
264 //-----------------------------------------------------------------------------
265 template <typename varT>
267 void USER_PROPERTY<varT>::DelAfterNotifier(const PROPERTY_NOTIFIER_BASE<varT> * n)
269 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
270 afterNotifiers.erase(const_cast<PROPERTY_NOTIFIER_BASE<varT> *>(n));
272 //-----------------------------------------------------------------------------
273 //-----------------------------------------------------------------------------
274 //-----------------------------------------------------------------------------
275 template <typename varT>
277 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(varT & val,
278 const std::string & n,
282 const std::string & sd,
283 std::map<std::string, USER_PROPERTY_BASE*> & properties)
285 : USER_PROPERTY<varT>(val),
292 properties.insert(std::make_pair(ToLower(name), this));
294 //-------------------------------------------------------------------------
295 template <typename varT>
296 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
298 const std::string & login,
300 const std::string & msg)
302 const PRIV * priv = admin->GetPriv();
304 if ((priv->userConf && !isStat) ||
305 (priv->userStat && isStat) ||
306 (priv->userPasswd && isPassword) ||
307 (priv->userCash && name == "cash"))
309 std::stringstream oldVal;
310 std::stringstream newVal;
312 oldVal.flags(oldVal.flags() | std::ios::fixed);
313 newVal.flags(newVal.flags() | std::ios::fixed);
315 oldVal << USER_PROPERTY<varT>::ConstData();
318 OnChange(login, name, oldVal.str(), newVal.str(), admin);
322 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
326 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
328 USER_PROPERTY<varT>::Set(val);
333 WriteAccessDenied(login, admin, name);
338 //-------------------------------------------------------------------------
339 template <typename varT>
341 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const std::string & login,
343 const std::string & parameter)
345 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
346 admin->GetLogStr().c_str(), login.c_str(), parameter.c_str());
348 //-------------------------------------------------------------------------
349 template <typename varT>
351 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const std::string & login,
353 const std::string & parameter,
354 const std::string & oldValue,
355 const std::string & newValue,
356 const std::string & msg,
359 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
360 admin->GetLogStr().c_str(),
367 store->WriteUserChgLog(login, admin->GetLogin(), admin->GetIP(), parameter, oldValue, newValue, msg);
369 //-------------------------------------------------------------------------
370 template <typename varT>
371 void USER_PROPERTY_LOGGED<varT>::OnChange(const std::string & login,
372 const std::string & paramName,
373 const std::string & oldValue,
374 const std::string & newValue,
377 std::string filePath = scriptsDir + "/OnChange";
379 if (access(filePath.c_str(), X_OK) == 0)
381 std::string execString("\"" + filePath + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin->GetLogin() + "\" \"" + admin->GetIPStr() + "\"");
382 ScriptExec(execString.c_str());
386 stgLogger("Script OnChange cannot be executed. File %s not found.", filePath.c_str());
389 //-------------------------------------------------------------------------
390 //-------------------------------------------------------------------------
391 //-------------------------------------------------------------------------
393 std::string USER_PROPERTIES::GetPropertyValue(const std::string & name) const
395 std::map<std::string, USER_PROPERTY_BASE*>::const_iterator it = properties.find(ToLower(name));
396 if (it == properties.end())
398 return it->second->ToString();
400 //-----------------------------------------------------------------------------
402 bool USER_PROPERTIES::Exists(const std::string & name) const
404 return properties.find(ToLower(name)) != properties.end();
406 //-------------------------------------------------------------------------
407 //-------------------------------------------------------------------------
408 //-------------------------------------------------------------------------
409 template<typename varT>
411 std::ostream & operator<< (std::ostream & stream, const USER_PROPERTY<varT> & value)
413 return stream << value.ConstData();
415 //-----------------------------------------------------------------------------
416 template<typename varT>
418 std::string USER_PROPERTY<varT>::ToString() const
420 std::ostringstream stream;
424 #endif // USER_PROPERTY_H