]> git.stg.codes - stg.git/blobdiff - projects/sgconf/admins.cpp
Fixed tag named, keep old values using Splice.
[stg.git] / projects / sgconf / admins.cpp
index e756618ba9fd19b9ab3261c667bef036cbda373f..c16e6e2b2d981dfb1897e585c8fa209b00f6be25 100644 (file)
@@ -3,6 +3,7 @@
 #include "api_action.h"
 #include "options.h"
 #include "config.h"
 #include "api_action.h"
 #include "options.h"
 #include "config.h"
+#include "utils.h"
 
 #include "stg/servconf.h"
 #include "stg/servconf_types.h"
 
 #include "stg/servconf.h"
 #include "stg/servconf_types.h"
@@ -43,6 +44,31 @@ std::cout << Indent(level, true) << "login: " << info.login << "\n"
           << Indent(level)       << "priviledges: " << PrivToString(info.priv) << "\n";
 }
 
           << Indent(level)       << "priviledges: " << PrivToString(info.priv) << "\n";
 }
 
+std::vector<SGCONF::API_ACTION::PARAM> GetAdminParams()
+{
+std::vector<SGCONF::API_ACTION::PARAM> params;
+params.push_back(SGCONF::API_ACTION::PARAM("password", "<password>", "password"));
+params.push_back(SGCONF::API_ACTION::PARAM("priv", "<priv>", "priviledges"));
+return params;
+}
+
+void ConvPriv(const std::string & value, RESETABLE<PRIV> & res)
+{
+if (value.length() != 9)
+    throw SGCONF::ACTION::ERROR("Priviledges value should be a 9-digits length binary number.");
+PRIV priv;
+priv.corpChg = (value[0] == '0' ? 0 : 1);
+priv.serviceChg = (value[1] == '0' ? 0 : 1);
+priv.tariffChg = (value[2] == '0' ? 0 : 1);
+priv.adminChg = (value[3] == '0' ? 0 : 1);
+priv.userAddDel = (value[4] == '0' ? 0 : 1);
+priv.userPasswd = (value[5] == '0' ? 0 : 1);
+priv.userCash = (value[6] == '0' ? 0 : 1);
+priv.userConf = (value[7] == '0' ? 0 : 1);
+priv.userStat = (value[8] == '0' ? 0 : 1);
+res = priv;
+}
+
 void SimpleCallback(bool result,
                     const std::string & reason,
                     void * /*data*/)
 void SimpleCallback(bool result,
                     const std::string & reason,
                     void * /*data*/)
@@ -94,6 +120,8 @@ bool GetAdminsFunction(const SGCONF::CONFIG & config,
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
+                    config.localAddress.data(),
+                    config.localPort.data(),
                     config.userName.data(),
                     config.userPass.data());
 return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
                     config.userName.data(),
                     config.userPass.data());
 return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
@@ -105,6 +133,8 @@ bool GetAdminFunction(const SGCONF::CONFIG & config,
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
+                    config.localAddress.data(),
+                    config.localPort.data(),
                     config.userName.data(),
                     config.userPass.data());
 // STG currently doesn't support <GetAdmin login="..."/>.
                     config.userName.data(),
                     config.userPass.data());
 // STG currently doesn't support <GetAdmin login="..."/>.
@@ -119,6 +149,8 @@ bool DelAdminFunction(const SGCONF::CONFIG & config,
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
 {
 STG::SERVCONF proto(config.server.data(),
                     config.port.data(),
+                    config.localAddress.data(),
+                    config.localPort.data(),
                     config.userName.data(),
                     config.userPass.data());
 return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
                     config.userName.data(),
                     config.userPass.data());
 return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
@@ -126,30 +158,47 @@ return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
 
 bool AddAdminFunction(const SGCONF::CONFIG & config,
                       const std::string & arg,
 
 bool AddAdminFunction(const SGCONF::CONFIG & config,
                       const std::string & arg,
-                      const std::map<std::string, std::string> & /*options*/)
+                      const std::map<std::string, std::string> & options)
 {
 {
-// TODO
-std::cerr << "Unimplemented.\n";
-return false;
+ADMIN_CONF_RES conf;
+conf.login = arg;
+SGCONF::MaybeSet(options, "priv", conf.priv, ConvPriv);
+SGCONF::MaybeSet(options, "password", conf.password);
+STG::SERVCONF proto(config.server.data(),
+                    config.port.data(),
+                    config.localAddress.data(),
+                    config.localPort.data(),
+                    config.userName.data(),
+                    config.userPass.data());
+return proto.AddAdmin(arg, conf, SimpleCallback, NULL) == STG::st_ok;
 }
 
 bool ChgAdminFunction(const SGCONF::CONFIG & config,
                       const std::string & arg,
                       const std::map<std::string, std::string> & options)
 {
 }
 
 bool ChgAdminFunction(const SGCONF::CONFIG & config,
                       const std::string & arg,
                       const std::map<std::string, std::string> & options)
 {
-// TODO
-std::cerr << "Unimplemented.\n";
-return false;
+ADMIN_CONF_RES conf;
+conf.login = arg;
+SGCONF::MaybeSet(options, "priv", conf.priv, ConvPriv);
+SGCONF::MaybeSet(options, "password", conf.password);
+STG::SERVCONF proto(config.server.data(),
+                    config.port.data(),
+                    config.localAddress.data(),
+                    config.localPort.data(),
+                    config.userName.data(),
+                    config.userPass.data());
+return proto.ChgAdmin(conf, SimpleCallback, NULL) == STG::st_ok;
 }
 
 } // namespace anonymous
 
 void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
 {
 }
 
 } // namespace anonymous
 
 void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
 {
+std::vector<API_ACTION::PARAM> params(GetAdminParams());
 blocks.Add("Admin management options")
       .Add("get-admins", SGCONF::MakeAPIAction(commands, GetAdminsFunction), "\tget admin list")
 blocks.Add("Admin management options")
       .Add("get-admins", SGCONF::MakeAPIAction(commands, GetAdminsFunction), "\tget admin list")
-      .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", true, GetAdminFunction), "get admin")
-      .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", true, AddAdminFunction), "add admin")
-      .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", true, DelAdminFunction), "del admin")
-      .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", true, ChgAdminFunction), "change admin");
+      .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", GetAdminFunction), "get admin")
+      .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", params, AddAdminFunction), "add admin")
+      .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", DelAdminFunction), "del admin")
+      .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", params, ChgAdminFunction), "change admin");
 }
 }