3 #include "api_action.h"
7 #include "stg/servconf.h"
8 #include "stg/servconf_types.h"
9 #include "stg/os_int.h"
19 std::string Indent(size_t level, bool dash = false)
23 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
26 std::string PrivToString(const PRIV& priv)
28 return std::string("") +
29 (priv.corpChg ? "1" : "0") +
30 (priv.serviceChg ? "1" : "0") +
31 (priv.tariffChg ? "1" : "0") +
32 (priv.adminChg ? "1" : "0") +
33 (priv.userAddDel ? "1" : "0") +
34 (priv.userPasswd ? "1" : "0") +
35 (priv.userCash ? "1" : "0") +
36 (priv.userConf ? "1" : "0") +
37 (priv.userStat ? "1" : "0");
40 void PrintAdmin(const STG::GET_ADMIN::INFO & info, size_t level = 0)
42 std::cout << Indent(level, true) << "login: " << info.login << "\n"
43 << Indent(level) << "priviledges: " << PrivToString(info.priv) << "\n";
46 std::vector<SGCONF::API_ACTION::PARAM> GetAdminParams()
48 std::vector<SGCONF::API_ACTION::PARAM> params;
49 params.push_back({"priv", "<priv>", "priviledges"});
53 void SimpleCallback(bool result,
54 const std::string & reason,
59 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
62 std::cout << "Success.\n";
65 void GetAdminsCallback(bool result,
66 const std::string & reason,
67 const std::vector<STG::GET_ADMIN::INFO> & info,
72 std::cerr << "Failed to get admin list. Reason: '" << reason << "'." << std::endl;
75 std::cout << "Admins:\n";
76 for (size_t i = 0; i < info.size(); ++i)
77 PrintAdmin(info[i], 1);
80 void GetAdminCallback(bool result,
81 const std::string & reason,
82 const std::vector<STG::GET_ADMIN::INFO> & info,
85 assert(data != NULL && "Expecting pointer to std::string with the admin's login.");
86 const std::string & login = *static_cast<const std::string *>(data);
89 std::cerr << "Failed to get admin. Reason: '" << reason << "'." << std::endl;
92 for (size_t i = 0; i < info.size(); ++i)
93 if (info[i].login == login)
98 bool GetAdminsFunction(const SGCONF::CONFIG & config,
99 const std::string & /*arg*/,
100 const std::map<std::string, std::string> & /*options*/)
102 STG::SERVCONF proto(config.server.data(),
104 config.userName.data(),
105 config.userPass.data());
106 return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
109 bool GetAdminFunction(const SGCONF::CONFIG & config,
110 const std::string & arg,
111 const std::map<std::string, std::string> & /*options*/)
113 STG::SERVCONF proto(config.server.data(),
115 config.userName.data(),
116 config.userPass.data());
117 // STG currently doesn't support <GetAdmin login="..."/>.
118 // So get a list of admins and filter it. 'data' param holds a pointer to 'login'.
119 std::string login(arg);
120 return proto.GetAdmins(GetAdminCallback, &login) == STG::st_ok;
123 bool DelAdminFunction(const SGCONF::CONFIG & config,
124 const std::string & arg,
125 const std::map<std::string, std::string> & /*options*/)
127 STG::SERVCONF proto(config.server.data(),
129 config.userName.data(),
130 config.userPass.data());
131 return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
134 bool AddAdminFunction(const SGCONF::CONFIG & config,
135 const std::string & arg,
136 const std::map<std::string, std::string> & /*options*/)
139 std::cerr << "Unimplemented.\n";
143 bool ChgAdminFunction(const SGCONF::CONFIG & config,
144 const std::string & arg,
145 const std::map<std::string, std::string> & options)
148 std::cerr << "Unimplemented.\n";
152 } // namespace anonymous
154 void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
156 std::vector<API_ACTION::PARAM> params(GetAdminParams());
157 blocks.Add("Admin management options")
158 .Add("get-admins", SGCONF::MakeAPIAction(commands, GetAdminsFunction), "\tget admin list")
159 .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", GetAdminFunction), "get admin")
160 .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", params, AddAdminFunction), "add admin")
161 .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", DelAdminFunction), "del admin")
162 .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", params, ChgAdminFunction), "change admin");