X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/e9ae1f101b5418c0ba2e6c9d86b23c12f0140982..b2b89723a2427bba8290bd6967a1ab39cbb630be:/include/stg/user_property.h diff --git a/include/stg/user_property.h b/include/stg/user_property.h index 9ce57c37..d73a636b 100644 --- a/include/stg/user_property.h +++ b/include/stg/user_property.h @@ -4,7 +4,7 @@ #include "stg/user_ips.h" #include "stg/store.h" #include "stg/admin.h" -#include "stg/notifer.h" +#include "stg/subscriptions.h" #include "stg/admin_conf.h" #include "stg/logger.h" #include "stg/locker.h" @@ -22,12 +22,11 @@ #include // access -extern volatile time_t stgTime; - namespace STG { //----------------------------------------------------------------------------- -struct UserPropertyBase { +struct UserPropertyBase +{ virtual ~UserPropertyBase() = default; virtual std::string ToString() const = 0; }; @@ -35,7 +34,8 @@ struct UserPropertyBase { using Registry = std::map; //----------------------------------------------------------------------------- template -class UserProperty : public UserPropertyBase { +class UserProperty : public UserPropertyBase +{ public: explicit UserProperty(T& val); @@ -49,11 +49,10 @@ class UserProperty : public UserPropertyBase { operator const T&() const noexcept { return value; } - void AddBeforeNotifier(PropertyNotifierBase* n); - void DelBeforeNotifier(const PropertyNotifierBase* n); - - void AddAfterNotifier(PropertyNotifierBase* n); - void DelAfterNotifier(const PropertyNotifierBase* n); + template + auto beforeChange(F&& f) { return m_beforeCallbacks.add(std::forward(f)); } + template + auto afterChange(F&& f) { return m_afterCallbacks.add(std::forward(f)); } time_t ModificationTime() const noexcept { return modificationTime; } void ModifyTime() noexcept; @@ -62,13 +61,14 @@ class UserProperty : public UserPropertyBase { private: T& value; time_t modificationTime; - std::set*> beforeNotifiers; - std::set*> afterNotifiers; + Subscriptions m_beforeCallbacks; + Subscriptions m_afterCallbacks; std::mutex mutex; }; //----------------------------------------------------------------------------- template -class UserPropertyLogged: public UserProperty { +class UserPropertyLogged: public UserProperty +{ public: UserPropertyLogged(T& val, const std::string& n, @@ -112,7 +112,8 @@ class UserPropertyLogged: public UserProperty { const Settings& settings; }; //----------------------------------------------------------------------------- -class UserProperties { +class UserProperties +{ /* В этом месте важен порядок следования приватной и открытой частей. Это связано с тем, что часть которая находится в публичной секции @@ -186,9 +187,7 @@ template inline UserProperty::UserProperty(T& val) : value(val), - modificationTime(stgTime), - beforeNotifiers(), - afterNotifiers() + modificationTime(time(NULL)) { } //----------------------------------------------------------------------------- @@ -196,27 +195,23 @@ template inline void UserProperty::ModifyTime() noexcept { - modificationTime = stgTime; + modificationTime = time(NULL); } //----------------------------------------------------------------------------- template inline -void UserProperty::Set(const T& rvalue) +void UserProperty::Set(const T& rhs) { std::lock_guard lock(mutex); T oldVal = value; - auto ni = beforeNotifiers.begin(); - while (ni != beforeNotifiers.end()) - (*ni++)->Notify(oldVal, rvalue); + m_beforeCallbacks.notify(oldVal, rhs); - value = rvalue; - modificationTime = stgTime; + value = rhs; + modificationTime = time(NULL); - ni = afterNotifiers.begin(); - while (ni != afterNotifiers.end()) - (*ni++)->Notify(oldVal, rvalue); + m_afterCallbacks.notify(oldVal, rhs); } //----------------------------------------------------------------------------- template @@ -227,38 +222,6 @@ UserProperty& UserProperty::operator=(const T& newValue) return *this; } //----------------------------------------------------------------------------- -template -inline -void UserProperty::AddBeforeNotifier(PropertyNotifierBase* n) -{ - std::lock_guard lock(mutex); - beforeNotifiers.insert(n); -} -//----------------------------------------------------------------------------- -template -inline -void UserProperty::DelBeforeNotifier(const PropertyNotifierBase* n) -{ - std::lock_guard lock(mutex); - beforeNotifiers.erase(const_cast*>(n)); -} -//----------------------------------------------------------------------------- -template -inline -void UserProperty::AddAfterNotifier(PropertyNotifierBase* n) -{ - std::lock_guard lock(mutex); - afterNotifiers.insert(n); -} -//----------------------------------------------------------------------------- -template -inline -void UserProperty::DelAfterNotifier(const PropertyNotifierBase* n) -{ - std::lock_guard lock(mutex); - afterNotifiers.erase(const_cast*>(n)); -} -//----------------------------------------------------------------------------- //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- template @@ -288,12 +251,12 @@ bool UserPropertyLogged::Set(const T& val, const Store& store, const std::string& msg) { - const auto priv = admin.GetPriv(); + const auto& priv = admin.priv(); - if ((priv->userConf && !isStat) || - (priv->userStat && isStat) || - (priv->userPasswd && isPassword) || - (priv->userCash && name == "cash")) + if ((priv.userConf && !isStat) || + (priv.userStat && isStat) || + (priv.userPasswd && isPassword) || + (priv.userCash && name == "cash")) { std::stringstream oldVal; std::stringstream newVal; @@ -326,7 +289,7 @@ void UserPropertyLogged::WriteAccessDenied(const std::string& login, const std::string& parameter) { stgLogger("%s Change user \'%s.\' Parameter \'%s\'. Access denied.", - admin.GetLogStr().c_str(), login.c_str(), parameter.c_str()); + admin.logStr().c_str(), login.c_str(), parameter.c_str()); } //------------------------------------------------------------------------- template @@ -340,7 +303,7 @@ void UserPropertyLogged::WriteSuccessChange(const std::string& login, const Store& store) { stgLogger("%s User \'%s\': \'%s\' parameter changed from \'%s\' to \'%s\'. %s", - admin.GetLogStr().c_str(), + admin.logStr().c_str(), login.c_str(), parameter.c_str(), oldValue.c_str(), @@ -350,7 +313,7 @@ void UserPropertyLogged::WriteSuccessChange(const std::string& login, for (size_t i = 0; i < settings.GetFilterParamsLog().size(); ++i) if (settings.GetFilterParamsLog()[i] == "*" || strcasecmp(settings.GetFilterParamsLog()[i].c_str(), parameter.c_str()) == 0) { - store.WriteUserChgLog(login, admin.GetLogin(), admin.GetIP(), parameter, oldValue, newValue, msg); + store.WriteUserChgLog(login, admin.login(), admin.IP(), parameter, oldValue, newValue, msg); return; } } @@ -366,7 +329,7 @@ void UserPropertyLogged::OnChange(const std::string& login, if (access(filePath.c_str(), X_OK) == 0) { - const auto execString = "\"" + filePath + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin.GetLogin() + "\" \"" + admin.GetIPStr() + "\""; + const auto execString = "\"" + filePath + "\" \"" + login + "\" \"" + paramName + "\" \"" + oldValue + "\" \"" + newValue + "\" \"" + admin.login() + "\" \"" + admin.IPStr() + "\""; ScriptExec(execString.c_str()); } else