#pragma once
+#include "admin_conf.h"
+
+#include "stg/common.h"
+
#include <string>
#include <cstdint>
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;
};
}
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),
//-----------------------------------------------------------------------------
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)
#pragma once
#include <string>
+#include <functional>
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<void (const Admin&)> callback) const = 0;
};
}
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;
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 <typename T>
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(),
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;
}
}
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
tariffs_impl.cpp
corps_impl.cpp
services_impl.cpp
- admin_impl.cpp
user_impl.cpp
tariff_impl.cpp
eventloop.cpp
+++ /dev/null
-/*
- * 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 <stg34@stargazer.dp.ua>
- */
-
- /*
- $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() + ":";
-}
-//-----------------------------------------------------------------------------
+++ /dev/null
-/*
- * 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 <stg34@stargazer.dp.ua>
- */
-
-#pragma once
-
-#include "stg/admin.h"
-#include "stg/admin_conf.h"
-
-#include <string>
-#include <cstdint>
-
-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;
-};
-
-}
* Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
*/
- /*
- $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 <algorithm>
-#include <cerrno>
-#include <cassert>
-
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<std::mutex> 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<std::mutex> 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<int, const_admin_iter>::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<std::mutex> 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<std::string> adminsList;
-if (store->GetAdminsList(&adminsList) < 0)
+ std::vector<std::string> 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<Admin> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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<std::mutex> 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;
}
-//-----------------------------------------------------------------------------
#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 <vector>
-#include <map>
#include <string>
-
-#include <pthread.h>
+#include <algorithm>
+#include <mutex>
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<AdminImpl>::iterator admin_iter;
- typedef std::vector<AdminImpl>::const_iterator const_admin_iter;
+ size_t count() const override { return m_data.size(); }
- int Read();
+ void fmap(std::function<void (const Admin&)> callback) const
+ {
+ for (const auto& admin : m_data)
+ callback(admin);
+ }
- AdminImpl stg;
- AdminImpl noAdmin;
- std::vector<AdminImpl> data;
- Store * store;
- Logger & WriteServLog;
- mutable std::map<int, const_admin_iter> 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<Admin> m_data;
+ Store& m_store;
+ Logger& WriteServLog;
+ mutable std::mutex m_mutex;
+ std::string m_strError;
};
}
int CorporationsImpl::Add(const CorpConf & corp, const Admin * admin)
{
std::lock_guard<std::mutex> 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;
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;
}
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;
}
int CorporationsImpl::Del(const std::string & name, const Admin * admin)
{
std::lock_guard<std::mutex> 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;
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;
}
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<std::mutex> 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;
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;
}
}
WriteServLog("%s Corporation \'%s\' changed.",
- admin->GetLogStr().c_str(), corp.name.c_str());
+ admin->logStr().c_str(), corp.name.c_str());
return 0;
}
std::lock_guard<std::mutex> lock(mutex);
if (data.empty())
{
- printfd(__FILE__, "no admin in system!\n");
+ printfd(__FILE__, "no corporations in system!\n");
return true;
}
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());
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);
}
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);
}
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);
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;
STG::Admin * loggedAdmin;
-if (admins->Find(adminInfo.admin, &loggedAdmin))
+if (admins->find(adminInfo.admin, &loggedAdmin))
{
*retvalPtr = xmlrpc_c::value_boolean(false);
return;
STG::Admin * admin;
-if (admins->Find(login, &admin))
+if (admins->find(login, &admin))
{
*retvalPtr = xmlrpc_c::value_boolean(false);
return;
STG::AdminConf conf;
-conf.priv = *admin->GetPriv();
-conf.password = admin->GetPassword();
+conf.priv = admin->priv();
+conf.password = admin->password();
conf.login = login;
std::map<std::string, xmlrpc_c::value> structVal = info;
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);
}
return;
}
-STG::AdminConf ac;
-int h = admins->OpenSearch();
-
-while (admins->SearchNext(h, &ac) == 0)
+admins->fmap([&retval](const auto& admin)
{
- std::map<std::string, xmlrpc_c::value> 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<std::string, xmlrpc_c::value> 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);
}
{
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;
ADMIN_INFO info;
time(&info.accessTime);
info.admin = login;
-info.priviledges = *admin->GetPriv();
+info.priviledges = admin->priv();
*cookie = GetCookie();
cookies[*cookie] = info;
STG::Admin * admin;
-if (admins->Find(adminInfo.admin, &admin))
+if (admins->find(adminInfo.admin, &admin))
{
*retvalPtr = xmlrpc_c::value_boolean(false);
return;
STG::Admin * admin;
-if (admins->Find(adminInfo.admin, &admin))
+if (admins->find(adminInfo.admin, &admin))
{
*retvalPtr = xmlrpc_c::value_boolean(false);
return;
STG::Admin * admin;
-if (admins->Find(adminInfo.admin, &admin))
+if (admins->find(adminInfo.admin, &admin))
{
*retvalPtr = xmlrpc_c::value_boolean(false);
return;
STG::Admin * admin = NULL;
-if (admins->Find(adminInfo.admin, &admin))
+if (admins->find(adminInfo.admin, &admin))
{
*retvalPtr = xmlrpc_c::value_boolean(false);
return;
STG::Admin * admin;
-if (admins->Find(adminInfo.admin, &admin))
+if (admins->find(adminInfo.admin, &admin))
{
*retvalPtr = xmlrpc_c::value_boolean(false);
return;
STG::Admin * admin;
-if (admins->Find(adminInfo.admin, &admin))
+if (admins->find(adminInfo.admin, &admin))
{
*retvalPtr = xmlrpc_c::value_boolean(false);
return;
STG::Admin * admin;
-if (admins->Find(adminInfo.admin, &admin))
+if (admins->find(adminInfo.admin, &admin))
{
*retvalPtr = xmlrpc_c::value_boolean(false);
return;
STG::Admin * admin;
-if (admins->Find(adminInfo.admin, &admin))
+if (admins->find(adminInfo.admin, &admin))
{
*retvalPtr = xmlrpc_c::value_boolean(false);
return;
STG::Admin * admin;
-if (admins->Find(adminInfo.admin, &admin))
+if (admins->find(adminInfo.admin, &admin))
{
*retvalPtr = xmlrpc_c::value_boolean(false);
return;
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() + ".");
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);
{
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;
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
}
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<int>(XML_GetCurrentLineNumber(state.conn.m_xmlParser)),
static_cast<int>(XML_GetCurrentColumnNumber(state.conn.m_xmlParser)),
{
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;
}
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;
}
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();
void GET_ADMINS::CreateAnswer()
{
- const auto priv = m_currAdmin.GetPriv();
- if (!priv->adminChg)
+ const auto& priv = m_currAdmin.priv();
+ if (!priv.adminChg)
{
m_answer = "<Error Result=\"Error. Access denied.\"/>";
return;
}
m_answer = "<Admins>";
- 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 += "<admin login=\"" + ac.login + "\" priv=\"" + std::to_string(p) + "\"/>";
- }
- 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 += "<admin login=\"" + admin.login() + "\" priv=\"" + std::to_string(p) + "\"/>";
+ });
m_answer += "</Admins>";
}
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)
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)
{
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();
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\"/>";
}
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);
if (m_users.FindByName(m_login, &u))
m_answer = "<User result=\"error\" reason=\"User not found.\"/>";
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)
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;
}
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;
}
}
bool GetValue(ObjectSyntax_t * objectSyntax) const override
{
- ValueToOS(admins.Count(), objectSyntax);
+ ValueToOS(admins.count(), objectSyntax);
return true;
}
int ServicesImpl::Add(const ServiceConf & service, const Admin * admin)
{
std::lock_guard<std::mutex> 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;
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;
}
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;
}
int ServicesImpl::Del(const std::string & name, const Admin * admin)
{
std::lock_guard<std::mutex> 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;
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;
}
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<std::mutex> 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;
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<unsigned>(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<unsigned>(si->payDay));
if (store->SaveService(service))
{
WriteServLog("Cannot write service %s.", service.name.c_str());
}
WriteServLog("%s Service \'%s\' changed.",
- admin->GetLogStr().c_str(), service.name.c_str());
+ admin->logStr().c_str(), service.name.c_str());
return 0;
}
//-----------------------------------------------------------------------------
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());
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;
}
}
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());
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;
}
}
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());
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;
}
}
WriteServLog("%s Tariff \'%s\' added.",
- admin->GetLogStr().c_str(), name.c_str());
+ admin->logStr().c_str(), name.c_str());
return 0;
}
//-----------------------------------------------------------------------------
UsersImpl::UsersImpl(SettingsImpl * s, Store * st,
Tariffs * t, Services & svcs,
- const Admin * sa)
+ const Admin& sa)
: settings(s),
tariffs(t),
m_services(svcs),
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);
u.WriteStat();
WriteServLog("%s User \'%s\' added.",
- admin->GetLogStr().c_str(), login.c_str());
+ admin->logStr().c_str(), login.c_str());
u.OnAdd();
//-----------------------------------------------------------------------------
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;
}
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;
}
DelUserFromIndexes(u);
WriteServLog("%s User \'%s\' deleted.",
- admin->GetLogStr().c_str(), login.c_str());
+ admin->logStr().c_str(), login.c_str());
}
}
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);
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;
Tariffs * tariffs;
Services & m_services;
Store * store;
- const Admin * sysAdmin;
+ const Admin& sysAdmin;
Logger & WriteServLog;
bool nonstop;
std::set<NotifierBase<UserImplPtr>*> onAddNotifiersImpl;
std::set<NotifierBase<UserImplPtr>*> 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);
-}*/
-//-----------------------------------------------------------------------------
}
#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"
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;
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;
#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"
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);
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);
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);
#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"
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;
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;
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;
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;
#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"
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;
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;
+++ /dev/null
-#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