X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/becf6dfe4fe2ecd43792aa53a302c5866483f306..5e7cbf1bbd282c0b18a09c5e77e237a4e20f6e32:/projects/stargazer/admins_impl.cpp diff --git a/projects/stargazer/admins_impl.cpp b/projects/stargazer/admins_impl.cpp index 4e89c9fd..fb28b6e6 100644 --- a/projects/stargazer/admins_impl.cpp +++ b/projects/stargazer/admins_impl.cpp @@ -32,75 +32,75 @@ #include #include -#include "admins.h" -#include "admin.h" -#include "common.h" - -using namespace std; +#include "stg/common.h" +#include "admins_impl.h" +#include "admin_impl.h" //----------------------------------------------------------------------------- -ADMINS::ADMINS(BASE_STORE * st) - : stg(0xFFFF, "@stargazer", ""), +ADMINS_IMPL::ADMINS_IMPL(STORE * st) + : ADMINS(), + stg(0xFFFF, "@stargazer", ""), noAdmin(0xFFFF, "NO-ADMIN", ""), data(), store(st), WriteServLog(GetStgLogger()), searchDescriptors(), - handle(0) + handle(0), + mutex(), + strError() { pthread_mutex_init(&mutex, NULL); -ReadAdmins(); +Read(); } //----------------------------------------------------------------------------- -int ADMINS::Add(const string & login, const ADMIN & admin) +int ADMINS_IMPL::Add(const std::string & login, const ADMIN * admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -const PRIV * priv = admin.GetPriv(); +STG_LOCKER lock(&mutex); +const PRIV * priv = admin->GetPriv(); if (!priv->adminChg) { - string s = admin.GetLogStr() + " Add administrator \'" + login + "\'. Access denied."; + std::string s = admin->GetLogStr() + " Add administrator \'" + login + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; } -ADMIN adm(0, login, ""); +ADMIN_IMPL adm(0, login, ""); admin_iter ai(find(data.begin(), data.end(), adm)); if (ai != data.end()) { - strError = "Administrator \'" + login + "\' cannot not be added. Administrator alredy exist."; - WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str()); + strError = "Administrator \'" + login + "\' cannot not be added. Administrator already exist."; + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); return -1; } data.push_back(adm); -/*ADMIN_CONF ac; -ac.login = login;*/ -if (store->AddAdmin(login) == 0 /*&& store->SaveAdmin(ac) == 0*/) + +if (store->AddAdmin(login) == 0) { WriteServLog("%s Administrator \'%s\' added.", - admin.GetLogStr().c_str(), login.c_str()); + admin->GetLogStr().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()); +WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); return -1; } //----------------------------------------------------------------------------- -int ADMINS::Del(const string & login, const ADMIN & admin) +int ADMINS_IMPL::Del(const std::string & login, const ADMIN * admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -ADMIN adm(0, login, ""); -const PRIV * priv = admin.GetPriv(); +STG_LOCKER lock(&mutex); +ADMIN_IMPL adm(0, login, ""); +const PRIV * priv = admin->GetPriv(); if (!priv->adminChg) { - string s = admin.GetLogStr() + " Delete administrator \'" + login + "\'. Access denied."; + std::string s = admin->GetLogStr() + " Delete administrator \'" + login + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; @@ -111,11 +111,11 @@ 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()); + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); return -1; } -map::iterator si; +std::map::iterator si; si = searchDescriptors.begin(); while (si != searchDescriptors.end()) { @@ -128,35 +128,35 @@ data.remove(*ai); if (store->DelAdmin(login) < 0) { strError = "Administrator \'" + login + "\' was not deleted. Error: " + store->GetStrError(); - WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); return -1; } -WriteServLog("%s Administrator \'%s\' deleted.", admin.GetLogStr().c_str(), login.c_str()); +WriteServLog("%s Administrator \'%s\' deleted.", admin->GetLogStr().c_str(), login.c_str()); return 0; } //----------------------------------------------------------------------------- -int ADMINS::Change(const ADMIN_CONF & ac, const ADMIN & admin) +int ADMINS_IMPL::Change(const ADMIN_CONF & ac, const ADMIN * admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -const PRIV * priv = admin.GetPriv(); +STG_LOCKER lock(&mutex); +const PRIV * priv = admin->GetPriv(); if (!priv->adminChg) { - string s = admin.GetLogStr() + " Change administrator \'" + ac.login + "\'. Access denied."; + std::string s = admin->GetLogStr() + " Change administrator \'" + ac.login + "\'. Access denied."; strError = "Access denied."; WriteServLog(s.c_str()); return -1; } -ADMIN adm(0, ac.login, ""); +ADMIN_IMPL adm(0, ac.login, ""); admin_iter ai(find(data.begin(), data.end(), adm)); if (ai == data.end()) { strError = "Administrator \'" + ac.login + "\' cannot be changed " + ". Administrator does not exist."; - WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str()); + WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str()); return -1; } @@ -169,15 +169,15 @@ if (store->SaveAdmin(ac)) } WriteServLog("%s Administrator \'%s\' changed.", - admin.GetLogStr().c_str(), ac.login.c_str()); + admin->GetLogStr().c_str(), ac.login.c_str()); return 0; } //----------------------------------------------------------------------------- -int ADMINS::ReadAdmins() +int ADMINS_IMPL::Read() { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); -vector adminsList; +STG_LOCKER lock(&mutex); +std::vector adminsList; if (store->GetAdminsList(&adminsList) < 0) { WriteServLog(store->GetStrError().c_str()); @@ -194,56 +194,45 @@ for (unsigned int i = 0; i < adminsList.size(); i++) return -1; } - data.push_back(ADMIN(ac)); + data.push_back(ADMIN_IMPL(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++; - } -} -//----------------------------------------------------------------------------- -bool ADMINS::FindAdmin(const string & l, ADMIN * admin) const +bool ADMINS_IMPL::Find(const std::string & l, ADMIN ** admin) { assert(admin != NULL && "Pointer to admin is not null"); -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (data.empty()) { - printfd(__FILE__, "no admin in system!\n"); - *admin = noAdmin; + printfd(__FILE__, "No admin in system!\n"); + *admin = &noAdmin; return false; } -ADMIN adm(0, l, ""); -const_admin_iter ai(find(data.begin(), data.end(), adm)); +ADMIN_IMPL adm(0, l, ""); +admin_iter ai(find(data.begin(), data.end(), adm)); if (ai != data.end()) { - *admin = *ai; + *admin = &(*ai); return false; } return true; } //----------------------------------------------------------------------------- -bool ADMINS::AdminExists(const string & login) const +bool ADMINS_IMPL::Exists(const std::string & login) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (data.empty()) { printfd(__FILE__, "no admin in system!\n"); return true; } -ADMIN adm(0, login, ""); +ADMIN_IMPL adm(0, login, ""); const_admin_iter ai(find(data.begin(), data.end(), adm)); if (ai != data.end()) @@ -252,17 +241,17 @@ if (ai != data.end()) return false; } //----------------------------------------------------------------------------- -bool ADMINS::AdminCorrect(const string & login, const std::string & password, ADMIN * admin) const +bool ADMINS_IMPL::Correct(const std::string & login, const std::string & password, ADMIN ** admin) { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (data.empty()) { printfd(__FILE__, "no admin in system!\n"); return true; } -ADMIN adm(0, login, ""); -const_admin_iter ai(find(data.begin(), data.end(), adm)); +ADMIN_IMPL adm(0, login, ""); +admin_iter ai(find(data.begin(), data.end(), adm)); if (ai == data.end()) { @@ -274,22 +263,22 @@ if (ai->GetPassword() != password) return false; } -*admin = *ai; +*admin = &(*ai); return true; } //----------------------------------------------------------------------------- -int ADMINS::OpenSearch() const +int ADMINS_IMPL::OpenSearch() const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); handle++; searchDescriptors[handle] = data.begin(); return handle; } //----------------------------------------------------------------------------- -int ADMINS::SearchNext(int h, ADMIN_CONF * ac) const +int ADMINS_IMPL::SearchNext(int h, ADMIN_CONF * ac) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (searchDescriptors.find(h) == searchDescriptors.end()) { WriteServLog("ADMINS. Incorrect search handle."); @@ -299,16 +288,16 @@ if (searchDescriptors.find(h) == searchDescriptors.end()) if (searchDescriptors[h] == data.end()) return -1; -ADMIN a = *searchDescriptors[h]++; +ADMIN_IMPL a = *searchDescriptors[h]++; *ac = a.GetConf(); return 0; } //----------------------------------------------------------------------------- -int ADMINS::CloseSearch(int h) const +int ADMINS_IMPL::CloseSearch(int h) const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); if (searchDescriptors.find(h) != searchDescriptors.end()) { searchDescriptors.erase(searchDescriptors.find(h));