X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/6f7160dd027c8ce7e2760aa369768ba39ab80181..30a838da28667633e16040250680b793dee271c7:/projects/stargazer/admins_impl.cpp?ds=inline diff --git a/projects/stargazer/admins_impl.cpp b/projects/stargazer/admins_impl.cpp index 20bd8ef8..8f2760ce 100644 --- a/projects/stargazer/admins_impl.cpp +++ b/projects/stargazer/admins_impl.cpp @@ -28,52 +28,51 @@ $Author: faust $ */ -#include -#include -#include - +#include "stg/common.h" #include "admins_impl.h" -#include "admins.h" -#include "admin.h" #include "admin_impl.h" -#include "common.h" -using namespace std; +#include +#include +#include //----------------------------------------------------------------------------- -ADMINS_IMPL::ADMINS_IMPL(BASE_STORE * st) - : stg(0xFFFF, "@stargazer", ""), - noAdmin(0xFFFF, "NO-ADMIN", ""), +ADMINS_IMPL::ADMINS_IMPL(STORE * st) + : ADMINS(), + stg(PRIV(0xFFFF), "@stargazer", ""), + noAdmin(PRIV(0xFFFF), "NO-ADMIN", ""), data(), store(st), WriteServLog(GetStgLogger()), searchDescriptors(), - handle(0) + handle(0), + mutex(), + strError() { pthread_mutex_init(&mutex, NULL); -ReadAdmins(); +Read(); } //----------------------------------------------------------------------------- -int ADMINS_IMPL::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_IMPL adm(0, login, ""); +ADMIN_IMPL adm(PRIV(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; } @@ -83,81 +82,79 @@ data.push_back(adm); 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_IMPL::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_IMPL adm(0, login, ""); -const PRIV * priv = admin.GetPriv(); +STG_LOCKER lock(&mutex); +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; } -admin_iter ai(find(data.begin(), data.end(), adm)); +admin_iter ai(find(data.begin(), data.end(), ADMIN_IMPL(PRIV(0), login, ""))); 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()) { if (si->second == ai) (si->second)++; - si++; + ++si; } 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_IMPL::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_IMPL adm(0, ac.login, ""); -admin_iter ai(find(data.begin(), data.end(), adm)); +admin_iter ai(find(data.begin(), data.end(), ADMIN_IMPL(PRIV(0), ac.login, ""))); 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; } @@ -170,15 +167,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_IMPL::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()); @@ -187,7 +184,7 @@ if (store->GetAdminsList(&adminsList) < 0) for (unsigned int i = 0; i < adminsList.size(); i++) { - ADMIN_CONF ac(0, adminsList[i], ""); + ADMIN_CONF ac(PRIV(0), adminsList[i], ""); if (store->RestoreAdmin(&ac, adminsList[i])) { @@ -200,31 +197,19 @@ for (unsigned int i = 0; i < adminsList.size(); i++) 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++; - } -} -//----------------------------------------------------------------------------- -bool ADMINS_IMPL::FindAdmin(const string & l, ADMIN ** admin) +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"); + printfd(__FILE__, "No admin in system!\n"); *admin = &noAdmin; return false; } -ADMIN_IMPL adm(0, l, ""); -admin_iter ai(find(data.begin(), data.end(), adm)); +admin_iter ai(find(data.begin(), data.end(), ADMIN_IMPL(PRIV(0), l, ""))); if (ai != data.end()) { @@ -235,17 +220,16 @@ if (ai != data.end()) return true; } //----------------------------------------------------------------------------- -bool ADMINS_IMPL::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_IMPL adm(0, login, ""); -const_admin_iter ai(find(data.begin(), data.end(), adm)); +const_admin_iter ai(find(data.begin(), data.end(), ADMIN_IMPL(PRIV(0), login, ""))); if (ai != data.end()) return true; @@ -253,17 +237,16 @@ if (ai != data.end()) return false; } //----------------------------------------------------------------------------- -bool ADMINS_IMPL::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_IMPL adm(0, login, ""); -const_admin_iter ai(find(data.begin(), data.end(), adm)); +admin_iter ai(find(data.begin(), data.end(), ADMIN_IMPL(PRIV(0), login, ""))); if (ai == data.end()) { @@ -275,14 +258,14 @@ if (ai->GetPassword() != password) return false; } -*admin = *ai; +*admin = &(*ai); return true; } //----------------------------------------------------------------------------- int ADMINS_IMPL::OpenSearch() const { -STG_LOCKER lock(&mutex, __FILE__, __LINE__); +STG_LOCKER lock(&mutex); handle++; searchDescriptors[handle] = data.begin(); return handle; @@ -290,7 +273,7 @@ return handle; //----------------------------------------------------------------------------- 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."); @@ -309,7 +292,7 @@ return 0; //----------------------------------------------------------------------------- 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));