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/settings.h"
11 #include "stg/scriptexecuter.h"
12 #include "stg/common.h"
22 #include <unistd.h> // access
26 //-----------------------------------------------------------------------------
27 struct UserPropertyBase
29 virtual ~UserPropertyBase() = default;
30 virtual std::string ToString() const = 0;
32 //-----------------------------------------------------------------------------
33 using Registry = std::map<std::string, UserPropertyBase*>;
34 //-----------------------------------------------------------------------------
36 class UserProperty : public UserPropertyBase
39 explicit UserProperty(T& val);
41 void Set(const T& rhs);
42 T get() const { return value; }
44 UserProperty<T>& operator=(const T& rhs);
46 const T* operator&() const noexcept { return &value; }
47 const T& ConstData() const noexcept { return value; }
49 operator const T&() const noexcept { return value; }
52 auto beforeChange(F&& f) { return m_beforeCallbacks.add(std::forward<F>(f)); }
54 auto afterChange(F&& f) { return m_afterCallbacks.add(std::forward<F>(f)); }
56 time_t ModificationTime() const noexcept { return modificationTime; }
57 void ModifyTime() noexcept;
59 std::string ToString() const override;
62 time_t modificationTime;
63 Subscriptions<T, T> m_beforeCallbacks;
64 Subscriptions<T, T> m_afterCallbacks;
67 //-----------------------------------------------------------------------------
69 class UserPropertyLogged: public UserProperty<T>
72 UserPropertyLogged(T& val,
77 Registry& properties);
79 UserPropertyLogged<T>* GetPointer() noexcept { return this; }
80 const UserPropertyLogged<T>* GetPointer() const noexcept { return this; }
81 const T& Get() const { return UserProperty<T>::ConstData(); }
82 const std::string& GetName() const { return name; }
83 bool Set(const T& val,
85 const std::string& login,
87 const std::string& msg = "");
89 void WriteAccessDenied(const std::string& login,
91 const std::string& parameter);
93 void WriteSuccessChange(const std::string& login,
95 const std::string& parameter,
96 const std::string& oldValue,
97 const std::string& newValue,
98 const std::string& msg,
101 void OnChange(const std::string& login,
102 const std::string& paramName,
103 const std::string& oldValue,
104 const std::string& newValue,
111 const Settings& settings;
113 //-----------------------------------------------------------------------------
117 В этом месте важен порядок следования приватной и открытой частей.
118 Это связано с тем, что часть которая находится в публичной секции
119 по сути является завуалированной ссылкой на закрытую часть. Т.о. нам нужно
120 чтобы конструкторы из закрытой части вызвались раньше открытой. Поэтомому в
121 начале идет закрытая секция
130 explicit UserProperties(const Settings& s);
132 UserStat& Stat() { return stat; }
133 UserConf& Conf() { return conf; }
134 const UserStat& GetStat() const { return stat; }
135 const UserConf& GetConf() const { return conf; }
136 void SetStat(const UserStat& s) { stat = s; }
137 void SetConf(const UserConf& c) { conf = c; }
139 void SetProperties(const UserProperties& 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 UserPropertyLogged<double> cash;
145 UserPropertyLogged<DirTraff> up;
146 UserPropertyLogged<DirTraff> down;
147 UserPropertyLogged<double> lastCashAdd;
148 UserPropertyLogged<time_t> passiveTime;
149 UserPropertyLogged<time_t> lastCashAddTime;
150 UserPropertyLogged<double> freeMb;
151 UserPropertyLogged<time_t> lastActivityTime;
153 UserPropertyLogged<std::string> password;
154 UserPropertyLogged<int> passive;
155 UserPropertyLogged<int> disabled;
156 UserPropertyLogged<int> disabledDetailStat;
157 UserPropertyLogged<int> alwaysOnline;
158 UserPropertyLogged<std::string> tariffName;
159 UserPropertyLogged<std::string> nextTariff;
160 UserPropertyLogged<std::string> address;
161 UserPropertyLogged<std::string> note;
162 UserPropertyLogged<std::string> group;
163 UserPropertyLogged<std::string> email;
164 UserPropertyLogged<std::string> phone;
165 UserPropertyLogged<std::string> realName;
166 UserPropertyLogged<double> credit;
167 UserPropertyLogged<time_t> creditExpire;
168 UserPropertyLogged<UserIPs> ips;
169 UserPropertyLogged<std::string> userdata0;
170 UserPropertyLogged<std::string> userdata1;
171 UserPropertyLogged<std::string> userdata2;
172 UserPropertyLogged<std::string> userdata3;
173 UserPropertyLogged<std::string> userdata4;
174 UserPropertyLogged<std::string> userdata5;
175 UserPropertyLogged<std::string> userdata6;
176 UserPropertyLogged<std::string> userdata7;
177 UserPropertyLogged<std::string> userdata8;
178 UserPropertyLogged<std::string> userdata9;
180 //=============================================================================
182 //-----------------------------------------------------------------------------
183 //-----------------------------------------------------------------------------
184 //-----------------------------------------------------------------------------
185 template <typename T>
187 UserProperty<T>::UserProperty(T& val)
189 modificationTime(time(NULL))
192 //-----------------------------------------------------------------------------
193 template <typename T>
195 void UserProperty<T>::ModifyTime() noexcept
197 modificationTime = time(NULL);
199 //-----------------------------------------------------------------------------
200 template <typename T>
202 void UserProperty<T>::Set(const T& rhs)
204 std::lock_guard<std::mutex> lock(mutex);
208 m_beforeCallbacks.notify(oldVal, rhs);
211 modificationTime = time(NULL);
213 m_afterCallbacks.notify(oldVal, rhs);
215 //-----------------------------------------------------------------------------
216 template <typename T>
218 UserProperty<T>& UserProperty<T>::operator=(const T& newValue)
223 //-----------------------------------------------------------------------------
224 //-----------------------------------------------------------------------------
225 //-----------------------------------------------------------------------------
226 template <typename T>
228 UserPropertyLogged<T>::UserPropertyLogged(T& val,
229 const std::string& n,
233 Registry& properties)
235 : UserProperty<T>(val),
236 stgLogger(Logger::get()),
242 properties.insert(std::make_pair(ToLower(name), this));
244 //-------------------------------------------------------------------------
245 template <typename T>
247 bool UserPropertyLogged<T>::Set(const T& val,
249 const std::string& login,
251 const std::string& msg)
253 const auto& priv = admin.priv();
255 if ((priv.userConf && !isStat) ||
256 (priv.userStat && isStat) ||
257 (priv.userPasswd && isPassword) ||
258 (priv.userCash && name == "cash"))
260 std::stringstream oldVal;
261 std::stringstream newVal;
263 oldVal.flags(oldVal.flags() | std::ios::fixed);
264 newVal.flags(newVal.flags() | std::ios::fixed);
266 oldVal << UserProperty<T>::ConstData();
269 OnChange(login, name, oldVal.str(), newVal.str(), admin);
272 WriteSuccessChange(login, admin, name, "******", "******", msg, store);
274 WriteSuccessChange(login, admin, name, oldVal.str(), newVal.str(), msg, store);
276 UserProperty<T>::Set(val);
280 WriteAccessDenied(login, admin, name);
283 //-------------------------------------------------------------------------
284 template <typename T>
286 void UserPropertyLogged<T>::WriteAccessDenied(const std::string& login,
288 const std::string& parameter)
290 stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.",
291 admin.logStr().c_str(), login.c_str(), parameter.c_str());
293 //-------------------------------------------------------------------------
294 template <typename T>
296 void UserPropertyLogged<T>::WriteSuccessChange(const std::string& login,
298 const std::string& parameter,
299 const std::string& oldValue,
300 const std::string& newValue,
301 const std::string& msg,
304 stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s",
305 admin.logStr().c_str(),
312 for (size_t i = 0; i < settings.GetFilterParamsLog().size(); ++i)
313 if (settings.GetFilterParamsLog()[i] == "*" || strcasecmp(settings.GetFilterParamsLog()[i].c_str(), parameter.c_str()) == 0)
315 store.WriteUserChgLog(login, admin.login(), admin.IP(), parameter, oldValue, newValue, msg);
319 //-------------------------------------------------------------------------
320 template <typename T>
321 void UserPropertyLogged<T>::OnChange(const std::string& login,
322 const std::string& paramName,
323 const std::string& oldValue,
324 const std::string& newValue,
327 const auto filePath = settings.GetScriptsDir() + "/OnChange";
329 if (access(filePath.c_str(), X_OK) == 0)
331 const auto execString = "\"" + filePath + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin.login() + "\" \"" + admin.IPStr() + "\"";
332 ScriptExec(execString.c_str());
335 stgLogger("Script OnChange cannot be executed. File %s not found.", filePath.c_str());
337 //-------------------------------------------------------------------------
338 //-------------------------------------------------------------------------
339 //-------------------------------------------------------------------------
341 std::string UserProperties::GetPropertyValue(const std::string& name) const
343 const auto it = properties.find(ToLower(name));
344 if (it == properties.end())
346 return it->second->ToString();
348 //-----------------------------------------------------------------------------
350 bool UserProperties::Exists(const std::string& name) const
352 return properties.find(ToLower(name)) != properties.end();
354 //-------------------------------------------------------------------------
355 //-------------------------------------------------------------------------
356 //-------------------------------------------------------------------------
359 std::ostream& operator<<(std::ostream& stream, const UserProperty<T>& value)
361 return stream << value.ConstData();
363 //-----------------------------------------------------------------------------
366 std::string UserProperty<T>::ToString() const
368 std::ostringstream stream;