X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/22cb2560274e05e6490cf964cbbeb74a1991c642..148f79dce19457e24754501db6844bc363c6f3fb:/projects/stargazer/admins_impl.cpp?ds=inline diff --git a/projects/stargazer/admins_impl.cpp b/projects/stargazer/admins_impl.cpp index 8f2760ce..1e597367 100644 --- a/projects/stargazer/admins_impl.cpp +++ b/projects/stargazer/admins_impl.cpp @@ -22,284 +22,202 @@ * Author : Boris Mikhailenko */ - /* - $Revision: 1.15 $ - $Date: 2010/10/04 20:17:12 $ - $Author: faust $ - */ +#include "admins_impl.h" #include "stg/common.h" -#include "admins_impl.h" -#include "admin_impl.h" -#include -#include -#include +using STG::AdminsImpl; //----------------------------------------------------------------------------- -ADMINS_IMPL::ADMINS_IMPL(STORE * st) - : ADMINS(), - stg(PRIV(0xFFFF), "@stargazer", ""), - noAdmin(PRIV(0xFFFF), "NO-ADMIN", ""), - data(), - store(st), - WriteServLog(GetStgLogger()), - 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 ADMINS_IMPL::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; } -ADMIN_IMPL 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 ADMINS_IMPL::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(), ADMIN_IMPL(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.remove(*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 ADMINS_IMPL::Change(const ADMIN_CONF & 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(), ADMIN_IMPL(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 ADMINS_IMPL::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) { - ADMIN_CONF 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(ADMIN_IMPL(ac)); + m_data.push_back(Admin(ac)); } -return 0; + + std::lock_guard lock(m_mutex); + m_data.swap(admins); } //----------------------------------------------------------------------------- -bool ADMINS_IMPL::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(), ADMIN_IMPL(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 ADMINS_IMPL::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(), ADMIN_IMPL(PRIV(0), login, ""))); - -if (ai != data.end()) return true; - -return false; } //----------------------------------------------------------------------------- -bool ADMINS_IMPL::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(), ADMIN_IMPL(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 ADMINS_IMPL::OpenSearch() const -{ -STG_LOCKER lock(&mutex); -handle++; -searchDescriptors[handle] = data.begin(); -return handle; + return find(login) != m_data.end(); } //----------------------------------------------------------------------------- -int ADMINS_IMPL::SearchNext(int h, ADMIN_CONF * 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; - -ADMIN_IMPL a = *searchDescriptors[h]++; + const auto it = find(login); -*ac = a.GetConf(); + if (it == m_data.end() || it->password() != password) + return false; -return 0; -} -//----------------------------------------------------------------------------- -int ADMINS_IMPL::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; } -//-----------------------------------------------------------------------------