From: Maksym Mamontov Date: Mon, 25 May 2020 15:57:48 +0000 (+0300) Subject: Non-virtual admin. X-Git-Url: https://git.stg.codes/stg.git/commitdiff_plain/20d4dd0571e75d254444acf755a01b48a691c7c2?ds=sidebyside;hp=6e6de2ae8f5f661500c565aca8c36277e860b834 Non-virtual admin. --- diff --git a/include/stg/admin.h b/include/stg/admin.h index f78640ae..7f302bb4 100644 --- a/include/stg/admin.h +++ b/include/stg/admin.h @@ -20,27 +20,50 @@ #pragma once +#include "admin_conf.h" + +#include "stg/common.h" + #include #include namespace STG { -struct AdminConf; -struct Priv; - -struct Admin { - virtual ~Admin() = default; - - virtual const std::string& GetPassword() const = 0; - virtual const std::string& GetLogin() const = 0; - virtual const Priv* GetPriv() const = 0; - virtual uint32_t GetPrivAsInt() const = 0; - virtual const AdminConf& GetConf() const = 0; - virtual uint32_t GetIP() const = 0; - virtual std::string GetIPStr() const = 0; - virtual void SetIP(uint32_t ip) = 0; - virtual const std::string GetLogStr() const = 0; +class Admin +{ + public: + Admin() noexcept : Admin(AdminConf{}) {} + Admin(const Priv& priv, + const std::string& login, + const std::string& password) noexcept + : Admin(AdminConf{priv, login, password}) + {} + explicit Admin(const AdminConf& ac) noexcept : m_conf(ac), m_ip(0) {} + + Admin(const Admin&) = default; + Admin& operator=(const Admin&) = default; + Admin(Admin&&) = default; + Admin& operator=(Admin&&) = default; + + Admin& operator=(const AdminConf& ac) noexcept { m_conf = ac; return *this; } + bool operator==(const Admin& rhs) const noexcept { return m_conf.login == rhs.m_conf.login; } + bool operator!=(const Admin& rhs) const noexcept { return !(*this == rhs); } + bool operator<(const Admin& rhs) const noexcept { return m_conf.login < rhs.m_conf.login; } + + const std::string& password() const { return m_conf.password; } + const std::string& login() const { return m_conf.login; } + const Priv& priv() const { return m_conf.priv; } + uint32_t privAsInt() const { return m_conf.priv.toInt(); } + const AdminConf& conf() const { return m_conf; } + uint32_t IP() const { return m_ip; } + std::string IPStr() const { return inet_ntostring(m_ip); } + void setIP(uint32_t v) { m_ip = v; } + const std::string logStr() const { return "Admin \'" + m_conf.login + "\', " + IPStr() + ":"; } + + private: + AdminConf m_conf; + uint32_t m_ip; }; } diff --git a/include/stg/admin_conf.h b/include/stg/admin_conf.h index c4871970..8531647e 100644 --- a/include/stg/admin_conf.h +++ b/include/stg/admin_conf.h @@ -14,17 +14,7 @@ namespace STG struct Priv { - Priv() noexcept - : userStat(0), - userConf(0), - userCash(0), - userPasswd(0), - userAddDel(0), - adminChg(0), - tariffChg(0), - serviceChg(0), - corpChg(0) - {} + Priv() noexcept : Priv(0) {} explicit Priv(uint32_t p) noexcept : userStat((p & 0x00000003) >> 0x00), userConf((p & 0x0000000C) >> 0x02), @@ -69,10 +59,8 @@ struct Priv //----------------------------------------------------------------------------- struct AdminConf { - AdminConf() - : password("* NO PASSWORD *") - {} - AdminConf(const Priv & pr, const std::string & l, const std::string & p) + AdminConf() : AdminConf({}, {}, "* NO PASSWORD *") {} + AdminConf(const Priv& pr, const std::string& l, const std::string& p) : priv(pr), login(l), password(p) diff --git a/include/stg/admins.h b/include/stg/admins.h index 3945aac4..e70b1fe7 100644 --- a/include/stg/admins.h +++ b/include/stg/admins.h @@ -21,32 +21,31 @@ #pragma once #include +#include namespace STG { struct AdminConf; -struct Admin; +class Admin; -struct Admins { +struct Admins +{ virtual ~Admins() = default; - virtual int Add(const std::string& login, const Admin* admin) = 0; - virtual int Del(const std::string& login, const Admin* admin) = 0; - virtual int Change(const AdminConf& ac, const Admin* admin) = 0; - virtual const Admin* GetSysAdmin() const = 0; - virtual const Admin* GetNoAdmin() const = 0; - virtual bool Find(const std::string& l, Admin** admin) = 0; - virtual bool Exists(const std::string& login) const = 0; - virtual bool Correct(const std::string& login, + virtual int add(const std::string& login, const Admin& admin) = 0; + virtual int del(const std::string& login, const Admin& admin) = 0; + virtual int change(const AdminConf& ac, const Admin& admin) = 0; + virtual const Admin& sysAdmin() const = 0; + virtual const Admin& noAdmin() const = 0; + virtual bool find(const std::string& login, Admin** admin) = 0; + virtual bool exists(const std::string& login) const = 0; + virtual bool correct(const std::string& login, const std::string& password, Admin** admin) = 0; - virtual const std::string& GetStrError() const = 0; - virtual size_t Count() const = 0; - - virtual int OpenSearch() const = 0; - virtual int SearchNext(int, AdminConf* ac) const = 0; - virtual int CloseSearch(int) const = 0; + virtual const std::string& strError() const = 0; + virtual size_t count() const = 0; + virtual void fmap(std::function callback) const = 0; }; } diff --git a/include/stg/user_property.h b/include/stg/user_property.h index 9ce57c37..7c65f962 100644 --- a/include/stg/user_property.h +++ b/include/stg/user_property.h @@ -288,12 +288,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 +326,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 +340,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 +350,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 +366,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 diff --git a/stargazer/CMakeLists.txt b/stargazer/CMakeLists.txt index d64efc6c..073c7165 100644 --- a/stargazer/CMakeLists.txt +++ b/stargazer/CMakeLists.txt @@ -4,7 +4,6 @@ set ( CPP_FILES main.cpp tariffs_impl.cpp corps_impl.cpp services_impl.cpp - admin_impl.cpp user_impl.cpp tariff_impl.cpp eventloop.cpp diff --git a/stargazer/admin_impl.cpp b/stargazer/admin_impl.cpp deleted file mode 100644 index 86764e08..00000000 --- a/stargazer/admin_impl.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Date: 27.10.2002 - */ - -/* - * Author : Boris Mikhailenko - */ - - /* - $Revision: 1.13 $ - $Date: 2010/10/04 20:16:09 $ - $Author: faust $ - */ - -#include "admin_impl.h" - -#include "stg/common.h" - -using STG::AdminImpl; - -//----------------------------------------------------------------------------- -std::string AdminImpl::GetIPStr() const -{ - return inet_ntostring(ip); -} -//----------------------------------------------------------------------------- -void AdminImpl::Print() const -{ - printfd(__FILE__, "=======================================\n"); - printfd(__FILE__, "login %s\n", conf.login.c_str()); - printfd(__FILE__, "password %s\n", conf.password.c_str()); - printfd(__FILE__, "ChgConf %d\n", conf.priv.userConf); - printfd(__FILE__, "ChgStat %d\n", conf.priv.userStat); - printfd(__FILE__, "ChgCash %d\n", conf.priv.userCash); - printfd(__FILE__, "UsrAddDel %d\n", conf.priv.userAddDel); - printfd(__FILE__, "ChgAdmin %d\n", conf.priv.adminChg); - printfd(__FILE__, "ChgTariff %d\n", conf.priv.tariffChg); - printfd(__FILE__, "=======================================\n"); -} -//----------------------------------------------------------------------------- -const std::string AdminImpl::GetLogStr() const -{ - return "Admin \'" + conf.login + "\', " + GetIPStr() + ":"; -} -//----------------------------------------------------------------------------- diff --git a/stargazer/admin_impl.h b/stargazer/admin_impl.h deleted file mode 100644 index 34431b01..00000000 --- a/stargazer/admin_impl.h +++ /dev/null @@ -1,74 +0,0 @@ -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ - -/* - * Date: 27.10.2002 - */ - -/* - * Author : Boris Mikhailenko - */ - -#pragma once - -#include "stg/admin.h" -#include "stg/admin_conf.h" - -#include -#include - -namespace STG -{ - -class AdminImpl : public Admin { - public: - AdminImpl() noexcept : ip(0) {} - - explicit AdminImpl(const AdminConf& ac) noexcept : conf(ac), ip(0) {} - AdminImpl(const Priv& priv, - const std::string& login, - const std::string& password) noexcept - : conf(priv, login, password), ip(0) - {} - - AdminImpl(const AdminImpl&) = default; - AdminImpl& operator=(const AdminImpl&) = default; - AdminImpl(AdminImpl&&) = default; - AdminImpl& operator=(AdminImpl&&) = default; - - AdminImpl& operator=(const AdminConf& ac) noexcept { conf = ac; return *this; } - bool operator==(const AdminImpl& rhs) const noexcept { return conf.login == rhs.conf.login; } - bool operator!=(const AdminImpl& rhs) const noexcept { return !(*this == rhs); } - bool operator<(const AdminImpl& rhs) const noexcept { return conf.login < rhs.conf.login; } - //bool operator<=(const AdminImpl & rhs) const; - - const std::string& GetPassword() const override { return conf.password; } - const std::string& GetLogin() const override { return conf.login; } - const Priv* GetPriv() const override { return &conf.priv; } - uint32_t GetPrivAsInt() const override { return conf.priv.toInt(); } - const AdminConf& GetConf() const override { return conf; } - void Print() const; - uint32_t GetIP() const override { return ip; } - std::string GetIPStr() const override; - void SetIP(uint32_t v) override { ip = v; } - const std::string GetLogStr() const override; - - private: - AdminConf conf; - uint32_t ip; -}; - -} diff --git a/stargazer/admins_impl.cpp b/stargazer/admins_impl.cpp index 4cb2095b..1e597367 100644 --- a/stargazer/admins_impl.cpp +++ b/stargazer/admins_impl.cpp @@ -22,286 +22,202 @@ * Author : Boris Mikhailenko */ - /* - $Revision: 1.15 $ - $Date: 2010/10/04 20:17:12 $ - $Author: faust $ - */ - #include "admins_impl.h" -#include "admin_impl.h" #include "stg/common.h" -#include -#include -#include - using STG::AdminsImpl; //----------------------------------------------------------------------------- -AdminsImpl::AdminsImpl(Store * st) - : stg(Priv(0xFFFF), "@stargazer", ""), - noAdmin(Priv(0xFFFF), "NO-ADMIN", ""), - data(), - store(st), - WriteServLog(Logger::get()), - searchDescriptors(), - handle(0), - mutex(), - strError() +AdminsImpl::AdminsImpl(Store& st) + : m_stg(Priv(0xFFFF), "@stargazer", ""), + m_noAdmin(Priv(0xFFFF), "NO-ADMIN", ""), + m_store(st), + WriteServLog(Logger::get()) { -pthread_mutex_init(&mutex, NULL); -Read(); + read(); } //----------------------------------------------------------------------------- -int AdminsImpl::Add(const std::string & login, const Admin * admin) +int AdminsImpl::add(const std::string& login, const Admin& admin) { -STG_LOCKER lock(&mutex); -const Priv * priv = admin->GetPriv(); - -if (!priv->adminChg) + if (!admin.priv().adminChg) { - std::string s = admin->GetLogStr() + " Add administrator \'" + login + "\'. Access denied."; - strError = "Access denied."; - WriteServLog(s.c_str()); - return -1; + const std::string s = admin.logStr() + " Add administrator \'" + login + "\'. Access denied."; + m_strError = "Access denied."; + WriteServLog(s.c_str()); + return -1; } -AdminImpl adm(Priv(0), login, ""); -admin_iter ai(find(data.begin(), data.end(), adm)); + std::lock_guard lock(m_mutex); + const auto it = find(login); -if (ai != data.end()) + if (it != m_data.end()) { - strError = "Administrator \'" + login + "\' cannot not be added. Administrator already exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); - - return -1; + m_strError = "Administrator \'" + login + "\' cannot not be added. Administrator already exists."; + WriteServLog("%s %s", admin.logStr().c_str(), m_strError.c_str()); + return -1; } -data.push_back(adm); + m_data.push_back(Admin(Priv(0), login, {})); -if (store->AddAdmin(login) == 0) + if (m_store.AddAdmin(login) == 0) { - WriteServLog("%s Administrator \'%s\' added.", - admin->GetLogStr().c_str(), login.c_str()); - return 0; + WriteServLog("%s Administrator \'%s\' added.", + admin.logStr().c_str(), login.c_str()); + return 0; } -strError = "Administrator \'" + login + "\' was not added. Error: " + store->GetStrError(); -WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + m_strError = "Administrator \'" + login + "\' was not added. Error: " + m_store.GetStrError(); + WriteServLog("%s %s", admin.logStr().c_str(), m_strError.c_str()); -return -1; + return -1; } //----------------------------------------------------------------------------- -int AdminsImpl::Del(const std::string & login, const Admin * admin) +int AdminsImpl::del(const std::string& login, const Admin& admin) { -STG_LOCKER lock(&mutex); -const Priv * priv = admin->GetPriv(); - -if (!priv->adminChg) + if (!admin.priv().adminChg) { - std::string s = admin->GetLogStr() + " Delete administrator \'" + login + "\'. Access denied."; - strError = "Access denied."; - WriteServLog(s.c_str()); - return -1; + const std::string s = admin.logStr() + " Delete administrator \'" + login + "\'. Access denied."; + m_strError = "Access denied."; + WriteServLog(s.c_str()); + return -1; } -admin_iter ai(find(data.begin(), data.end(), AdminImpl(Priv(0), login, ""))); + std::lock_guard lock(m_mutex); + const auto it = find(login); -if (ai == data.end()) + if (it == m_data.end()) { - strError = "Administrator \'" + login + "\' cannot be deleted. Administrator does not exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); - return -1; - } - -std::map::iterator si; -si = searchDescriptors.begin(); -while (si != searchDescriptors.end()) - { - if (si->second == ai) - (si->second)++; - ++si; + m_strError = "Administrator \'" + login + "\' cannot be deleted. Administrator does not exist."; + WriteServLog("%s %s", admin.logStr().c_str(), m_strError.c_str()); + return -1; } -data.erase(ai); -if (store->DelAdmin(login) < 0) + m_data.erase(it); + if (m_store.DelAdmin(login) < 0) { - strError = "Administrator \'" + login + "\' was not deleted. Error: " + store->GetStrError(); - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + m_strError = "Administrator \'" + login + "\' was not deleted. Error: " + m_store.GetStrError(); + WriteServLog("%s %s", admin.logStr().c_str(), m_strError.c_str()); - return -1; + return -1; } -WriteServLog("%s Administrator \'%s\' deleted.", admin->GetLogStr().c_str(), login.c_str()); -return 0; + WriteServLog("%s Administrator \'%s\' deleted.", admin.logStr().c_str(), login.c_str()); + return 0; } //----------------------------------------------------------------------------- -int AdminsImpl::Change(const AdminConf & ac, const Admin * admin) +int AdminsImpl::change(const AdminConf& ac, const Admin& admin) { -STG_LOCKER lock(&mutex); -const Priv * priv = admin->GetPriv(); - -if (!priv->adminChg) + if (!admin.priv().adminChg) { - std::string s = admin->GetLogStr() + " Change administrator \'" + ac.login + "\'. Access denied."; - strError = "Access denied."; - WriteServLog(s.c_str()); - return -1; + const std::string s = admin.logStr() + " Change administrator \'" + ac.login + "\'. Access denied."; + m_strError = "Access denied."; + WriteServLog(s.c_str()); + return -1; } -admin_iter ai(find(data.begin(), data.end(), AdminImpl(Priv(0), ac.login, ""))); + std::lock_guard lock(m_mutex); + const auto it = find(ac.login); -if (ai == data.end()) + if (it == m_data.end()) { - strError = "Administrator \'" + ac.login + "\' cannot be changed " + ". Administrator does not exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); - return -1; + m_strError = "Administrator \'" + ac.login + "\' cannot be changed " + ". Administrator does not exist."; + WriteServLog("%s %s", admin.logStr().c_str(), m_strError.c_str()); + return -1; } -*ai = ac; -if (store->SaveAdmin(ac)) + *it = ac; + if (m_store.SaveAdmin(ac)) { - WriteServLog("Cannot write admin %s.", ac.login.c_str()); - WriteServLog("%s", store->GetStrError().c_str()); - return -1; + WriteServLog("Cannot write admin %s.", ac.login.c_str()); + WriteServLog("%s", m_store.GetStrError().c_str()); + return -1; } -WriteServLog("%s Administrator \'%s\' changed.", - admin->GetLogStr().c_str(), ac.login.c_str()); + WriteServLog("%s Administrator \'%s\' changed.", + admin.logStr().c_str(), ac.login.c_str()); -return 0; + return 0; } //----------------------------------------------------------------------------- -int AdminsImpl::Read() +void AdminsImpl::read() { -STG_LOCKER lock(&mutex); -std::vector adminsList; -if (store->GetAdminsList(&adminsList) < 0) + std::vector logins; + if (m_store.GetAdminsList(&logins) < 0) { - WriteServLog(store->GetStrError().c_str()); - return -1; + WriteServLog(m_store.GetStrError().c_str()); + return; } -for (unsigned int i = 0; i < adminsList.size(); i++) + std::vector admins; + for (const auto& login : logins) { - AdminConf ac(Priv(0), adminsList[i], ""); + AdminConf ac(Priv(0), login, ""); - if (store->RestoreAdmin(&ac, adminsList[i])) + if (m_store.RestoreAdmin(&ac, login)) { - WriteServLog(store->GetStrError().c_str()); - return -1; + WriteServLog(m_store.GetStrError().c_str()); + return; } - data.push_back(AdminImpl(ac)); + m_data.push_back(Admin(ac)); } -return 0; + + std::lock_guard lock(m_mutex); + m_data.swap(admins); } //----------------------------------------------------------------------------- -bool AdminsImpl::Find(const std::string & l, Admin ** admin) +bool AdminsImpl::find(const std::string& login, Admin** admin) { -assert(admin != NULL && "Pointer to admin is not null"); - -STG_LOCKER lock(&mutex); -if (data.empty()) + std::lock_guard lock(m_mutex); + if (m_data.empty()) { - printfd(__FILE__, "No admin in system!\n"); - *admin = &noAdmin; - return false; + printfd(__FILE__, "No admin in system!\n"); + if (admin != nullptr) + *admin = &m_noAdmin; + return false; } -admin_iter ai(find(data.begin(), data.end(), AdminImpl(Priv(0), l, ""))); + auto it = find(login); -if (ai != data.end()) + if (it != m_data.end()) { - *admin = &(*ai); - return false; + if (admin != nullptr) + *admin = &(*it); + return false; } -return true; -} -//----------------------------------------------------------------------------- -bool AdminsImpl::Exists(const std::string & login) const -{ -STG_LOCKER lock(&mutex); -if (data.empty()) - { - printfd(__FILE__, "no admin in system!\n"); - return true; - } - -const_admin_iter ai(find(data.begin(), data.end(), AdminImpl(Priv(0), login, ""))); - -if (ai != data.end()) return true; - -return false; } //----------------------------------------------------------------------------- -bool AdminsImpl::Correct(const std::string & login, const std::string & password, Admin ** admin) +bool AdminsImpl::exists(const std::string& login) const { -STG_LOCKER lock(&mutex); -if (data.empty()) - { - printfd(__FILE__, "no admin in system!\n"); - return true; - } - -admin_iter ai(find(data.begin(), data.end(), AdminImpl(Priv(0), login, ""))); - -if (ai == data.end()) + std::lock_guard lock(m_mutex); + if (m_data.empty()) { - return false; + printfd(__FILE__, "No admin in system!\n"); + return true; } -if (ai->GetPassword() != password) - { - return false; - } - -*admin = &(*ai); - -return true; -} -//----------------------------------------------------------------------------- -int AdminsImpl::OpenSearch() const -{ -STG_LOCKER lock(&mutex); -handle++; -searchDescriptors[handle] = data.begin(); -return handle; + return find(login) != m_data.end(); } //----------------------------------------------------------------------------- -int AdminsImpl::SearchNext(int h, AdminConf * ac) const +bool AdminsImpl::correct(const std::string& login, const std::string& password, Admin** admin) { -STG_LOCKER lock(&mutex); -if (searchDescriptors.find(h) == searchDescriptors.end()) + std::lock_guard lock(m_mutex); + if (m_data.empty()) { - WriteServLog("ADMINS. Incorrect search handle."); - return -1; + printfd(__FILE__, "No admin in system!\n"); + return true; } -if (searchDescriptors[h] == data.end()) - return -1; - -AdminImpl a = *searchDescriptors[h]++; + const auto it = find(login); -*ac = a.GetConf(); + if (it == m_data.end() || it->password() != password) + return false; -return 0; -} -//----------------------------------------------------------------------------- -int AdminsImpl::CloseSearch(int h) const -{ -STG_LOCKER lock(&mutex); -if (searchDescriptors.find(h) != searchDescriptors.end()) - { - searchDescriptors.erase(searchDescriptors.find(h)); - return 0; - } + if (admin != nullptr) + *admin = &(*it); -WriteServLog("ADMINS. Incorrect search handle."); -return -1; + return true; } -//----------------------------------------------------------------------------- diff --git a/stargazer/admins_impl.h b/stargazer/admins_impl.h index a533cf2a..9c443a77 100644 --- a/stargazer/admins_impl.h +++ b/stargazer/admins_impl.h @@ -24,65 +24,60 @@ #pragma once -#include "admin_impl.h" - #include "stg/admins.h" #include "stg/admin.h" -#include "stg/locker.h" #include "stg/store.h" -#include "stg/noncopyable.h" #include "stg/logger.h" #include -#include #include - -#include +#include +#include namespace STG { -class AdminsImpl : public Admins { +class AdminsImpl : public Admins +{ public: - explicit AdminsImpl(Store * st); - virtual ~AdminsImpl() {} + explicit AdminsImpl(Store& st); - int Add(const std::string & login, const Admin * admin); - int Del(const std::string & login, const Admin * admin); - int Change(const AdminConf & ac, const Admin * admin); - const Admin * GetSysAdmin() const { return &stg; } - const Admin * GetNoAdmin() const { return &noAdmin; } - bool Find(const std::string & l, Admin ** admin); - bool Exists(const std::string & login) const; - bool Correct(const std::string & login, - const std::string & password, - Admin ** admin); - const std::string & GetStrError() const { return strError; } + AdminsImpl(const AdminsImpl&) = delete; + AdminsImpl& operator=(const AdminsImpl&) = delete; - size_t Count() const { return data.size(); } + int add(const std::string& login, const Admin& admin) override; + int del(const std::string& login, const Admin& admin) override; + int change(const AdminConf& ac, const Admin& admin) override; + const Admin& sysAdmin() const override { return m_stg; } + const Admin& noAdmin() const override { return m_noAdmin; } + bool find(const std::string& login, Admin** admin) override; + bool exists(const std::string& login) const override; + bool correct(const std::string& login, + const std::string& password, + Admin** admin) override; - int OpenSearch() const; - int SearchNext(int, AdminConf * ac) const; - int CloseSearch(int) const; + const std::string& strError() const override { return m_strError; } - private: - AdminsImpl(const AdminsImpl & rvalue); - AdminsImpl & operator=(const AdminsImpl & rvalue); - - typedef std::vector::iterator admin_iter; - typedef std::vector::const_iterator const_admin_iter; + size_t count() const override { return m_data.size(); } - int Read(); + void fmap(std::function callback) const + { + for (const auto& admin : m_data) + callback(admin); + } - AdminImpl stg; - AdminImpl noAdmin; - std::vector data; - Store * store; - Logger & WriteServLog; - mutable std::map searchDescriptors; - mutable unsigned int handle; - mutable pthread_mutex_t mutex; - std::string strError; + private: + void read(); + auto find(const std::string& login) { return std::find(m_data.begin(), m_data.end(), Admin(Priv(0), login, "")); } + auto find(const std::string& login) const { return std::find(m_data.begin(), m_data.end(), Admin(Priv(0), login, "")); } + + Admin m_stg; + Admin m_noAdmin; + std::vector m_data; + Store& m_store; + Logger& WriteServLog; + mutable std::mutex m_mutex; + std::string m_strError; }; } diff --git a/stargazer/corps_impl.cpp b/stargazer/corps_impl.cpp index c0da9a67..b41b4559 100644 --- a/stargazer/corps_impl.cpp +++ b/stargazer/corps_impl.cpp @@ -42,11 +42,11 @@ Read(); int CorporationsImpl::Add(const CorpConf & corp, const Admin * admin) { std::lock_guard lock(mutex); -const auto priv = admin->GetPriv(); +const auto& priv = admin->priv(); -if (!priv->corpChg) +if (!priv.corpChg) { - std::string s = admin->GetLogStr() + " Add corporation \'" + corp.name + "\'. Access denied."; + std::string s = admin->logStr() + " Add corporation \'" + corp.name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; @@ -57,7 +57,7 @@ crp_iter si(find(data.begin(), data.end(), corp)); if (si != data.end()) { strError = "Corporation \'" + corp.name + "\' cannot not be added. Corporation already exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } @@ -67,12 +67,12 @@ data.push_back(corp); if (store->AddCorp(corp.name) == 0) { WriteServLog("%s Corporation \'%s\' added.", - admin->GetLogStr().c_str(), corp.name.c_str()); + admin->logStr().c_str(), corp.name.c_str()); return 0; } strError = "Corporation \'" + corp.name + "\' was not added. Error: " + store->GetStrError(); -WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); +WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } @@ -80,11 +80,11 @@ return -1; int CorporationsImpl::Del(const std::string & name, const Admin * admin) { std::lock_guard lock(mutex); -const auto priv = admin->GetPriv(); +const auto& priv = admin->priv(); -if (!priv->corpChg) +if (!priv.corpChg) { - std::string s = admin->GetLogStr() + " Delete corporation \'" + name + "\'. Access denied."; + std::string s = admin->logStr() + " Delete corporation \'" + name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; @@ -95,7 +95,7 @@ crp_iter si(find(data.begin(), data.end(), CorpConf(name))); if (si == data.end()) { strError = "Corporation \'" + name + "\' cannot be deleted. Corporation does not exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } @@ -112,23 +112,23 @@ data.erase(si); if (store->DelCorp(name) < 0) { strError = "Corporation \'" + name + "\' was not deleted. Error: " + store->GetStrError(); - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } -WriteServLog("%s Corporation \'%s\' deleted.", admin->GetLogStr().c_str(), name.c_str()); +WriteServLog("%s Corporation \'%s\' deleted.", admin->logStr().c_str(), name.c_str()); return 0; } //----------------------------------------------------------------------------- int CorporationsImpl::Change(const CorpConf & corp, const Admin * admin) { std::lock_guard lock(mutex); -const auto priv = admin->GetPriv(); +const auto& priv = admin->priv(); -if (!priv->corpChg) +if (!priv.corpChg) { - std::string s = admin->GetLogStr() + " Change corporation \'" + corp.name + "\'. Access denied."; + std::string s = admin->logStr() + " Change corporation \'" + corp.name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; @@ -139,7 +139,7 @@ crp_iter si(find(data.begin(), data.end(), corp)); if (si == data.end()) { strError = "Corporation \'" + corp.name + "\' cannot be changed " + ". Corporation does not exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } @@ -152,7 +152,7 @@ if (store->SaveCorp(corp)) } WriteServLog("%s Corporation \'%s\' changed.", - admin->GetLogStr().c_str(), corp.name.c_str()); + admin->logStr().c_str(), corp.name.c_str()); return 0; } @@ -206,7 +206,7 @@ bool CorporationsImpl::Exists(const std::string & name) const std::lock_guard lock(mutex); if (data.empty()) { - printfd(__FILE__, "no admin in system!\n"); + printfd(__FILE__, "no corporations in system!\n"); return true; } diff --git a/stargazer/main.cpp b/stargazer/main.cpp index 565ca762..825cba58 100644 --- a/stargazer/main.cpp +++ b/stargazer/main.cpp @@ -302,11 +302,11 @@ int main(int argc, char* argv[]) auto& store = storeLoader.get(); WriteServLog("Storage plugin: %s. Loading successfull.", store.GetVersion().c_str()); - AdminsImpl admins(&store); + AdminsImpl admins(store); TariffsImpl tariffs(&store); ServicesImpl services(&store); CorporationsImpl corps(&store); - UsersImpl users(&settings, &store, &tariffs, services, admins.GetSysAdmin()); + UsersImpl users(&settings, &store, &tariffs, services, admins.sysAdmin()); TraffCounterImpl traffCnt(&users, settings.GetRulesFileName()); traffCnt.SetMonitorDir(settings.GetMonitorDir()); diff --git a/stargazer/plugins/configuration/rpcconfig/admins_methods.cpp b/stargazer/plugins/configuration/rpcconfig/admins_methods.cpp index 670f6789..d8fb0123 100644 --- a/stargazer/plugins/configuration/rpcconfig/admins_methods.cpp +++ b/stargazer/plugins/configuration/rpcconfig/admins_methods.cpp @@ -30,7 +30,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) STG::Admin * admin; -if (admins->Find(login, &admin)) +if (admins->find(login, &admin)) { structVal["result"] = xmlrpc_c::value_boolean(false); *retvalPtr = xmlrpc_c::value_struct(structVal); @@ -38,18 +38,18 @@ if (admins->Find(login, &admin)) } structVal["result"] = xmlrpc_c::value_boolean(true); -structVal["login"] = xmlrpc_c::value_string(admin->GetLogin()); -structVal["password"] = xmlrpc_c::value_string(admin->GetPassword()); +structVal["login"] = xmlrpc_c::value_string(admin->login()); +structVal["password"] = xmlrpc_c::value_string(admin->password()); -const auto priv = admin->GetPriv(); +const auto priv = admin->priv(); -structVal["user_stat"] = xmlrpc_c::value_boolean(priv->userStat); -structVal["user_conf"] = xmlrpc_c::value_boolean(priv->userConf); -structVal["user_cash"] = xmlrpc_c::value_boolean(priv->userCash); -structVal["user_passwd"] = xmlrpc_c::value_boolean(priv->userPasswd); -structVal["user_add_del"] = xmlrpc_c::value_boolean(priv->userAddDel); -structVal["admin_chg"] = xmlrpc_c::value_boolean(priv->adminChg); -structVal["tariff_chg"] = xmlrpc_c::value_boolean(priv->tariffChg); +structVal["user_stat"] = xmlrpc_c::value_boolean(priv.userStat); +structVal["user_conf"] = xmlrpc_c::value_boolean(priv.userConf); +structVal["user_cash"] = xmlrpc_c::value_boolean(priv.userCash); +structVal["user_passwd"] = xmlrpc_c::value_boolean(priv.userPasswd); +structVal["user_add_del"] = xmlrpc_c::value_boolean(priv.userAddDel); +structVal["admin_chg"] = xmlrpc_c::value_boolean(priv.adminChg); +structVal["tariff_chg"] = xmlrpc_c::value_boolean(priv.tariffChg); *retvalPtr = xmlrpc_c::value_struct(structVal); } @@ -74,14 +74,14 @@ if (config->GetAdminInfo(cookie, &adminInfo)) STG::Admin * admin; -if (admins->Find(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { printfd(__FILE__, "METHOD_ADMIN_ADD::execute(): 'Invalid admin (logged)'\n"); *retvalPtr = xmlrpc_c::value_boolean(false); return; } -if (admins->Add(login, admin)) +if (admins->add(login, *admin)) { printfd(__FILE__, "METHOD_ADMIN_ADD::execute(): 'Failed to add admin'\n"); *retvalPtr = xmlrpc_c::value_boolean(false); @@ -110,13 +110,13 @@ if (config->GetAdminInfo(cookie, &adminInfo)) STG::Admin * admin; -if (admins->Find(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; } -if (admins->Del(login, admin)) +if (admins->del(login, *admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -145,7 +145,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) STG::Admin * loggedAdmin; -if (admins->Find(adminInfo.admin, &loggedAdmin)) +if (admins->find(adminInfo.admin, &loggedAdmin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -153,7 +153,7 @@ if (admins->Find(adminInfo.admin, &loggedAdmin)) STG::Admin * admin; -if (admins->Find(login, &admin)) +if (admins->find(login, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -161,8 +161,8 @@ if (admins->Find(login, &admin)) STG::AdminConf conf; -conf.priv = *admin->GetPriv(); -conf.password = admin->GetPassword(); +conf.priv = admin->priv(); +conf.password = admin->password(); conf.login = login; std::map structVal = info; @@ -209,7 +209,7 @@ if ((it = structVal.find("tariff_chg")) != structVal.end()) conf.priv.tariffChg = xmlrpc_c::value_boolean(it->second); } -if (admins->Change(conf, loggedAdmin)) +if (admins->change(conf, *loggedAdmin)) { *retvalPtr = xmlrpc_c::value_boolean(false); } @@ -236,25 +236,22 @@ if (config->GetAdminInfo(cookie, &adminInfo)) return; } -STG::AdminConf ac; -int h = admins->OpenSearch(); - -while (admins->SearchNext(h, &ac) == 0) +admins->fmap([&retval](const auto& admin) { - std::map structVal; - structVal["result"] = xmlrpc_c::value_boolean(true); - structVal["login"] = xmlrpc_c::value_string(ac.login); - structVal["password"] = xmlrpc_c::value_string(ac.password); - structVal["user_stat"] = xmlrpc_c::value_boolean(ac.priv.userStat); - structVal["user_conf"] = xmlrpc_c::value_boolean(ac.priv.userConf); - structVal["user_cash"] = xmlrpc_c::value_boolean(ac.priv.userCash); - structVal["user_passwd"] = xmlrpc_c::value_boolean(ac.priv.userPasswd); - structVal["user_add_del"] = xmlrpc_c::value_boolean(ac.priv.userAddDel); - structVal["admin_chg"] = xmlrpc_c::value_boolean(ac.priv.adminChg); - structVal["tariff_chg"] = xmlrpc_c::value_boolean(ac.priv.tariffChg); - - retval.push_back(xmlrpc_c::value_struct(structVal)); - } + const std::map structVal{ + {"result", xmlrpc_c::value_boolean(true)}, + {"login", xmlrpc_c::value_string(admin.login())}, + {"password", xmlrpc_c::value_string(admin.password())}, + {"user_stat", xmlrpc_c::value_boolean(admin.priv().userStat)}, + {"user_conf", xmlrpc_c::value_boolean(admin.priv().userConf)}, + {"user_cash", xmlrpc_c::value_boolean(admin.priv().userCash)}, + {"user_passwd", xmlrpc_c::value_boolean(admin.priv().userPasswd)}, + {"user_add_del", xmlrpc_c::value_boolean(admin.priv().userAddDel)}, + {"admin_chg", xmlrpc_c::value_boolean(admin.priv().adminChg)}, + {"tariff_chg", xmlrpc_c::value_boolean(admin.priv().tariffChg)} + }; + retval.push_back(xmlrpc_c::value_struct(structVal)); + }); *retvalPtr = xmlrpc_c::value_array(retval); } diff --git a/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp b/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp index 629c7c37..2d45f109 100644 --- a/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp +++ b/stargazer/plugins/configuration/rpcconfig/rpcconfig.cpp @@ -259,7 +259,7 @@ bool RPC_CONFIG::CheckAdmin(const std::string & login, { STG::Admin * admin = NULL; -if (!admins->Correct(login, password, &admin)) +if (!admins->correct(login, password, &admin)) { logger("Attempt to connect with invalid credentials. Login: %s", login.c_str()); return true; @@ -268,7 +268,7 @@ if (!admins->Correct(login, password, &admin)) ADMIN_INFO info; time(&info.accessTime); info.admin = login; -info.priviledges = *admin->GetPriv(); +info.priviledges = admin->priv(); *cookie = GetCookie(); cookies[*cookie] = info; diff --git a/stargazer/plugins/configuration/rpcconfig/tariffs_methods.cpp b/stargazer/plugins/configuration/rpcconfig/tariffs_methods.cpp index 4a912cae..2047202a 100644 --- a/stargazer/plugins/configuration/rpcconfig/tariffs_methods.cpp +++ b/stargazer/plugins/configuration/rpcconfig/tariffs_methods.cpp @@ -62,7 +62,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) STG::Admin * admin; -if (admins->Find(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -142,7 +142,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) STG::Admin * admin; -if (admins->Find(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -174,7 +174,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) STG::Admin * admin; -if (admins->Find(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; diff --git a/stargazer/plugins/configuration/rpcconfig/users_methods.cpp b/stargazer/plugins/configuration/rpcconfig/users_methods.cpp index f2044854..650f7b37 100644 --- a/stargazer/plugins/configuration/rpcconfig/users_methods.cpp +++ b/stargazer/plugins/configuration/rpcconfig/users_methods.cpp @@ -72,7 +72,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) STG::Admin * admin = NULL; -if (admins->Find(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -115,7 +115,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) STG::Admin * admin; -if (admins->Find(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -205,7 +205,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) STG::Admin * admin; -if (admins->Find(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -257,7 +257,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) STG::Admin * admin; -if (admins->Find(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -306,7 +306,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) STG::Admin * admin; -if (admins->Find(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; @@ -353,7 +353,7 @@ if (config->GetAdminInfo(cookie, &adminInfo)) STG::Admin * admin; -if (admins->Find(adminInfo.admin, &admin)) +if (admins->find(adminInfo.admin, &admin)) { *retvalPtr = xmlrpc_c::value_boolean(false); return; diff --git a/stargazer/plugins/configuration/sgconfig/conn.cpp b/stargazer/plugins/configuration/sgconfig/conn.cpp index 31a0cbda..92d74796 100644 --- a/stargazer/plugins/configuration/sgconfig/conn.cpp +++ b/stargazer/plugins/configuration/sgconfig/conn.cpp @@ -165,7 +165,7 @@ bool Conn::HandleHeader() bool Conn::HandleLogin() { - if (m_admins.Find(m_login, &m_admin)) // ADMINS::Find returns true on error. + if (m_admins.find(m_login, &m_admin)) // ADMINS::Find returns true on error. { std::string login(m_login, strnlen(m_login, sizeof(m_login))); Log(__FILE__, "Received invalid login '" + ToPrintable(login) + "' from " + endpoint() + "."); @@ -173,7 +173,7 @@ bool Conn::HandleLogin() m_state = ERROR; return false; } - m_admin->SetIP(IP()); + m_admin->setIP(IP()); m_state = CRYPTO_LOGIN; m_buffer = m_cryptoLogin; m_bufferSize = sizeof(m_cryptoLogin); @@ -184,12 +184,12 @@ bool Conn::HandleCryptoLogin() { char login[ADM_LOGIN_LEN + 1]; BLOWFISH_CTX ctx; - InitContext(m_admin->GetPassword().c_str(), ADM_PASSWD_LEN, &ctx); + InitContext(m_admin->password().c_str(), ADM_PASSWD_LEN, &ctx); DecryptString(login, m_cryptoLogin, ADM_LOGIN_LEN, &ctx); if (strncmp(m_login, login, sizeof(login)) != 0) { - Log(__FILE__, "Attempt to connect with wrong password from " + m_admin->GetLogin() + "@" + endpoint() + "."); + Log(__FILE__, "Attempt to connect with wrong password from " + m_admin->login() + "@" + endpoint() + "."); WriteAnswer(ERR_LOGINS, sizeof(ERR_LOGINS) - 1); // Without \0 m_state = ERROR; return false; @@ -198,7 +198,7 @@ bool Conn::HandleCryptoLogin() m_state = DATA; m_buffer = m_data; m_bufferSize = sizeof(m_data); - m_stream = new STG::DECRYPT_STREAM(m_admin->GetPassword(), DataCallback, &m_dataState); + m_stream = new STG::DECRYPT_STREAM(m_admin->password(), DataCallback, &m_dataState); return WriteAnswer(OK_LOGINS, sizeof(OK_LOGINS) - 1); // Without \0 } @@ -221,7 +221,7 @@ bool Conn::DataCallback(const void * block, size_t size, void * data) if (XML_Parse(state.conn.m_xmlParser, xml, length, state.final) == XML_STATUS_ERROR) { - state.conn.Log(__FILE__, "Received invalid XML from " + state.conn.m_admin->GetLogin() + "@" + state.conn.endpoint() + "."); + state.conn.Log(__FILE__, "Received invalid XML from " + state.conn.m_admin->login() + "@" + state.conn.endpoint() + "."); printfd(__FILE__, "XML parse error at line %d, %d: %s. Is final: %d\n", static_cast(XML_GetCurrentLineNumber(state.conn.m_xmlParser)), static_cast(XML_GetCurrentColumnNumber(state.conn.m_xmlParser)), @@ -235,7 +235,7 @@ bool Conn::DataCallback(const void * block, size_t size, void * data) { if (!state.conn.WriteResponse()) { - state.conn.Log(__FILE__, "Failed to write response to " + state.conn.m_admin->GetLogin() + "@" + state.conn.endpoint() + "."); + state.conn.Log(__FILE__, "Failed to write response to " + state.conn.m_admin->login() + "@" + state.conn.endpoint() + "."); state.conn.m_state = ERROR; return false; } @@ -255,7 +255,7 @@ void Conn::ParseXMLStart(void * data, const char * el, const char ** attr) if (conn.m_parser == NULL) { - conn.Log(__FILE__, "Received unknown command '" + std::string(el) + "' from " + conn.m_admin->GetLogin() + "@" + conn.endpoint() + "."); + conn.Log(__FILE__, "Received unknown command '" + std::string(el) + "' from " + conn.m_admin->login() + "@" + conn.endpoint() + "."); conn.m_state = ERROR; return; } @@ -280,7 +280,7 @@ void Conn::ParseXMLEnd(void * data, const char * el) bool Conn::WriteResponse() { - STG::ENCRYPT_STREAM stream(m_admin->GetPassword(), WriteCallback, this); + STG::ENCRYPT_STREAM stream(m_admin->password(), WriteCallback, this); std::string answer; if (m_parser != NULL) answer = m_parser->GetAnswer(); diff --git a/stargazer/plugins/configuration/sgconfig/parser_admins.cpp b/stargazer/plugins/configuration/sgconfig/parser_admins.cpp index 0e390ffd..bc47b7fd 100644 --- a/stargazer/plugins/configuration/sgconfig/parser_admins.cpp +++ b/stargazer/plugins/configuration/sgconfig/parser_admins.cpp @@ -39,29 +39,25 @@ const char * CHG_ADMIN::tag = "ChgAdmin"; void GET_ADMINS::CreateAnswer() { - const auto priv = m_currAdmin.GetPriv(); - if (!priv->adminChg) + const auto& priv = m_currAdmin.priv(); + if (!priv.adminChg) { m_answer = ""; return; } m_answer = ""; - AdminConf ac; - int h = m_admins.OpenSearch(); - - while (m_admins.SearchNext(h, &ac) == 0) - { - unsigned int p = (ac.priv.userStat << 0) + - (ac.priv.userConf << 2) + - (ac.priv.userCash << 4) + - (ac.priv.userPasswd << 6) + - (ac.priv.userAddDel << 8) + - (ac.priv.adminChg << 10) + - (ac.priv.tariffChg << 12); - m_answer += ""; - } - m_admins.CloseSearch(h); + m_admins.fmap([this](const Admin& admin) + { + const unsigned int p = (admin.priv().userStat << 0) + + (admin.priv().userConf << 2) + + (admin.priv().userCash << 4) + + (admin.priv().userPasswd << 6) + + (admin.priv().userAddDel << 8) + + (admin.priv().adminChg << 10) + + (admin.priv().tariffChg << 12); + m_answer += ""; + }); m_answer += ""; } @@ -77,10 +73,10 @@ int DEL_ADMIN::Start(void *, const char * el, const char ** attr) void DEL_ADMIN::CreateAnswer() { - if (m_admins.Del(m_admin, &m_currAdmin) == 0) + if (m_admins.del(m_admin, m_currAdmin) == 0) m_answer = "<" + m_tag + " Result=\"Ok\"/>"; else - m_answer = "<" + m_tag + " Result=\"Error. " + m_admins.GetStrError() + "\"/>"; + m_answer = "<" + m_tag + " Result=\"Error. " + m_admins.strError() + "\"/>"; } int ADD_ADMIN::Start(void *, const char *el, const char **attr) @@ -95,10 +91,10 @@ int ADD_ADMIN::Start(void *, const char *el, const char **attr) void ADD_ADMIN::CreateAnswer() { - if (m_admins.Add(m_admin, &m_currAdmin) == 0) + if (m_admins.add(m_admin, m_currAdmin) == 0) m_answer = "<" + m_tag + " Result=\"Ok\"/>"; else - m_answer = "<" + m_tag + " Result=\"Error. " + m_admins.GetStrError() + "\"/>"; + m_answer = "<" + m_tag + " Result=\"Error. " + m_admins.strError() + "\"/>"; } int CHG_ADMIN::Start(void *, const char * el, const char ** attr) @@ -141,13 +137,13 @@ void CHG_ADMIN::CreateAnswer() { Admin * origAdmin = NULL; - if (m_admins.Find(login, &origAdmin)) + if (m_admins.find(login, &origAdmin)) { m_answer = "<" + m_tag + " Result = \"Admin '" + login + "' is not found.\"/>"; return; } - AdminConf conf(origAdmin->GetConf()); + AdminConf conf(origAdmin->conf()); if (!password.empty()) conf.password = password.data(); @@ -164,8 +160,8 @@ void CHG_ADMIN::CreateAnswer() conf.priv = Priv(p); } - if (m_admins.Change(conf, &m_currAdmin) != 0) - m_answer = "<" + m_tag + " Result = \"" + m_admins.GetStrError() + "\"/>"; + if (m_admins.change(conf, m_currAdmin) != 0) + m_answer = "<" + m_tag + " Result = \"" + m_admins.strError() + "\"/>"; else m_answer = "<" + m_tag + " Result = \"Ok\"/>"; } diff --git a/stargazer/plugins/configuration/sgconfig/parser_users.cpp b/stargazer/plugins/configuration/sgconfig/parser_users.cpp index 6ef06b30..bd7dd4ef 100644 --- a/stargazer/plugins/configuration/sgconfig/parser_users.cpp +++ b/stargazer/plugins/configuration/sgconfig/parser_users.cpp @@ -209,7 +209,7 @@ void GET_USERS::CreateAnswer() UserPtr u; while (m_users.SearchNext(h, &u) == 0) - m_answer += UserToXML(*u, true, m_currAdmin.GetPriv()->userConf || m_currAdmin.GetPriv()->userPasswd, m_lastUserUpdateTime); + m_answer += UserToXML(*u, true, m_currAdmin.priv().userConf || m_currAdmin.priv().userPasswd, m_lastUserUpdateTime); m_users.CloseSearch(h); @@ -235,7 +235,7 @@ void GET_USER::CreateAnswer() if (m_users.FindByName(m_login, &u)) m_answer = ""; else - m_answer = UserToXML(*u, false, m_currAdmin.GetPriv()->userConf || m_currAdmin.GetPriv()->userPasswd); + m_answer = UserToXML(*u, false, m_currAdmin.priv().userConf || m_currAdmin.priv().userPasswd); } int ADD_USER::Start(void *, const char * el, const char ** attr) @@ -484,7 +484,7 @@ int CHG_USER::ApplyChanges() if (check && alwaysOnline && !onlyOneIP) { printfd(__FILE__, "Requested change leads to a forbidden state: AlwaysOnline with multiple IP's\n"); - PluginLogger::get("conf_sg")("%s Requested change leads to a forbidden state: AlwaysOnline with multiple IP's", m_currAdmin.GetLogStr().c_str()); + PluginLogger::get("conf_sg")("%s Requested change leads to a forbidden state: AlwaysOnline with multiple IP's", m_currAdmin.logStr().c_str()); return -1; } @@ -495,7 +495,7 @@ int CHG_USER::ApplyChanges() if (m_users.IsIPInUse(ip, m_login, &user)) { printfd(__FILE__, "Trying to assign an IP %s to '%s' that is already in use by '%s'\n", inet_ntostring(ip).c_str(), m_login.c_str(), user->GetLogin().c_str()); - PluginLogger::get("conf_sg")("%s trying to assign an IP %s to '%s' that is currently in use by '%s'", m_currAdmin.GetLogStr().c_str(), inet_ntostring(ip).c_str(), m_login.c_str(), user->GetLogin().c_str()); + PluginLogger::get("conf_sg")("%s trying to assign an IP %s to '%s' that is currently in use by '%s'", m_currAdmin.logStr().c_str(), inet_ntostring(ip).c_str(), m_login.c_str(), user->GetLogin().c_str()); return -1; } } diff --git a/stargazer/plugins/other/smux/sensors.h b/stargazer/plugins/other/smux/sensors.h index 91ebb01a..92151807 100644 --- a/stargazer/plugins/other/smux/sensors.h +++ b/stargazer/plugins/other/smux/sensors.h @@ -185,7 +185,7 @@ class TotalAdminsSensor : public Sensor { bool GetValue(ObjectSyntax_t * objectSyntax) const override { - ValueToOS(admins.Count(), objectSyntax); + ValueToOS(admins.count(), objectSyntax); return true; } diff --git a/stargazer/services_impl.cpp b/stargazer/services_impl.cpp index 620a6ecb..36640c88 100644 --- a/stargazer/services_impl.cpp +++ b/stargazer/services_impl.cpp @@ -43,11 +43,11 @@ Read(); int ServicesImpl::Add(const ServiceConf & service, const Admin * admin) { std::lock_guard lock(mutex); -const auto priv = admin->GetPriv(); +const auto& priv = admin->priv(); -if (!priv->serviceChg) +if (!priv.serviceChg) { - std::string s = admin->GetLogStr() + " Add service \'" + service.name + "\'. Access denied."; + std::string s = admin->logStr() + " Add service \'" + service.name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; @@ -58,7 +58,7 @@ iterator si(std::find(data.begin(), data.end(), service)); if (si != data.end()) { strError = "Service \'" + service.name + "\' cannot not be added. Service already exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } @@ -68,12 +68,12 @@ data.push_back(service); if (store->AddService(service.name) == 0) { WriteServLog("%s Service \'%s\' added.", - admin->GetLogStr().c_str(), service.name.c_str()); + admin->logStr().c_str(), service.name.c_str()); return 0; } strError = "Service \'" + service.name + "\' was not added. Error: " + store->GetStrError(); -WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); +WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } @@ -81,11 +81,11 @@ return -1; int ServicesImpl::Del(const std::string & name, const Admin * admin) { std::lock_guard lock(mutex); -const auto priv = admin->GetPriv(); +const auto& priv = admin->priv(); -if (!priv->serviceChg) +if (!priv.serviceChg) { - std::string s = admin->GetLogStr() + " Delete service \'" + name + "\'. Access denied."; + std::string s = admin->logStr() + " Delete service \'" + name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; @@ -96,7 +96,7 @@ iterator si(std::find(data.begin(), data.end(), ServiceConf(name))); if (si == data.end()) { strError = "Service \'" + name + "\' cannot be deleted. Service does not exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } @@ -113,23 +113,23 @@ data.erase(si); if (store->DelService(name) < 0) { strError = "Service \'" + name + "\' was not deleted. Error: " + store->GetStrError(); - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } -WriteServLog("%s Service \'%s\' deleted.", admin->GetLogStr().c_str(), name.c_str()); +WriteServLog("%s Service \'%s\' deleted.", admin->logStr().c_str(), name.c_str()); return 0; } //----------------------------------------------------------------------------- int ServicesImpl::Change(const ServiceConf & service, const Admin * admin) { std::lock_guard lock(mutex); -const auto priv = admin->GetPriv(); +const auto& priv = admin->priv(); -if (!priv->serviceChg) +if (!priv.serviceChg) { - std::string s = admin->GetLogStr() + " Change service \'" + service.name + "\'. Access denied."; + std::string s = admin->logStr() + " Change service \'" + service.name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; @@ -140,13 +140,13 @@ iterator si(std::find(data.begin(), data.end(), service)); if (si == data.end()) { strError = "Service \'" + service.name + "\' cannot be changed " + ". Service does not exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } -printfd(__FILE__, "Old cost = %f, old pay day = %d\n", si->cost, (unsigned)si->payDay); +printfd(__FILE__, "Old cost = %f, old pay day = %u\n", si->cost, static_cast(si->payDay)); *si = service; -printfd(__FILE__, "New cost = %f, New pay day = %d\n", si->cost, (unsigned)si->payDay); +printfd(__FILE__, "New cost = %f, New pay day = %u\n", si->cost, static_cast(si->payDay)); if (store->SaveService(service)) { WriteServLog("Cannot write service %s.", service.name.c_str()); @@ -155,7 +155,7 @@ if (store->SaveService(service)) } WriteServLog("%s Service \'%s\' changed.", - admin->GetLogStr().c_str(), service.name.c_str()); + admin->logStr().c_str(), service.name.c_str()); return 0; } diff --git a/stargazer/tariffs_impl.cpp b/stargazer/tariffs_impl.cpp index 7e92657d..9cc829dd 100644 --- a/stargazer/tariffs_impl.cpp +++ b/stargazer/tariffs_impl.cpp @@ -100,11 +100,11 @@ return NULL; //----------------------------------------------------------------------------- int TariffsImpl::Chg(const TariffData & td, const Admin * admin) { -const auto priv = admin->GetPriv(); +const auto& priv = admin->priv(); -if (!priv->tariffChg) +if (!priv.tariffChg) { - std::string s = admin->GetLogStr() + " Change tariff \'" + std::string s = admin->logStr() + " Change tariff \'" + td.tariffConf.name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); @@ -118,7 +118,7 @@ auto ti = find(tariffs.begin(), tariffs.end(), TariffImpl(td.tariffConf.name)); if (ti == tariffs.end()) { strError = "Tariff \'" + td.tariffConf.name + "\' cannot be changed. Tariff does not exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } @@ -132,18 +132,18 @@ if (store->SaveTariff(td, td.tariffConf.name)) } WriteServLog("%s Tariff \'%s\' changed.", - admin->GetLogStr().c_str(), td.tariffConf.name.c_str()); + admin->logStr().c_str(), td.tariffConf.name.c_str()); return 0; } //----------------------------------------------------------------------------- int TariffsImpl::Del(const std::string & name, const Admin * admin) { -const auto priv = admin->GetPriv(); +const auto& priv = admin->priv(); -if (!priv->tariffChg) +if (!priv.tariffChg) { - std::string s = admin->GetLogStr() + " Delete tariff \'" + std::string s = admin->logStr() + " Delete tariff \'" + name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); @@ -160,7 +160,7 @@ TariffData td; if (ti == tariffs.end()) { strError = "Tariff \'" + name + "\' cannot be deleted. Tariff does not exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } @@ -184,18 +184,18 @@ while (ni != onDelNotifiers.end()) } WriteServLog("%s Tariff \'%s\' deleted.", - admin->GetLogStr().c_str(), + admin->logStr().c_str(), name.c_str()); return 0; } //----------------------------------------------------------------------------- int TariffsImpl::Add(const std::string & name, const Admin * admin) { -const auto priv = admin->GetPriv(); +const auto& priv = admin->priv(); -if (!priv->tariffChg) +if (!priv.tariffChg) { - std::string s = admin->GetLogStr() + " Add tariff \'" + std::string s = admin->logStr() + " Add tariff \'" + name + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); @@ -210,7 +210,7 @@ if (!priv->tariffChg) if (ti != tariffs.end()) { strError = "Tariff \'" + name + "\' cannot be added. Tariff already exist."; - WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->logStr().c_str(), strError.c_str()); return -1; } @@ -233,7 +233,7 @@ while (ni != onAddNotifiers.end()) } WriteServLog("%s Tariff \'%s\' added.", - admin->GetLogStr().c_str(), name.c_str()); + admin->logStr().c_str(), name.c_str()); return 0; } diff --git a/stargazer/users_impl.cpp b/stargazer/users_impl.cpp index 535a0316..ca8f681f 100644 --- a/stargazer/users_impl.cpp +++ b/stargazer/users_impl.cpp @@ -44,7 +44,7 @@ using STG::UsersImpl; //----------------------------------------------------------------------------- UsersImpl::UsersImpl(SettingsImpl * s, Store * st, Tariffs * t, Services & svcs, - const Admin * sa) + const Admin& sa) : settings(s), tariffs(t), m_services(svcs), @@ -130,42 +130,19 @@ return false; int UsersImpl::Add(const std::string & login, const Admin * admin) { STG_LOCKER lock(&mutex); -const auto priv = admin->GetPriv(); +const auto& priv = admin->priv(); -if (!priv->userAddDel) +if (!priv.userAddDel) { WriteServLog("%s tried to add user \'%s\'. Access denied.", - admin->GetLogStr().c_str(), login.c_str()); - /*errorStr = "Admin \'" + admin->GetLogin() + - "\': tried to add user \'" + ud->login + "\'. Access denied.";*/ + admin->logStr().c_str(), login.c_str()); return -1; } -////// if (store->AddUser(login)) - { - //TODO - //WriteServLog("Admin \'%s\': tried to add user \'%s\'. Access denied.", - // admin->GetLogin().c_str(), ud->login.c_str()); return -1; - } -////// - -UserImpl u(settings, store, tariffs, sysAdmin, this, m_services); - -/*struct tm * tms; -time_t t = stgTime; - -tms = localtime(&t); - -tms->tm_hour = 0; -tms->tm_min = 0; -tms->tm_sec = 0; - -if (settings->GetDayResetTraff() > tms->tm_mday) - tms->tm_mon -= 1; -tms->tm_mday = settings->GetDayResetTraff();*/ +UserImpl u(settings, store, tariffs, &sysAdmin, this, m_services); u.SetLogin(login); @@ -175,7 +152,7 @@ u.WriteConf(); u.WriteStat(); WriteServLog("%s User \'%s\' added.", - admin->GetLogStr().c_str(), login.c_str()); + admin->logStr().c_str(), login.c_str()); u.OnAdd(); @@ -208,13 +185,13 @@ return 0; //----------------------------------------------------------------------------- void UsersImpl::Del(const std::string & login, const Admin * admin) { -const auto priv = admin->GetPriv(); +const auto& priv = admin->priv(); user_iter u; -if (!priv->userAddDel) +if (!priv.userAddDel) { WriteServLog("%s tried to remove user \'%s\'. Access denied.", - admin->GetLogStr().c_str(), login.c_str()); + admin->logStr().c_str(), login.c_str()); return; } @@ -225,7 +202,7 @@ if (!priv->userAddDel) if (FindByNameNonLock(login, &u)) { WriteServLog("%s tried to delete user \'%s\': not found.", - admin->GetLogStr().c_str(), + admin->logStr().c_str(), login.c_str()); return; } @@ -264,7 +241,7 @@ if (!priv->userAddDel) DelUserFromIndexes(u); WriteServLog("%s User \'%s\' deleted.", - admin->GetLogStr().c_str(), login.c_str()); + admin->logStr().c_str(), login.c_str()); } } @@ -339,7 +316,7 @@ user_iter ui; unsigned errors = 0; for (unsigned int i = 0; i < usersList.size(); i++) { - UserImpl u(settings, store, tariffs, sysAdmin, this, m_services); + UserImpl u(settings, store, tariffs, &sysAdmin, this, m_services); u.SetLogin(usersList[i]); users.push_front(u); diff --git a/stargazer/users_impl.h b/stargazer/users_impl.h index d8414375..ef5ee0ec 100644 --- a/stargazer/users_impl.h +++ b/stargazer/users_impl.h @@ -70,7 +70,7 @@ public: UsersImpl(SettingsImpl * s, Store * store, Tariffs * tariffs, Services & svcs, - const Admin * sysAdmin); + const Admin& sysAdmin); virtual ~UsersImpl(); int FindByName(const std::string & login, UserPtr * user) override; @@ -150,7 +150,7 @@ private: Tariffs * tariffs; Services & m_services; Store * store; - const Admin * sysAdmin; + const Admin& sysAdmin; Logger & WriteServLog; bool nonstop; @@ -167,29 +167,5 @@ private: std::set*> onAddNotifiersImpl; std::set*> onDelNotifiersImpl; }; -//----------------------------------------------------------------------------- -/*inline -void PROPERTY_NOTIFER_IP_BEFORE::Notify(const uint32_t & oldValue, - const uint32_t &) -{ -if (!oldValue) - return; -//EVENT_LOOP_SINGLETON::GetInstance().Enqueue(users, &Users::DelFromIPIdx, oldValue); -// Using explicit call to assure that index is valid, because fast reconnect with delayed call can result in authorization error -users.DelFromIPIdx(oldValue); -} -//----------------------------------------------------------------------------- -inline -void PROPERTY_NOTIFER_IP_AFTER::Notify(const uint32_t &, - const uint32_t & newValue) -{ -if (!newValue) - return; - -//EVENT_LOOP_SINGLETON::GetInstance().Enqueue(users, &Users::AddToIPIdx, user); -// Using explicit call to assure that index is valid, because fast reconnect with delayed call can result in authorization error -users.AddToIPIdx(user); -}*/ -//----------------------------------------------------------------------------- } diff --git a/tests/test_disable_session_log.cpp b/tests/test_disable_session_log.cpp index ac042a58..449f1f78 100644 --- a/tests/test_disable_session_log.cpp +++ b/tests/test_disable_session_log.cpp @@ -1,11 +1,11 @@ #include "tut/tut.hpp" +#include "stg/admin.h" #include "stg/user_property.h" #include "user_impl.h" #include "testsettings.h" #include "testtariffs.h" -#include "testadmin.h" #include "teststore.h" #include "testauth.h" #include "testusers.h" @@ -71,7 +71,7 @@ namespace tut TEST_SETTINGS_LOCAL settings(false); TEST_TARIFFS tariffs; - TEST_ADMIN admin; + STG::Admin admin(STG::Priv(0xFFFF), {}, {}); TEST_STORE_LOCAL store; TEST_AUTH auth; TEST_USERS users; @@ -114,7 +114,7 @@ namespace tut TEST_SETTINGS_LOCAL settings(true); TEST_TARIFFS tariffs; - TEST_ADMIN admin; + STG::Admin admin(STG::Priv(0xFFFF), {}, {}); TEST_STORE_LOCAL store; TEST_AUTH auth; TEST_USERS users; diff --git a/tests/test_fee_charge_rules.cpp b/tests/test_fee_charge_rules.cpp index b2a2790e..1fe1542f 100644 --- a/tests/test_fee_charge_rules.cpp +++ b/tests/test_fee_charge_rules.cpp @@ -1,11 +1,11 @@ #include "tut/tut.hpp" +#include "stg/admin.h" #include "stg/user_property.h" #include "user_impl.h" #include "testsettings.h" #include "testtariffs.h" -#include "testadmin.h" #include "teststore.h" #include "testservices.h" @@ -39,7 +39,7 @@ namespace tut TEST_SETTINGS_LOCAL settings(0); TEST_TARIFFS tariffs; - TEST_ADMIN admin; + STG::Admin admin(STG::Priv(0xFFFF), {}, {}); TEST_STORE store; TEST_SERVICES services; STG::UserImpl user(&settings, &store, &tariffs, &admin, NULL, services); @@ -72,7 +72,7 @@ namespace tut TEST_SETTINGS_LOCAL settings(1); TEST_TARIFFS tariffs; - TEST_ADMIN admin; + STG::Admin admin(STG::Priv(0xFFFF), {}, {}); TEST_STORE store; TEST_SERVICES services; STG::UserImpl user(&settings, &store, &tariffs, &admin, NULL, services); @@ -119,7 +119,7 @@ namespace tut TEST_SETTINGS_LOCAL settings(2); TEST_TARIFFS tariffs; - TEST_ADMIN admin; + STG::Admin admin(STG::Priv(0xFFFF), {}, {}); TEST_STORE store; TEST_SERVICES services; STG::UserImpl user(&settings, &store, &tariffs, &admin, NULL, services); diff --git a/tests/test_filter_params_log.cpp b/tests/test_filter_params_log.cpp index 808a67fd..3223df83 100644 --- a/tests/test_filter_params_log.cpp +++ b/tests/test_filter_params_log.cpp @@ -1,11 +1,11 @@ #include "tut/tut.hpp" +#include "stg/admin.h" #include "stg/user_property.h" #include "user_impl.h" #include "testsettings.h" #include "testtariffs.h" -#include "testadmin.h" #include "teststore.h" #include "testauth.h" #include "testusers.h" @@ -65,7 +65,7 @@ namespace tut TEST_SETTINGS_LOCAL settings; settings.addFilter("*"); // Allow everything by default. TEST_TARIFFS tariffs; - TEST_ADMIN admin; + STG::Admin admin(STG::Priv(0xFFFF), {}, {}); TEST_STORE_LOCAL store; TEST_AUTH auth; TEST_USERS users; @@ -105,7 +105,7 @@ namespace tut TEST_SETTINGS_LOCAL settings; settings.addFilter("address"); // Allow everything by default. TEST_TARIFFS tariffs; - TEST_ADMIN admin; + STG::Admin admin(STG::Priv(0xFFFF), {}, {}); TEST_STORE_LOCAL store; TEST_AUTH auth; TEST_USERS users; @@ -145,7 +145,7 @@ namespace tut settings.addFilter("address"); // Allow everything by default. settings.addFilter("group"); // Allow everything by default. TEST_TARIFFS tariffs; - TEST_ADMIN admin; + STG::Admin admin(STG::Priv(0xFFFF), {}, {}); TEST_STORE_LOCAL store; TEST_AUTH auth; TEST_USERS users; @@ -183,7 +183,7 @@ namespace tut TEST_SETTINGS_LOCAL settings; TEST_TARIFFS tariffs; - TEST_ADMIN admin; + STG::Admin admin(STG::Priv(0xFFFF), {}, {}); TEST_STORE_LOCAL store; TEST_AUTH auth; TEST_USERS users; diff --git a/tests/test_reconnect_on_tariff_change.cpp b/tests/test_reconnect_on_tariff_change.cpp index 610231ce..b2e2aa02 100644 --- a/tests/test_reconnect_on_tariff_change.cpp +++ b/tests/test_reconnect_on_tariff_change.cpp @@ -1,11 +1,11 @@ #include "tut/tut.hpp" +#include "stg/admin.h" #include "stg/user_property.h" #include "user_impl.h" #include "testsettings.h" #include "testtariffs.h" -#include "testadmin.h" #include "teststore.h" #include "testauth.h" #include "testusers.h" @@ -63,7 +63,7 @@ namespace tut TEST_SETTINGS_LOCAL settings(false); TEST_TARIFFS tariffs; - TEST_ADMIN admin; + STG::Admin admin(STG::Priv(0xFFFF), {}, {}); TEST_STORE store; TEST_AUTH auth; TEST_USERS users; @@ -121,7 +121,7 @@ namespace tut ensure("s2->GetReconnectOnTariffChange() == true", s2->GetReconnectOnTariffChange()); TEST_TARIFFS tariffs; - TEST_ADMIN admin; + STG::Admin admin(STG::Priv(0xFFFF), {}, {}); TEST_STORE store; TEST_AUTH auth; TEST_USERS users; diff --git a/tests/testadmin.h b/tests/testadmin.h deleted file mode 100644 index 15b47742..00000000 --- a/tests/testadmin.h +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef __TEST_ADMIN_H__ -#define __TEST_ADMIN_H__ - -#include "stg/admin.h" - -class TEST_ADMIN : public STG::Admin { - public: - TEST_ADMIN() : priv(0xffFF), ip(0) {} - - const std::string & GetPassword() const override { return password; } - const std::string & GetLogin() const override { return login; } - STG::Priv const * GetPriv() const override { return &priv; } - uint32_t GetPrivAsInt() const override { return priv.toInt(); } - const STG::AdminConf & GetConf() const override { return conf; } - uint32_t GetIP() const override { return ip; } - std::string GetIPStr() const override { return inet_ntostring(ip); } - void SetIP(uint32_t ip) override { TEST_ADMIN::ip = ip; } - const std::string GetLogStr() const override { return ""; } - - private: - std::string password; - std::string login; - STG::Priv priv; - STG::AdminConf conf; - uint32_t ip; -}; - -#endif