]> git.stg.codes - stg.git/blobdiff - projects/stargazer/plugins/configuration/rpcconfig/admins_methods.cpp
Use std::lock_guard instead of STG_LOCKER.
[stg.git] / projects / stargazer / plugins / configuration / rpcconfig / admins_methods.cpp
index 2dbc9a7d8405279bcf10cef09f89e967adb60f9e..d8fb0123f431e4c2a3be61cac1066b88b1d4b838 100644 (file)
@@ -1,7 +1,14 @@
-#include "stg/common.h"
 #include "admins_methods.h"
 #include "rpcconfig.h"
 
+#include "stg/common.h"
+
+#include "stg/admins.h"
+#include "stg/admin.h"
+#include "stg/admin_conf.h"
+
+#include <ostream> // xmlrpc-c devs have missed something :)
+
 //------------------------------------------------------------------------------
 
 void METHOD_ADMIN_GET::execute(xmlrpc_c::paramList const & paramList,
@@ -21,9 +28,9 @@ if (config->GetAdminInfo(cookie, &adminInfo))
     return;
     }
 
-ADMIN * admin;
+STG::Admin * admin;
 
-if (admins->FindAdmin(login, &admin))
+if (admins->find(login, &admin))
     {
     structVal["result"] = xmlrpc_c::value_boolean(false);
     *retvalPtr = xmlrpc_c::value_struct(structVal);
@@ -31,18 +38,18 @@ if (admins->FindAdmin(login, &admin))
     }
 
 structVal["result"] = xmlrpc_c::value_boolean(true);
-structVal["login"] = xmlrpc_c::value_string(admin->GetLogin());
-structVal["password"] = xmlrpc_c::value_string(admin->GetPassword());
+structVal["login"] = xmlrpc_c::value_string(admin->login());
+structVal["password"] = xmlrpc_c::value_string(admin->password());
 
-const PRIV * priv = admin->GetPriv();
+const auto priv = admin->priv();
 
-structVal["user_stat"] = xmlrpc_c::value_boolean(priv->userStat);
-structVal["user_conf"] = xmlrpc_c::value_boolean(priv->userConf);
-structVal["user_cash"] = xmlrpc_c::value_boolean(priv->userCash);
-structVal["user_passwd"] = xmlrpc_c::value_boolean(priv->userPasswd);
-structVal["user_add_del"] = xmlrpc_c::value_boolean(priv->userAddDel);
-structVal["admin_chg"] = xmlrpc_c::value_boolean(priv->adminChg);
-structVal["tariff_chg"] = xmlrpc_c::value_boolean(priv->tariffChg);
+structVal["user_stat"] = xmlrpc_c::value_boolean(priv.userStat);
+structVal["user_conf"] = xmlrpc_c::value_boolean(priv.userConf);
+structVal["user_cash"] = xmlrpc_c::value_boolean(priv.userCash);
+structVal["user_passwd"] = xmlrpc_c::value_boolean(priv.userPasswd);
+structVal["user_add_del"] = xmlrpc_c::value_boolean(priv.userAddDel);
+structVal["admin_chg"] = xmlrpc_c::value_boolean(priv.adminChg);
+structVal["tariff_chg"] = xmlrpc_c::value_boolean(priv.tariffChg);
 
 *retvalPtr = xmlrpc_c::value_struct(structVal);
 }
@@ -65,16 +72,16 @@ if (config->GetAdminInfo(cookie, &adminInfo))
     return;
     }
 
-ADMIN * admin;
+STG::Admin * admin;
 
-if (admins->FindAdmin(adminInfo.admin, &admin))
+if (admins->find(adminInfo.admin, &admin))
     {
     printfd(__FILE__, "METHOD_ADMIN_ADD::execute(): 'Invalid admin (logged)'\n");
     *retvalPtr = xmlrpc_c::value_boolean(false);
     return;
     }
 
-if (admins->Add(login, admin))
+if (admins->add(login, *admin))
     {
     printfd(__FILE__, "METHOD_ADMIN_ADD::execute(): 'Failed to add admin'\n");
     *retvalPtr = xmlrpc_c::value_boolean(false);
@@ -93,7 +100,6 @@ std::string cookie = paramList.getString(0);
 std::string login = paramList.getString(1);
 paramList.verifyEnd(2);
 
-std::map<std::string, xmlrpc_c::value> structVal;
 ADMIN_INFO adminInfo;
 
 if (config->GetAdminInfo(cookie, &adminInfo))
@@ -102,15 +108,15 @@ if (config->GetAdminInfo(cookie, &adminInfo))
     return;
     }
 
-ADMIN * admin;
+STG::Admin * admin;
 
-if (admins->FindAdmin(adminInfo.admin, &admin))
+if (admins->find(adminInfo.admin, &admin))
     {
     *retvalPtr = xmlrpc_c::value_boolean(false);
     return;
     }
 
-if (admins->Del(login, admin))
+if (admins->del(login, *admin))
     {
     *retvalPtr = xmlrpc_c::value_boolean(false);
     return;
@@ -137,31 +143,29 @@ if (config->GetAdminInfo(cookie, &adminInfo))
     return;
     }
 
-ADMIN * loggedAdmin;
+STG::Admin * loggedAdmin;
 
-if (admins->FindAdmin(adminInfo.admin, &loggedAdmin))
+if (admins->find(adminInfo.admin, &loggedAdmin))
     {
     *retvalPtr = xmlrpc_c::value_boolean(false);
     return;
     }
 
-ADMIN * admin;
+STG::Admin * admin;
 
-if (admins->FindAdmin(login, &admin))
+if (admins->find(login, &admin))
     {
     *retvalPtr = xmlrpc_c::value_boolean(false);
     return;
     }
 
-ADMIN_CONF conf;
+STG::AdminConf conf;
 
-conf.priv = *admin->GetPriv();
-conf.password = admin->GetPassword();
+conf.priv = admin->priv();
+conf.password = admin->password();
 conf.login = login;
 
-std::map<std::string, xmlrpc_c::value> structVal(
-    static_cast<std::map<std::string, xmlrpc_c::value> >(xmlrpc_c::value_struct(info))
-    );
+std::map<std::string, xmlrpc_c::value> structVal = info;
 
 std::map<std::string, xmlrpc_c::value>::iterator it;
 
@@ -205,7 +209,7 @@ if ((it = structVal.find("tariff_chg")) != structVal.end())
     conf.priv.tariffChg = xmlrpc_c::value_boolean(it->second);
     }
 
-if (admins->Change(conf, loggedAdmin))
+if (admins->change(conf, *loggedAdmin))
     {
     *retvalPtr = xmlrpc_c::value_boolean(false);
     }
@@ -221,36 +225,33 @@ void METHOD_ADMINS_GET::execute(xmlrpc_c::paramList const & paramList,
 std::string cookie = paramList.getString(0);
 paramList.verifyEnd(1);
 
-std::map<std::string, xmlrpc_c::value> structVal;
+std::map<std::string, xmlrpc_c::value> mainStructVal;
 std::vector<xmlrpc_c::value> retval;
 ADMIN_INFO adminInfo;
 
 if (config->GetAdminInfo(cookie, &adminInfo))
     {
-    structVal["result"] = xmlrpc_c::value_boolean(false);
-    *retvalPtr = xmlrpc_c::value_struct(structVal);
+    mainStructVal["result"] = xmlrpc_c::value_boolean(false);
+    *retvalPtr = xmlrpc_c::value_struct(mainStructVal);
     return;
     }
 
-ADMIN_CONF ac;
-int h = admins->OpenSearch();
-
-while (admins->SearchNext(h, &ac) == 0)
+admins->fmap([&retval](const auto& admin)
     {
-    std::map<std::string, xmlrpc_c::value> structVal;
-    structVal["result"] = xmlrpc_c::value_boolean(true);
-    structVal["login"] = xmlrpc_c::value_string(ac.login);
-    structVal["password"] = xmlrpc_c::value_string(ac.password);
-    structVal["user_stat"] = xmlrpc_c::value_boolean(ac.priv.userStat);
-    structVal["user_conf"] = xmlrpc_c::value_boolean(ac.priv.userConf);
-    structVal["user_cash"] = xmlrpc_c::value_boolean(ac.priv.userCash);
-    structVal["user_passwd"] = xmlrpc_c::value_boolean(ac.priv.userPasswd);
-    structVal["user_add_del"] = xmlrpc_c::value_boolean(ac.priv.userAddDel);
-    structVal["admin_chg"] = xmlrpc_c::value_boolean(ac.priv.adminChg);
-    structVal["tariff_chg"] = xmlrpc_c::value_boolean(ac.priv.tariffChg);
-
-    retval.push_back(xmlrpc_c::value_struct(structVal));
-    }
+        const std::map<std::string, xmlrpc_c::value> structVal{
+            {"result", xmlrpc_c::value_boolean(true)},
+            {"login", xmlrpc_c::value_string(admin.login())},
+            {"password", xmlrpc_c::value_string(admin.password())},
+            {"user_stat", xmlrpc_c::value_boolean(admin.priv().userStat)},
+            {"user_conf", xmlrpc_c::value_boolean(admin.priv().userConf)},
+            {"user_cash", xmlrpc_c::value_boolean(admin.priv().userCash)},
+            {"user_passwd", xmlrpc_c::value_boolean(admin.priv().userPasswd)},
+            {"user_add_del", xmlrpc_c::value_boolean(admin.priv().userAddDel)},
+            {"admin_chg", xmlrpc_c::value_boolean(admin.priv().adminChg)},
+            {"tariff_chg", xmlrpc_c::value_boolean(admin.priv().tariffChg)}
+        };
+        retval.push_back(xmlrpc_c::value_struct(structVal));
+    });
 
 *retvalPtr = xmlrpc_c::value_array(retval);
 }