]> git.stg.codes - stg.git/blobdiff - stargazer/admins_impl.cpp
Non-virtual admin.
[stg.git] / stargazer / admins_impl.cpp
index 4cb2095bf309454ad52658c93bc23f1e7b2a8174..1e597367ed05d9eb22d9b189c94c70434b69460d 100644 (file)
  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
  */
 
- /*
- $Revision: 1.15 $
- $Date: 2010/10/04 20:17:12 $
- $Author: faust $
- */
-
 #include "admins_impl.h"
-#include "admin_impl.h"
 
 #include "stg/common.h"
 
-#include <algorithm>
-#include <cerrno>
-#include <cassert>
-
 using STG::AdminsImpl;
 
 //-----------------------------------------------------------------------------
-AdminsImpl::AdminsImpl(Store * st)
-    : stg(Priv(0xFFFF), "@stargazer", ""),
-      noAdmin(Priv(0xFFFF), "NO-ADMIN", ""),
-      data(),
-      store(st),
-      WriteServLog(Logger::get()),
-      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 AdminsImpl::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;
     }
 
-AdminImpl adm(Priv(0), login, "");
-admin_iter ai(find(data.begin(), data.end(), adm));
+    std::lock_guard<std::mutex> 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 AdminsImpl::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(), AdminImpl(Priv(0), login, "")));
+    std::lock_guard<std::mutex> 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<int, const_admin_iter>::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.erase(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 AdminsImpl::Change(const AdminConf & 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(), AdminImpl(Priv(0), ac.login, "")));
+    std::lock_guard<std::mutex> 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 AdminsImpl::Read()
+void AdminsImpl::read()
 {
-STG_LOCKER lock(&mutex);
-std::vector<std::string> adminsList;
-if (store->GetAdminsList(&adminsList) < 0)
+    std::vector<std::string> 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<Admin> admins;
+    for (const auto& login : logins)
     {
-    AdminConf 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(AdminImpl(ac));
+        m_data.push_back(Admin(ac));
     }
-return 0;
+
+    std::lock_guard<std::mutex> lock(m_mutex);
+    m_data.swap(admins);
 }
 //-----------------------------------------------------------------------------
-bool AdminsImpl::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<std::mutex> 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(), AdminImpl(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 AdminsImpl::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(), AdminImpl(Priv(0), login, "")));
-
-if (ai != data.end())
     return true;
-
-return false;
 }
 //-----------------------------------------------------------------------------
-bool AdminsImpl::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(), AdminImpl(Priv(0), login, "")));
-
-if (ai == data.end())
+    std::lock_guard<std::mutex> 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 AdminsImpl::OpenSearch() const
-{
-STG_LOCKER lock(&mutex);
-handle++;
-searchDescriptors[handle] = data.begin();
-return handle;
+    return find(login) != m_data.end();
 }
 //-----------------------------------------------------------------------------
-int AdminsImpl::SearchNext(int h, AdminConf * 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<std::mutex> 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;
-
-AdminImpl a = *searchDescriptors[h]++;
+    const auto it = find(login);
 
-*ac = a.GetConf();
+    if (it == m_data.end() || it->password() != password)
+        return false;
 
-return 0;
-}
-//-----------------------------------------------------------------------------
-int AdminsImpl::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;
 }
-//-----------------------------------------------------------------------------