5 #include "stg/servconf.h"
6 #include "stg/servconf_types.h"
7 #include "stg/os_int.h"
15 std::string Indent(size_t level, bool dash = false)
19 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
22 std::string PrivToString(const PRIV& priv)
24 return std::string("") +
25 (priv.corpChg ? "1" : "0") +
26 (priv.serviceChg ? "1" : "0") +
27 (priv.tariffChg ? "1" : "0") +
28 (priv.adminChg ? "1" : "0") +
29 (priv.userAddDel ? "1" : "0") +
30 (priv.userPasswd ? "1" : "0") +
31 (priv.userCash ? "1" : "0") +
32 (priv.userConf ? "1" : "0") +
33 (priv.userStat ? "1" : "0");
36 void PrintAdmin(const STG::GET_ADMIN::INFO & info, size_t level = 0)
38 std::cout << Indent(level, true) << "login: " << info.login << "\n"
39 << Indent(level) << "priviledges: " << PrivToString(info.priv) << "\n";
42 void SimpleCallback(bool result,
43 const std::string & reason,
48 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
51 std::cout << "Success.\n";
54 void GetAdminsCallback(bool result,
55 const std::string & reason,
56 const std::vector<STG::GET_ADMIN::INFO> & info,
61 std::cerr << "Failed to get admin list. Reason: '" << reason << "'." << std::endl;
64 std::cout << "Admins:\n";
65 for (size_t i = 0; i < info.size(); ++i)
66 PrintAdmin(info[i], 1);
69 void GetAdminCallback(bool result,
70 const std::string & reason,
71 const std::vector<STG::GET_ADMIN::INFO> & info,
74 assert(data != NULL && "Expecting pointer to std::string with the admin's login.");
75 const std::string & login = *static_cast<const std::string *>(data);
78 std::cerr << "Failed to get admin. Reason: '" << reason << "'." << std::endl;
81 for (size_t i = 0; i < info.size(); ++i)
82 if (info[i].login == login)
89 bool SGCONF::GetAdminsFunction(const SGCONF::CONFIG & config,
90 const std::string & /*arg*/,
91 const std::map<std::string, std::string> & /*options*/)
93 STG::SERVCONF proto(config.server.data(),
95 config.userName.data(),
96 config.userPass.data());
97 return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
100 bool SGCONF::GetAdminFunction(const SGCONF::CONFIG & config,
101 const std::string & arg,
102 const std::map<std::string, std::string> & /*options*/)
104 STG::SERVCONF proto(config.server.data(),
106 config.userName.data(),
107 config.userPass.data());
108 // STG currently doesn't support <GetAdmin login="..."/>.
109 // So get a list of admins and filter it. 'data' param holds a pointer to 'login'.
110 std::string login(arg);
111 return proto.GetAdmins(GetAdminCallback, &login) == STG::st_ok;
114 bool SGCONF::DelAdminFunction(const SGCONF::CONFIG & config,
115 const std::string & arg,
116 const std::map<std::string, std::string> & /*options*/)
118 STG::SERVCONF proto(config.server.data(),
120 config.userName.data(),
121 config.userPass.data());
122 return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
125 bool SGCONF::AddAdminFunction(const SGCONF::CONFIG & config,
126 const std::string & arg,
127 const std::map<std::string, std::string> & /*options*/)
130 std::cerr << "Unimplemented.\n";
134 bool SGCONF::ChgAdminFunction(const SGCONF::CONFIG & config,
135 const std::string & arg,
136 const std::map<std::string, std::string> & options)
139 std::cerr << "Unimplemented.\n";