]> git.stg.codes - stg.git/blob - include/stg/user_property.h
Initialize additional members in constructor for PROPERTY
[stg.git] / include / stg / user_property.h
1 /*
2 $Revision: 1.44 $
3 $Date: 2010/09/13 05:54:43 $
4 $Author: faust $
5 */
6
7 #ifndef USER_PROPERTY_H
8 #define USER_PROPERTY_H
9
10 #include <unistd.h> // access
11
12 #include <ctime>
13 #include <string>
14 #include <set>
15 #include <sstream>
16 #include <iostream>
17
18 #include "store.h"
19 #include "admin.h"
20 #include "notifer.h"
21 #include "logger.h"
22 #include "locker.h"
23 #include "scriptexecuter.h"
24 #include "noncopyable.h"
25
26 extern const volatile time_t stgTime;
27
28 //-----------------------------------------------------------------------------
29 template<typename varT>
30 class USER_PROPERTY {
31 public:
32     USER_PROPERTY(varT & val);
33     virtual ~USER_PROPERTY();
34
35     void Set(const varT & rvalue);
36
37     USER_PROPERTY<varT> & operator= (const varT & rvalue);
38
39     const varT * operator&() const throw() { return &value; }
40     const varT & ConstData() const throw() { return value; }
41
42     operator const varT&() const throw() { return value; }
43
44     void    AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
45     void    DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
46
47     void    AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
48     void    DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n);
49
50     time_t  ModificationTime() const throw() { return modificationTime; }
51     void    ModifyTime() throw();
52
53 private:
54     varT & value;
55     time_t modificationTime;
56     std::set<PROPERTY_NOTIFIER_BASE<varT> *> beforeNotifiers;
57     std::set<PROPERTY_NOTIFIER_BASE<varT> *> afterNotifiers;
58     pthread_mutex_t mutex;
59 };
60 //-----------------------------------------------------------------------------
61 template<typename varT>
62 class USER_PROPERTY_LOGGED: public USER_PROPERTY<varT> {
63 public:
64     USER_PROPERTY_LOGGED(varT & val,
65                          const std::string & n,
66                          bool isPassword,
67                          bool isStat,
68                          STG_LOGGER & logger,
69                          const std::string & sd);
70     virtual ~USER_PROPERTY_LOGGED() {}
71
72     USER_PROPERTY_LOGGED<varT> * GetPointer() throw() { return this; }
73     const varT & Get() const { return USER_PROPERTY<varT>::ConstData(); }
74     const std::string & GetName() const { return name; }
75     bool Set(const varT & val,
76              const ADMIN * admin,
77              const std::string & login,
78              const STORE * store,
79              const std::string & msg = "");
80 private:
81     void WriteAccessDenied(const std::string & login,
82                            const ADMIN * admin,
83                            const std::string & parameter);
84
85     void WriteSuccessChange(const std::string & login,
86                             const ADMIN * admin,
87                             const std::string & parameter,
88                             const std::string & oldValue,
89                             const std::string & newValue,
90                             const std::string & msg,
91                             const STORE * store);
92
93     void OnChange(const std::string & login,
94                   const std::string & paramName,
95                   const std::string & oldValue,
96                   const std::string & newValue,
97                   const ADMIN  * admin);
98
99     STG_LOGGER &      stgLogger;
100     bool              isPassword;
101     bool              isStat;
102     std::string       name;
103     const std::string scriptsDir;
104 };
105 //-----------------------------------------------------------------------------
106 class USER_PROPERTIES : private NONCOPYABLE {
107 /*
108  В этом месте важен порядок следования приватной и открытой частей.
109  Это связано с тем, что часть которая находится в публичной секции
110  по сути является завуалированной ссылкой на закрытую часть. Т.о. нам нужно
111  чтобы конструкторы из закрытой части вызвались раньше открытой. Поэтомому в
112  начале идет закрытая секция
113  * */
114
115 private:
116     USER_STAT stat;
117     USER_CONF conf;
118
119 public:
120     USER_PROPERTIES(const std::string & sd);
121
122     USER_STAT & Stat() { return stat; }
123     USER_CONF & Conf() { return conf; }
124     const USER_STAT & GetStat() const { return stat; }
125     const USER_CONF & GetConf() const { return conf; }
126     void SetStat(const USER_STAT & s) { stat = s; }
127     void SetConf(const USER_CONF & c) { conf = c; }
128
129     void SetProperties(const USER_PROPERTIES & p) { stat = p.stat; conf = p.conf; }
130
131     USER_PROPERTY_LOGGED<double>            cash;
132     USER_PROPERTY_LOGGED<DIR_TRAFF>         up;
133     USER_PROPERTY_LOGGED<DIR_TRAFF>         down;
134     USER_PROPERTY_LOGGED<double>            lastCashAdd;
135     USER_PROPERTY_LOGGED<time_t>            passiveTime;
136     USER_PROPERTY_LOGGED<time_t>            lastCashAddTime;
137     USER_PROPERTY_LOGGED<double>            freeMb;
138     USER_PROPERTY_LOGGED<time_t>            lastActivityTime;
139
140     USER_PROPERTY_LOGGED<std::string>       password;
141     USER_PROPERTY_LOGGED<int>               passive;
142     USER_PROPERTY_LOGGED<int>               disabled;
143     USER_PROPERTY_LOGGED<int>               disabledDetailStat;
144     USER_PROPERTY_LOGGED<int>               alwaysOnline;
145     USER_PROPERTY_LOGGED<std::string>       tariffName;
146     USER_PROPERTY_LOGGED<std::string>       nextTariff;
147     USER_PROPERTY_LOGGED<std::string>       address;
148     USER_PROPERTY_LOGGED<std::string>       note;
149     USER_PROPERTY_LOGGED<std::string>       group;
150     USER_PROPERTY_LOGGED<std::string>       email;
151     USER_PROPERTY_LOGGED<std::string>       phone;
152     USER_PROPERTY_LOGGED<std::string>       realName;
153     USER_PROPERTY_LOGGED<double>            credit;
154     USER_PROPERTY_LOGGED<time_t>            creditExpire;
155     USER_PROPERTY_LOGGED<USER_IPS>          ips;
156     USER_PROPERTY_LOGGED<std::string>       userdata0;
157     USER_PROPERTY_LOGGED<std::string>       userdata1;
158     USER_PROPERTY_LOGGED<std::string>       userdata2;
159     USER_PROPERTY_LOGGED<std::string>       userdata3;
160     USER_PROPERTY_LOGGED<std::string>       userdata4;
161     USER_PROPERTY_LOGGED<std::string>       userdata5;
162     USER_PROPERTY_LOGGED<std::string>       userdata6;
163     USER_PROPERTY_LOGGED<std::string>       userdata7;
164     USER_PROPERTY_LOGGED<std::string>       userdata8;
165     USER_PROPERTY_LOGGED<std::string>       userdata9;
166 };
167 //=============================================================================
168
169 //-----------------------------------------------------------------------------
170 //-----------------------------------------------------------------------------
171 //-----------------------------------------------------------------------------
172 template <typename varT>
173 inline
174 USER_PROPERTY<varT>::USER_PROPERTY(varT & val)
175     : value(val),
176       modificationTime(stgTime),
177       beforeNotifiers(),
178       afterNotifiers(),
179       mutex()
180 {
181 pthread_mutex_init(&mutex, NULL);
182 }
183 //-----------------------------------------------------------------------------
184 template <typename varT>
185 inline
186 USER_PROPERTY<varT>::~USER_PROPERTY()
187 {
188 pthread_mutex_destroy(&mutex);
189 }
190 //-----------------------------------------------------------------------------
191 template <typename varT>
192 inline
193 void USER_PROPERTY<varT>::ModifyTime() throw()
194 {
195 modificationTime = stgTime;
196 }
197 //-----------------------------------------------------------------------------
198 template <typename varT>
199 inline
200 void USER_PROPERTY<varT>::Set(const varT & rvalue)
201 {
202 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
203
204 typename std::set<PROPERTY_NOTIFIER_BASE<varT> *>::iterator ni;
205
206 varT oldVal = value;
207
208 ni = beforeNotifiers.begin();
209 while (ni != beforeNotifiers.end())
210     (*ni++)->Notify(oldVal, rvalue);
211
212 value = rvalue;
213 modificationTime = stgTime;
214
215 ni = afterNotifiers.begin();
216 while (ni != afterNotifiers.end())
217     (*ni++)->Notify(oldVal, rvalue);
218 }
219 //-----------------------------------------------------------------------------
220 template <typename varT>
221 inline
222 USER_PROPERTY<varT> & USER_PROPERTY<varT>::operator= (const varT & newValue)
223 {
224 Set(newValue);
225 return *this;
226 }
227 //-----------------------------------------------------------------------------
228 template <typename varT>
229 inline
230 void USER_PROPERTY<varT>::AddBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
231 {
232 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
233 beforeNotifiers.insert(n);
234 }
235 //-----------------------------------------------------------------------------
236 template <typename varT>
237 inline
238 void USER_PROPERTY<varT>::DelBeforeNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
239 {
240 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
241 beforeNotifiers.erase(n);
242 }
243 //-----------------------------------------------------------------------------
244 template <typename varT>
245 inline
246 void USER_PROPERTY<varT>::AddAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
247 {
248 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
249 afterNotifiers.insert(n);
250 }
251 //-----------------------------------------------------------------------------
252 template <typename varT>
253 inline
254 void USER_PROPERTY<varT>::DelAfterNotifier(PROPERTY_NOTIFIER_BASE<varT> * n)
255 {
256 STG_LOCKER locker(&mutex, __FILE__, __LINE__);
257 afterNotifiers.erase(n);
258 }
259 //-----------------------------------------------------------------------------
260 //-----------------------------------------------------------------------------
261 //-----------------------------------------------------------------------------
262 template <typename varT>
263 inline
264 USER_PROPERTY_LOGGED<varT>::USER_PROPERTY_LOGGED(varT & val,
265                                                  const std::string & n,
266                                                  bool isPass,
267                                                  bool isSt,
268                                                  STG_LOGGER & logger,
269                                                  const std::string & sd)
270
271     : USER_PROPERTY<varT>(val),
272       stgLogger(logger),
273       isPassword(isPass),
274       isStat(isSt),
275       name(n),
276       scriptsDir(sd)
277 {
278 }
279 //-------------------------------------------------------------------------
280 template <typename varT>
281 bool USER_PROPERTY_LOGGED<varT>::Set(const varT & val,
282                                      const ADMIN * admin,
283                                      const std::string & login,
284                                      const STORE * store,
285                                      const std::string & msg)
286 {
287 const PRIV * priv = admin->GetPriv();
288 std::string adm_login = admin->GetLogin();
289 std::string adm_ip = admin->GetIPStr();
290
291 if ((priv->userConf && !isStat) ||
292     (priv->userStat && isStat) ||
293     (priv->userPasswd && isPassword) ||
294     (priv->userCash && name == "cash"))
295     {
296     std::stringstream oldVal;
297     std::stringstream newVal;
298
299     oldVal.flags(oldVal.flags() | ios::fixed);
300     newVal.flags(newVal.flags() | ios::fixed);
301
302     oldVal << USER_PROPERTY<varT>::ConstData();
303     newVal << val;
304
305     OnChange(login, name, oldVal.str(), newVal.str(), admin);
306
307     if (isPassword)
308         {
309         WriteSuccessChange(login, admin, name, "******", "******", msg, store);
310         }
311     else
312         {
313         WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
314         }
315     USER_PROPERTY<varT>::Set(val);
316     return true;
317     }
318 else
319     {
320     WriteAccessDenied(login, admin, name);
321     return false;
322     }
323 return true;
324 }
325 //-------------------------------------------------------------------------
326 template <typename varT>
327 inline
328 void USER_PROPERTY_LOGGED<varT>::WriteAccessDenied(const std::string & login,
329                                                    const ADMIN * admin,
330                                                    const std::string & parameter)
331 {
332 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
333           admin->GetLogStr().c_str(), login.c_str(), parameter.c_str());
334 }
335 //-------------------------------------------------------------------------
336 template <typename varT>
337 inline
338 void USER_PROPERTY_LOGGED<varT>::WriteSuccessChange(const std::string & login,
339                                                     const ADMIN * admin,
340                                                     const std::string & parameter,
341                                                     const std::string & oldValue,
342                                                     const std::string & newValue,
343                                                     const std::string & msg,
344                                                     const STORE * store)
345 {
346 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
347           admin->GetLogStr().c_str(),
348           login.c_str(),
349           parameter.c_str(),
350           oldValue.c_str(),
351           newValue.c_str(),
352           msg.c_str());
353
354 store->WriteUserChgLog(login, admin->GetLogin(), admin->GetIP(), parameter, oldValue, newValue, msg);
355 }
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,
362                                           const ADMIN * admin)
363 {
364 std::string filePath = scriptsDir + "/OnChange";
365
366 if (access(filePath.c_str(), X_OK) == 0)
367     {
368     std::string execString("\"" + filePath + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin->GetLogin() + "\" \"" + admin->GetIPStr() + "\"");
369     ScriptExec(execString.c_str());
370     }
371 else
372     {
373     stgLogger("Script OnChange cannot be executed. File %s not found.", filePath.c_str());
374     }
375 }
376 //-------------------------------------------------------------------------
377 //-------------------------------------------------------------------------
378 //-------------------------------------------------------------------------
379 template<typename varT>
380 inline
381 ostream & operator<< (ostream & stream, const USER_PROPERTY<varT> & value)
382 {
383 return stream << value.ConstData();
384 }
385 //-----------------------------------------------------------------------------
386
387 #endif // USER_PROPERTY_H