./common_sg.cpp \
./options.cpp \
./actions.cpp \
+ ./admins.cpp \
./xml.cpp
STGLIBS = conffiles \
--- /dev/null
+#include "admins.h"
+
+#include "config.h"
+
+#include "stg/servconf.h"
+#include "stg/servconf_types.h"
+#include "stg/os_int.h"
+
+#include <iostream>
+#include <cassert>
+
+namespace
+{
+
+std::string Indent(size_t level, bool dash = false)
+{
+if (level == 0)
+ return "";
+return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
+}
+
+std::string PrivToString(const PRIV& priv)
+{
+return std::string("") +
+ (priv.corpChg ? "1" : "0") +
+ (priv.serviceChg ? "1" : "0") +
+ (priv.tariffChg ? "1" : "0") +
+ (priv.adminChg ? "1" : "0") +
+ (priv.userAddDel ? "1" : "0") +
+ (priv.userPasswd ? "1" : "0") +
+ (priv.userCash ? "1" : "0") +
+ (priv.userConf ? "1" : "0") +
+ (priv.userStat ? "1" : "0");
+}
+
+void PrintAdmin(const STG::GET_ADMIN::INFO & info, size_t level = 0)
+{
+std::cout << Indent(level, true) << "login: " << info.login << "\n"
+ << Indent(level) << "priviledges: " << PrivToString(info.priv) << "\n";
+}
+
+void SimpleCallback(bool result,
+ const std::string & reason,
+ void * /*data*/)
+{
+if (!result)
+ {
+ std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
+ return;
+ }
+std::cout << "Success.\n";
+}
+
+void GetAdminsCallback(bool result,
+ const std::string & reason,
+ const std::vector<STG::GET_ADMIN::INFO> & info,
+ void * /*data*/)
+{
+if (!result)
+ {
+ std::cerr << "Failed to get admin list. Reason: '" << reason << "'." << std::endl;
+ return;
+ }
+std::cout << "Admins:\n";
+for (size_t i = 0; i < info.size(); ++i)
+ PrintAdmin(info[i], 1);
+}
+
+void GetAdminCallback(bool result,
+ const std::string & reason,
+ const std::vector<STG::GET_ADMIN::INFO> & info,
+ void * data)
+{
+assert(data != NULL && "Expecting pointer to std::string with the admin's login.");
+const std::string & login = *static_cast<const std::string *>(data);
+if (!result)
+ {
+ std::cerr << "Failed to get admin. Reason: '" << reason << "'." << std::endl;
+ return;
+ }
+for (size_t i = 0; i < info.size(); ++i)
+ if (info[i].login == login)
+ PrintAdmin(info[i]);
+}
+
+}
+
+
+bool SGCONF::GetAdminsFunction(const SGCONF::CONFIG & config,
+ const std::string & /*arg*/,
+ const std::map<std::string, std::string> & /*options*/)
+{
+STG::SERVCONF proto(config.server.data(),
+ config.port.data(),
+ config.userName.data(),
+ config.userPass.data());
+return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
+}
+
+bool SGCONF::GetAdminFunction(const SGCONF::CONFIG & config,
+ const std::string & arg,
+ const std::map<std::string, std::string> & /*options*/)
+{
+STG::SERVCONF proto(config.server.data(),
+ config.port.data(),
+ config.userName.data(),
+ config.userPass.data());
+// STG currently doesn't support <GetAdmin login="..."/>.
+// So get a list of admins and filter it. 'data' param holds a pointer to 'login'.
+std::string login(arg);
+return proto.GetAdmins(GetAdminCallback, &login) == STG::st_ok;
+}
+
+bool SGCONF::DelAdminFunction(const SGCONF::CONFIG & config,
+ const std::string & arg,
+ const std::map<std::string, std::string> & /*options*/)
+{
+STG::SERVCONF proto(config.server.data(),
+ config.port.data(),
+ config.userName.data(),
+ config.userPass.data());
+return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
+}
+
+bool SGCONF::AddAdminFunction(const SGCONF::CONFIG & config,
+ const std::string & arg,
+ const std::map<std::string, std::string> & /*options*/)
+{
+return false;
+}
+
+bool SGCONF::ChgAdminFunction(const SGCONF::CONFIG & config,
+ const std::string & arg,
+ const std::map<std::string, std::string> & options)
+{
+return false;
+}
--- /dev/null
+#ifndef __STG_SGCONF_ADMINS_H__
+#define __STG_SGCONF_ADMINS_H__
+
+#include <string>
+#include <map>
+
+namespace SGCONF
+{
+
+class CONFIG;
+
+bool GetAdminsFunction(const CONFIG & config,
+ const std::string & /*arg*/,
+ const std::map<std::string, std::string> & /*options*/);
+
+bool GetAdminFunction(const CONFIG & config,
+ const std::string & arg,
+ const std::map<std::string, std::string> & /*options*/);
+
+bool DelAdminFunction(const CONFIG & config,
+ const std::string & arg,
+ const std::map<std::string, std::string> & /*options*/);
+
+bool AddAdminFunction(const CONFIG & config,
+ const std::string & arg,
+ const std::map<std::string, std::string> & options);
+
+bool ChgAdminFunction(const CONFIG & config,
+ const std::string & arg,
+ const std::map<std::string, std::string> & options);
+
+} // namespace SGCONF
+
+#endif
}
};
-}
+} // namespace SGCONF
#endif
#include "sg_error_codes.h"
#include "xml.h"
+#include "admins.h"
#include "options.h"
#include "actions.h"
#include "config.h"
return new API_ACTION(commands, paramDescription, needArgument, funPtr);
}
+inline
+ACTION * MakeAPIAction(COMMANDS & commands,
+ API_FUNCTION funPtr)
+{
+return new API_ACTION(commands, "", false, funPtr);
+}
+
bool RawXMLFunction(const SGCONF::CONFIG & config,
const std::string & arg,
const std::map<std::string, std::string> & /*options*/)
.Add("a", "address", SGCONF::MakeParamAction(config, "<connection string>"), "connection params as a single string in format: <login>:<password>@<host>:<port>");
blocks.Add("Raw XML")
.Add("r", "raw", SGCONF::MakeAPIAction(commands, "<xml>", true, SGCONF::RawXMLFunction), "\tmake raw XML request");
-/*blocks.Add("Admins management options")
- .Add("get-admins", SGCONF::MakeConfAction())
- .Add("get-admin", SGCONF::MakeConfAction())
- .Add("add-admin", SGCONF::MakeConfAction())
- .Add("del-admin", SGCONF::MakeConfAction())
- .Add("chg-admin", SGCONF::MakeConfAction());*/
+blocks.Add("Admins management options")
+ .Add("get-admins", SGCONF::MakeAPIAction(commands, SGCONF::GetAdminsFunction), "\tget admin list")
+ .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::GetAdminFunction), "\tget admin")
+ .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::AddAdminFunction), "\tadd admin")
+ .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::DelAdminFunction), "\tdel admin")
+ .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", true, SGCONF::ChgAdminFunction), "\tchange admin");
SGCONF::PARSER_STATE state(false, argc, argv);
template <>
inline
-bool GetValue<PRIV>(const char ** attr, PRIV & value)
+bool GetValue<PRIV>(const char ** attr, PRIV & value, const std::string & attrName)
{
uint32_t priv;
-if (!GetValue(attr, priv))
+if (!GetValue(attr, priv, attrName))
return false;
value = priv;
return true;
if (depth == 1)
ParseAdmin(el, attr);
-if (depth == 2 && parsingAnswer)
- ParseAdminParams(el, attr);
+/*if (depth == 2 && parsingAnswer)
+ ParseAdminParams(el, attr);*/
return 0;
}
error = "Admin not found.";
}
else
+ {
parsingAnswer = true;
+ for (const char ** pos = attr; *pos != NULL; pos = pos + 2)
+ if (!TryParse(propertyParsers, ToLower(*pos), pos, *pos))
+ {
+ error = "Invalid parameter.";
+ break;
+ }
+ }
}
else
parsingAnswer = true;
}
}
//-----------------------------------------------------------------------------
-void GET_ADMIN::PARSER::ParseAdminParams(const char * el, const char ** attr)
+/*void GET_ADMIN::PARSER::ParseAdminParams(const char * el, const char ** attr)
{
if (!TryParse(propertyParsers, ToLower(el), attr))
error = "Invalid parameter.";
-}
+}*/
std::string error;
void ParseAdmin(const char * el, const char ** attr);
- void ParseAdminParams(const char * el, const char ** attr);
+ //void ParseAdminParams(const char * el, const char ** attr);
};
} // namespace GET_ADMIN
public:
typedef bool (* FUNC)(const char **, A &, T A::value_type:: *);
AOS_PARSER(A & a, T A::value_type:: * fld, FUNC f) : array(a), field(fld), func(f) {}
- virtual bool Parse(const char ** attr) { return func(attr, array, field); }
+ virtual bool Parse(const char ** attr, const std::string & /*attrName*/) { return func(attr, array, field); }
private:
A & array;
T A::value_type:: * field;
parsers.insert(std::make_pair(ToLower(name), new AOS_PARSER<A, T>(array, field, func)));
}
-bool GetTimeSpan(const char ** attr, DIRPRICE_DATA & value)
+bool GetTimeSpan(const char ** attr, DIRPRICE_DATA & value, const std::string & attrName)
{
int hb = 0;
int mb = 0;
int he = 0;
int me = 0;
-if (CheckValue(attr))
+if (CheckValue(attr, attrName))
if (ParseTariffTimeStr(attr[1], hb, mb, he, me) == 0)
{
value.hDay = hb;
}
template <typename T>
-bool GetTraffType(const char ** attr, T & value)
+bool GetTraffType(const char ** attr, T & value, const std::string & attrName)
{
-if (!CheckValue(attr))
+if (!CheckValue(attr, attrName))
return false;
std::string type(attr[1]);
if (type == "up")
template <typename A, typename T>
bool GetSlashedValue(const char ** attr, A & array, T A::value_type:: * field)
{
-if (!CheckValue(attr))
+if (!CheckValue(attr, "value"))
return false;
const char * start = attr[1];
size_t item = 0;
{
template <>
-bool GetValue<GET_USER::STAT>(const char ** attr, GET_USER::STAT & value)
+bool GetValue<GET_USER::STAT>(const char ** attr, GET_USER::STAT & value, const std::string & /*attrName*/)
{
if (!attr)
return false;
#include <strings.h>
-bool STG::CheckValue(const char ** attr)
+bool STG::CheckValue(const char ** attr, const std::string & attrName)
{
-return attr && attr[0] && attr[1] && strcasecmp(attr[0], "value") == 0;
+return attr && attr[0] && attr[1] && strcasecmp(attr[0], attrName.c_str()) == 0;
}
-bool STG::GetEncodedValue(const char ** attr, std::string & value)
+bool STG::GetEncodedValue(const char ** attr, std::string & value, const std::string & attrName)
{
-if (!CheckValue(attr))
+if (!CheckValue(attr, attrName))
return false;
Decode21str(value, attr[1]);
return true;
}
-bool STG::GetIPValue(const char ** attr, uint32_t & value)
+bool STG::GetIPValue(const char ** attr, uint32_t & value, const std::string & attrName)
{
-if (!CheckValue(attr))
+if (!CheckValue(attr, attrName))
return false;
std::string ip(attr[1]);
value = inet_strington(attr[1]);
return true;
}
-bool STG::TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr)
+bool STG::TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr, const std::string & attrName)
{
PROPERTY_PARSERS::iterator it(parsers.find(name));
if (it != parsers.end())
- return it->second->Parse(attr);
+ return it->second->Parse(attr, attrName);
return true; // Assume that non-existing params are ok.
}
{
public:
virtual ~BASE_PROPERTY_PARSER() {}
- virtual bool Parse(const char ** attr) = 0;
+ virtual bool Parse(const char ** attr, const std::string & attrName) = 0;
};
template <typename T>
class PROPERTY_PARSER : public BASE_PROPERTY_PARSER
{
public:
- typedef bool (* FUNC)(const char **, T &);
+ typedef bool (* FUNC)(const char **, T &, const std::string &);
PROPERTY_PARSER(T & v, FUNC f) : value(v), func(f) {}
- virtual bool Parse(const char ** attr) { return func(attr, value); }
+ virtual bool Parse(const char ** attr, const std::string & attrName) { return func(attr, value, attrName); }
private:
T & value;
FUNC func;
typedef std::map<std::string, BASE_PROPERTY_PARSER *> PROPERTY_PARSERS;
-bool CheckValue(const char ** attr);
+bool CheckValue(const char ** attr, const std::string & attrName);
template <typename T>
inline
-bool GetValue(const char ** attr, T & value)
+bool GetValue(const char ** attr, T & value, const std::string & attrName)
{
-if (CheckValue(attr))
+if (CheckValue(attr, attrName))
if (str2x(attr[1], value) < 0)
return false;
return true;
template <>
inline
-bool GetValue<std::string>(const char ** attr, std::string & value)
+bool GetValue<std::string>(const char ** attr, std::string & value, const std::string & attrName)
{
-if (!CheckValue(attr))
+if (!CheckValue(attr, attrName))
return false;
value = attr[1];
return true;
template <>
inline
-bool GetValue<double>(const char ** attr, double & value)
+bool GetValue<double>(const char ** attr, double & value, const std::string & attrName)
{
-if (CheckValue(attr))
+if (CheckValue(attr, attrName))
if (strtodouble2(attr[1], value))
return false;
return true;
}
-bool GetEncodedValue(const char ** attr, std::string & value);
+bool GetEncodedValue(const char ** attr, std::string & value, const std::string & attrName);
-bool GetIPValue(const char ** attr, uint32_t& value);
+bool GetIPValue(const char ** attr, uint32_t& value, const std::string & attrName);
template <typename T>
void AddParser(PROPERTY_PARSERS & parsers, const std::string & name, T & value, const typename PROPERTY_PARSER<T>::FUNC & func = GetValue<T>);
parsers.insert(std::make_pair(ToLower(name), new PROPERTY_PARSER<T>(value, func)));
}
-bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr);
+bool TryParse(PROPERTY_PARSERS & parsers, const std::string & name, const char ** attr, const std::string & attrName = "value");
} // namespace STG