X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/6f7160dd027c8ce7e2760aa369768ba39ab80181..296c906e2689b9b5dc26d2f2d3e1e2d9e1662d3b:/projects/stargazer/admins_impl.cpp?ds=sidebyside diff --git a/projects/stargazer/admins_impl.cpp b/projects/stargazer/admins_impl.cpp index 20bd8ef8..1e597367 100644 --- a/projects/stargazer/admins_impl.cpp +++ b/projects/stargazer/admins_impl.cpp @@ -22,301 +22,202 @@ * Author : Boris Mikhailenko */ - /* - $Revision: 1.15 $ - $Date: 2010/10/04 20:17:12 $ - $Author: faust $ - */ - -#include -#include -#include - #include "admins_impl.h" -#include "admins.h" -#include "admin.h" -#include "admin_impl.h" -#include "common.h" -using namespace std; +#include "stg/common.h" + +using STG::AdminsImpl; //----------------------------------------------------------------------------- -ADMINS_IMPL::ADMINS_IMPL(BASE_STORE * st) - : stg(0xFFFF, "@stargazer", ""), - noAdmin(0xFFFF, "NO-ADMIN", ""), - data(), - store(st), - WriteServLog(GetStgLogger()), - searchDescriptors(), - handle(0) +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); -ReadAdmins(); + read(); } //----------------------------------------------------------------------------- -int ADMINS_IMPL::Add(const string & login, const ADMIN & admin) +int AdminsImpl::add(const std::string& login, const Admin& admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -const PRIV * priv = admin.GetPriv(); - -if (!priv->adminChg) + if (!admin.priv().adminChg) { - 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(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 alredy 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 string & login, const ADMIN & admin) +int AdminsImpl::del(const std::string& login, const Admin& admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -ADMIN_IMPL adm(0, login, ""); -const PRIV * priv = admin.GetPriv(); - -if (!priv->adminChg) + if (!admin.priv().adminChg) { - 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(), adm)); - -if (ai == 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::lock_guard lock(m_mutex); + const auto it = find(login); -map::iterator si; -si = searchDescriptors.begin(); -while (si != searchDescriptors.end()) + if (it == m_data.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, __FILE__, __LINE__); -const PRIV * priv = admin.GetPriv(); - -if (!priv->adminChg) + if (!admin.priv().adminChg) { - 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_IMPL adm(0, ac.login, ""); -admin_iter ai(find(data.begin(), data.end(), adm)); + 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::ReadAdmins() +void AdminsImpl::read() { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -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(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)); - } -return 0; -} -//----------------------------------------------------------------------------- -void ADMINS_IMPL::PrintAdmins() const -{ -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -const_admin_iter ai(data.begin()); -while (ai != data.end()) - { - ai->Print(); - ai++; + m_data.push_back(Admin(ac)); } + + std::lock_guard lock(m_mutex); + m_data.swap(admins); } //----------------------------------------------------------------------------- -bool ADMINS_IMPL::FindAdmin(const 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, __FILE__, __LINE__); -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_IMPL adm(0, l, ""); -admin_iter ai(find(data.begin(), data.end(), adm)); + auto it = find(login); -if (ai != data.end()) - { - *admin = &(*ai); - return false; - } - -return true; -} -//----------------------------------------------------------------------------- -bool ADMINS_IMPL::AdminExists(const string & login) const -{ -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -if (data.empty()) + if (it != m_data.end()) { - printfd(__FILE__, "no admin in system!\n"); - return true; + if (admin != nullptr) + *admin = &(*it); + return false; } -ADMIN_IMPL adm(0, login, ""); -const_admin_iter ai(find(data.begin(), data.end(), adm)); - -if (ai != data.end()) return true; - -return false; } //----------------------------------------------------------------------------- -bool ADMINS_IMPL::AdminCorrect(const string & login, const std::string & password, ADMIN * admin) const +bool AdminsImpl::exists(const std::string& login) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -if (data.empty()) - { - printfd(__FILE__, "no admin in system!\n"); - return true; - } - -ADMIN_IMPL adm(0, login, ""); -const_admin_iter ai(find(data.begin(), data.end(), adm)); - -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; + return find(login) != m_data.end(); } //----------------------------------------------------------------------------- -int ADMINS_IMPL::OpenSearch() const +bool AdminsImpl::correct(const std::string& login, const std::string& password, Admin** admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -handle++; -searchDescriptors[handle] = data.begin(); -return handle; -} -//----------------------------------------------------------------------------- -int ADMINS_IMPL::SearchNext(int h, ADMIN_CONF * ac) const -{ -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -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, __FILE__, __LINE__); -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; } -//-----------------------------------------------------------------------------