X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/becf6dfe4fe2ecd43792aa53a302c5866483f306..38f134e6a96c08fdd7d4ed07ca3550e8434d56e9:/projects/stargazer/admins_impl.cpp diff --git a/projects/stargazer/admins_impl.cpp b/projects/stargazer/admins_impl.cpp index 4e89c9fd..1e597367 100644 --- a/projects/stargazer/admins_impl.cpp +++ b/projects/stargazer/admins_impl.cpp @@ -22,300 +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 "common.h" +#include "stg/common.h" -using namespace std; +using STG::AdminsImpl; //----------------------------------------------------------------------------- -ADMINS::ADMINS(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::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 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); -/*ADMIN_CONF ac; -ac.login = login;*/ -if (store->AddAdmin(login) == 0 /*&& store->SaveAdmin(ac) == 0*/) + m_data.push_back(Admin(Priv(0), login, {})); + + 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::Del(const string & login, const ADMIN & admin) +int AdminsImpl::del(const std::string& login, const Admin& admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -ADMIN 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::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 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::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(ac)); - } -return 0; -} -//----------------------------------------------------------------------------- -void ADMINS::PrintAdmins() const -{ -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -const_admin_iter ai(data.begin()); -while (ai != data.end()) - { - ai->PrintAdmin(); - ai++; + m_data.push_back(Admin(ac)); } + + std::lock_guard lock(m_mutex); + m_data.swap(admins); } //----------------------------------------------------------------------------- -bool ADMINS::FindAdmin(const string & l, ADMIN * admin) const +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 adm(0, l, ""); -const_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::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 adm(0, login, ""); -const_admin_iter ai(find(data.begin(), data.end(), adm)); - -if (ai != data.end()) return true; - -return false; } //----------------------------------------------------------------------------- -bool ADMINS::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()) + std::lock_guard lock(m_mutex); + if (m_data.empty()) { - printfd(__FILE__, "no admin in system!\n"); - return true; + printfd(__FILE__, "No admin in system!\n"); + return true; } -ADMIN adm(0, login, ""); -const_admin_iter ai(find(data.begin(), data.end(), adm)); - -if (ai == data.end()) - { - return false; - } - -if (ai->GetPassword() != password) - { - return false; - } - -*admin = *ai; - -return true; + return find(login) != m_data.end(); } //----------------------------------------------------------------------------- -int ADMINS::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::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; + const auto it = find(login); -ADMIN a = *searchDescriptors[h]++; + if (it == m_data.end() || it->password() != password) + return false; -*ac = a.GetConf(); + if (admin != nullptr) + *admin = &(*it); -return 0; -} -//----------------------------------------------------------------------------- -int ADMINS::CloseSearch(int h) const -{ -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -if (searchDescriptors.find(h) != searchDescriptors.end()) - { - searchDescriptors.erase(searchDescriptors.find(h)); - return 0; - } - -WriteServLog("ADMINS. Incorrect search handle."); -return -1; + return true; } -//-----------------------------------------------------------------------------