3 #include "stg/user_conf.h"
4 #include "stg/user_ips.h"
7 #include "stg/subscriptions.h"
8 #include "stg/admin_conf.h"
9 #include "stg/logger.h"
10 #include "stg/locker.h"
11 #include "stg/settings.h"
12 #include "stg/scriptexecuter.h"
13 #include "stg/common.h"
23 #include <unistd.h> // access
27 //-----------------------------------------------------------------------------
28 struct UserPropertyBase
30 virtual ~UserPropertyBase() = default;
31 virtual std::string ToString() const = 0;
33 //-----------------------------------------------------------------------------
34 using Registry = std::map<std::string, UserPropertyBase*>;
35 //-----------------------------------------------------------------------------
37 class UserProperty : public UserPropertyBase
40 explicit UserProperty(T& val);
42 void Set(const T& rhs);
43 T get() const { return value; }
45 UserProperty<T>& operator=(const T& rhs);
47 const T* operator&() const noexcept { return &value; }
48 const T& ConstData() const noexcept { return value; }
50 operator const T&() const noexcept { return value; }
53 auto beforeChange(F&& f) { return m_beforeCallbacks.add(std::forward<F>(f)); }
55 auto afterChange(F&& f) { return m_afterCallbacks.add(std::forward<F>(f)); }
57 time_t ModificationTime() const noexcept { return modificationTime; }
58 void ModifyTime() noexcept;
60 std::string ToString() const override;
63 time_t modificationTime;
64 Subscriptions<T, T> m_beforeCallbacks;
65 Subscriptions<T, T> m_afterCallbacks;
68 //-----------------------------------------------------------------------------
70 class UserPropertyLogged: public UserProperty<T>
73 UserPropertyLogged(T& val,
78 Registry& properties);
80 UserPropertyLogged<T>* GetPointer() noexcept { return this; }
81 const UserPropertyLogged<T>* GetPointer() const noexcept { return this; }
82 const T& Get() const { return UserProperty<T>::ConstData(); }
83 const std::string& GetName() const { return name; }
84 bool Set(const T& 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,
102 void OnChange(const std::string& login,
103 const std::string& paramName,
104 const std::string& oldValue,
105 const std::string& newValue,
112 const Settings& settings;
114 //-----------------------------------------------------------------------------
118 В этом месте важен порядок следования приватной и открытой частей.
119 Это связано с тем, что часть которая находится в публичной секции
120 по сути является завуалированной ссылкой на закрытую часть. Т.о. нам нужно
121 чтобы конструкторы из закрытой части вызвались раньше открытой. Поэтомому в
122 начале идет закрытая секция
131 explicit UserProperties(const Settings& s);
133 UserStat& Stat() { return stat; }
134 UserConf& Conf() { return conf; }
135 const UserStat& GetStat() const { return stat; }
136 const UserConf& GetConf() const { return conf; }
137 void SetStat(const UserStat& s) { stat = s; }
138 void SetConf(const UserConf& c) { conf = c; }
140 void SetProperties(const UserProperties& p) { stat = p.stat; conf = p.conf; }
142 std::string GetPropertyValue(const std::string & name) const;
143 bool Exists(const std::string & name) const;
145 UserPropertyLogged<double> cash;
146 UserPropertyLogged<DirTraff> up;
147 UserPropertyLogged<DirTraff> down;
148 UserPropertyLogged<double> lastCashAdd;
149 UserPropertyLogged<time_t> passiveTime;
150 UserPropertyLogged<time_t> lastCashAddTime;
151 UserPropertyLogged<double> freeMb;
152 UserPropertyLogged<time_t> lastActivityTime;
154 UserPropertyLogged<std::string> password;
155 UserPropertyLogged<int> passive;
156 UserPropertyLogged<int> disabled;
157 UserPropertyLogged<int> disabledDetailStat;
158 UserPropertyLogged<int> alwaysOnline;
159 UserPropertyLogged<std::string> tariffName;
160 UserPropertyLogged<std::string> nextTariff;
161 UserPropertyLogged<std::string> address;
162 UserPropertyLogged<std::string> note;
163 UserPropertyLogged<std::string> group;
164 UserPropertyLogged<std::string> email;
165 UserPropertyLogged<std::string> phone;
166 UserPropertyLogged<std::string> realName;
167 UserPropertyLogged<double> credit;
168 UserPropertyLogged<time_t> creditExpire;
169 UserPropertyLogged<UserIPs> ips;
170 UserPropertyLogged<std::string> userdata0;
171 UserPropertyLogged<std::string> userdata1;
172 UserPropertyLogged<std::string> userdata2;
173 UserPropertyLogged<std::string> userdata3;
174 UserPropertyLogged<std::string> userdata4;
175 UserPropertyLogged<std::string> userdata5;
176 UserPropertyLogged<std::string> userdata6;
177 UserPropertyLogged<std::string> userdata7;
178 UserPropertyLogged<std::string> userdata8;
179 UserPropertyLogged<std::string> userdata9;
181 //=============================================================================
183 //-----------------------------------------------------------------------------
184 //-----------------------------------------------------------------------------
185 //-----------------------------------------------------------------------------
186 template <typename T>
188 UserProperty<T>::UserProperty(T& val)
190 modificationTime(time(NULL))
193 //-----------------------------------------------------------------------------
194 template <typename T>
196 void UserProperty<T>::ModifyTime() noexcept
198 modificationTime = time(NULL);
200 //-----------------------------------------------------------------------------
201 template <typename T>
203 void UserProperty<T>::Set(const T& rhs)
205 std::lock_guard<std::mutex> lock(mutex);
209 m_beforeCallbacks.notify(oldVal, rhs);
212 modificationTime = time(NULL);
214 m_afterCallbacks.notify(oldVal, rhs);
216 //-----------------------------------------------------------------------------
217 template <typename T>
219 UserProperty<T>& UserProperty<T>::operator=(const T& newValue)
224 //-----------------------------------------------------------------------------
225 //-----------------------------------------------------------------------------
226 //-----------------------------------------------------------------------------
227 template <typename T>
229 UserPropertyLogged<T>::UserPropertyLogged(T& val,
230 const std::string& n,
234 Registry& properties)
236 : UserProperty<T>(val),
237 stgLogger(Logger::get()),
243 properties.insert(std::make_pair(ToLower(name), this));
245 //-------------------------------------------------------------------------
246 template <typename T>
248 bool UserPropertyLogged<T>::Set(const T& val,
250 const std::string& login,
252 const std::string& msg)
254 const auto& priv = admin.priv();
256 if ((priv.userConf && !isStat) ||
257 (priv.userStat && isStat) ||
258 (priv.userPasswd && isPassword) ||
259 (priv.userCash && name == "cash"))
261 std::stringstream oldVal;
262 std::stringstream newVal;
264 oldVal.flags(oldVal.flags() | std::ios::fixed);
265 newVal.flags(newVal.flags() | std::ios::fixed);
267 oldVal << UserProperty<T>::ConstData();
270 OnChange(login, name, oldVal.str(), newVal.str(), admin);
273 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
275 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
277 UserProperty<T>::Set(val);
281 WriteAccessDenied(login, admin, name);
284 //-------------------------------------------------------------------------
285 template <typename T>
287 void UserPropertyLogged<T>::WriteAccessDenied(const std::string& login,
289 const std::string& parameter)
291 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
292 admin.logStr().c_str(), login.c_str(), parameter.c_str());
294 //-------------------------------------------------------------------------
295 template <typename T>
297 void UserPropertyLogged<T>::WriteSuccessChange(const std::string& login,
299 const std::string& parameter,
300 const std::string& oldValue,
301 const std::string& newValue,
302 const std::string& msg,
305 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
306 admin.logStr().c_str(),
313 for (size_t i = 0; i < settings.GetFilterParamsLog().size(); ++i)
314 if (settings.GetFilterParamsLog()[i] == "*" || strcasecmp(settings.GetFilterParamsLog()[i].c_str(), parameter.c_str()) == 0)
316 store.WriteUserChgLog(login, admin.login(), admin.IP(), parameter, oldValue, newValue, msg);
320 //-------------------------------------------------------------------------
321 template <typename T>
322 void UserPropertyLogged<T>::OnChange(const std::string& login,
323 const std::string& paramName,
324 const std::string& oldValue,
325 const std::string& newValue,
328 const auto filePath = settings.GetScriptsDir() + "/OnChange";
330 if (access(filePath.c_str(), X_OK) == 0)
332 const auto execString = "\"" + filePath + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin.login() + "\" \"" + admin.IPStr() + "\"";
333 ScriptExec(execString.c_str());
336 stgLogger("Script OnChange cannot be executed. File %s not found.", filePath.c_str());
338 //-------------------------------------------------------------------------
339 //-------------------------------------------------------------------------
340 //-------------------------------------------------------------------------
342 std::string UserProperties::GetPropertyValue(const std::string& name) const
344 const auto it = properties.find(ToLower(name));
345 if (it == properties.end())
347 return it->second->ToString();
349 //-----------------------------------------------------------------------------
351 bool UserProperties::Exists(const std::string& name) const
353 return properties.find(ToLower(name)) != properties.end();
355 //-------------------------------------------------------------------------
356 //-------------------------------------------------------------------------
357 //-------------------------------------------------------------------------
360 std::ostream& operator<<(std::ostream& stream, const UserProperty<T>& value)
362 return stream << value.ConstData();
364 //-----------------------------------------------------------------------------
367 std::string UserProperty<T>::ToString() const
369 std::ostringstream stream;