3 $Date: 2010/09/13 05:54:43 $
7 #ifndef USER_PROPERTY_H
8 #define USER_PROPERTY_H
17 #include "stg_logger.h"
20 #include "stg_logger.h"
21 #include "stg_locker.h"
22 #include "script_executer.h"
24 extern const volatile time_t stgTime;
26 //-----------------------------------------------------------------------------
27 template<typename varT>
30 USER_PROPERTY(varT& val);
31 virtual ~USER_PROPERTY();
33 void Set(const varT & rvalue);
35 USER_PROPERTY<varT>& operator= (const varT & rvalue);
36 USER_PROPERTY<varT>& operator-= (const varT & rvalue);
38 const varT * operator&() const throw();
39 const varT& ConstData() const throw();
41 operator const varT&() const throw()
46 void AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
47 void DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
49 void AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
50 void DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
52 time_t ModificationTime() const throw();
53 void ModifyTime() throw();
57 time_t modificationTime;
58 std::set<PROPERTY_NOTIFIER_BASE<varT> *> beforeNotifiers;
59 std::set<PROPERTY_NOTIFIER_BASE<varT> *> afterNotifiers;
60 mutable pthread_mutex_t mutex;
62 //-----------------------------------------------------------------------------
63 template<typename varT>
64 class USER_PROPERTY_LOGGED: public USER_PROPERTY<varT> {
66 USER_PROPERTY_LOGGED(varT & val,
71 const std::string & sd);
72 virtual ~USER_PROPERTY_LOGGED();
74 USER_PROPERTY_LOGGED<varT> * GetPointer() throw();
75 const varT & Get() const;
76 const std::string & GetName() const;
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 {
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>
175 USER_PROPERTY<varT>::USER_PROPERTY(varT& val)
178 pthread_mutex_init(&mutex, NULL);
179 modificationTime = stgTime;
181 //-----------------------------------------------------------------------------
182 template <typename varT>
183 USER_PROPERTY<varT>::~USER_PROPERTY()
186 //-----------------------------------------------------------------------------
187 template <typename varT>
188 void USER_PROPERTY<varT>::ModifyTime() throw()
190 modificationTime = stgTime;
192 //-----------------------------------------------------------------------------
193 template <typename varT>
194 void USER_PROPERTY<varT>::Set(const varT & rvalue)
196 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
198 typename std::set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
202 ni = beforeNotifiers.begin();
203 while (ni != beforeNotifiers.end())
204 (*ni++)->Notify(oldVal, rvalue);
207 modificationTime = stgTime;
209 ni = afterNotifiers.begin();
210 while (ni != afterNotifiers.end())
211 (*ni++)->Notify(oldVal, rvalue);
213 //-----------------------------------------------------------------------------
214 template <typename varT>
215 USER_PROPERTY<varT>& USER_PROPERTY<varT>::operator= (const varT & newValue)
220 //-----------------------------------------------------------------------------
221 template <typename varT>
222 USER_PROPERTY<varT>& USER_PROPERTY<varT>::operator-= (const varT & delta)
224 varT newValue = ConstData() - delta;
228 //-----------------------------------------------------------------------------
229 template <typename varT>
230 const varT * USER_PROPERTY<varT>::operator&() const throw()
234 //-----------------------------------------------------------------------------
235 template <typename varT>
236 const varT& USER_PROPERTY<varT>::ConstData() const throw()
240 //-----------------------------------------------------------------------------
241 template <typename varT>
242 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
244 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
245 beforeNotifiers.insert(n);
247 //-----------------------------------------------------------------------------
248 template <typename varT>
249 void USER_PROPERTY<varT>::DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
251 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
252 beforeNotifiers.erase(n);
254 //-----------------------------------------------------------------------------
255 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>
263 void USER_PROPERTY<varT>::DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
265 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
266 afterNotifiers.erase(n);
268 //-----------------------------------------------------------------------------
269 template <typename varT>
270 time_t USER_PROPERTY<varT>::ModificationTime() const throw()
272 return modificationTime;
274 //-----------------------------------------------------------------------------
275 //-----------------------------------------------------------------------------
276 //-----------------------------------------------------------------------------
277 template <typename varT>
278 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(varT& val,
283 const std::string & sd)
285 : USER_PROPERTY<varT>(val),
293 //-----------------------------------------------------------------------------
294 template <typename varT>
295 USER_PROPERTY_LOGGED<varT>::~USER_PROPERTY_LOGGED()
298 //-----------------------------------------------------------------------------
299 template <typename varT>
300 USER_PROPERTY_LOGGED<varT> * USER_PROPERTY_LOGGED<varT>::GetPointer() throw()
304 //-----------------------------------------------------------------------------
305 template <typename varT>
306 const varT & USER_PROPERTY_LOGGED<varT>::Get() const
308 return USER_PROPERTY<varT>::ConstData();
310 //-------------------------------------------------------------------------
311 template <typename varT>
312 const std::string & USER_PROPERTY_LOGGED<varT>::GetName() const
316 //-------------------------------------------------------------------------
317 template <typename varT>
318 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
320 const std::string & login,
322 const std::string & msg)
324 const PRIV * priv = admin->GetPriv();
325 std::string adm_login = admin->GetLogin();
326 std::string adm_ip = admin->GetIPStr();
328 if ((priv->userConf && !isStat) || (priv->userStat && isStat) || (priv->userPasswd && isPassword) || (priv->userCash && name == "cash"))
333 oldVal.flags(oldVal.flags() | ios::fixed);
334 newVal.flags(newVal.flags() | ios::fixed);
336 oldVal << USER_PROPERTY<varT>::ConstData();
339 OnChange(login, name, oldVal.str(), newVal.str(), admin);
343 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
347 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
349 USER_PROPERTY<varT>::Set(val);
354 WriteAccessDenied(login, admin, name);
359 //-------------------------------------------------------------------------
360 template <typename varT>
361 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const std::string & login,
363 const std::string & parameter)
365 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
366 admin->GetLogStr().c_str(), login.c_str(), parameter.c_str());
368 //-------------------------------------------------------------------------
369 template <typename varT>
370 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const std::string & login,
372 const std::string & parameter,
373 const std::string & oldValue,
374 const std::string & newValue,
375 const std::string & msg,
378 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
379 admin->GetLogStr().c_str(),
386 store->WriteUserChgLog(login, admin->GetLogin(), admin->GetIP(), parameter, oldValue, newValue, msg);
388 //-------------------------------------------------------------------------
389 template <typename varT>
390 void USER_PROPERTY_LOGGED<varT>::OnChange(const std::string & login,
391 const std::string & paramName,
392 const std::string & oldValue,
393 const std::string & newValue,
398 str1 = scriptsDir + "/OnChange";
400 if (access(str1.c_str(), X_OK) == 0)
402 std::string str2("\"" + str1 + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin->GetLogin() + "\" \"" + admin->GetIPStr() + "\"");
407 stgLogger("Script OnChange cannot be executed. File %s not found.", str1.c_str());
410 //-------------------------------------------------------------------------
411 //-------------------------------------------------------------------------
412 //-------------------------------------------------------------------------
413 template<typename varT>
414 ostream & operator<< (ostream & stream, const USER_PROPERTY<varT> & value)
416 return stream << value.ConstData();
418 //-----------------------------------------------------------------------------
420 #endif // USER_PROPERTY_H