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 void SimpleCallback(bool result,
47 const std::string & reason,
52 std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
55 std::cout << "Success.\n";
58 void GetAdminsCallback(bool result,
59 const std::string & reason,
60 const std::vector<STG::GET_ADMIN::INFO> & info,
65 std::cerr << "Failed to get admin list. Reason: '" << reason << "'." << std::endl;
68 std::cout << "Admins:\n";
69 for (size_t i = 0; i < info.size(); ++i)
70 PrintAdmin(info[i], 1);
73 void GetAdminCallback(bool result,
74 const std::string & reason,
75 const std::vector<STG::GET_ADMIN::INFO> & info,
78 assert(data != NULL && "Expecting pointer to std::string with the admin's login.");
79 const std::string & login = *static_cast<const std::string *>(data);
82 std::cerr << "Failed to get admin. Reason: '" << reason << "'." << std::endl;
85 for (size_t i = 0; i < info.size(); ++i)
86 if (info[i].login == login)
91 bool GetAdminsFunction(const SGCONF::CONFIG & config,
92 const std::string & /*arg*/,
93 const std::map<std::string, std::string> & /*options*/)
95 STG::SERVCONF proto(config.server.data(),
97 config.userName.data(),
98 config.userPass.data());
99 return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
102 bool GetAdminFunction(const SGCONF::CONFIG & config,
103 const std::string & arg,
104 const std::map<std::string, std::string> & /*options*/)
106 STG::SERVCONF proto(config.server.data(),
108 config.userName.data(),
109 config.userPass.data());
110 // STG currently doesn't support <GetAdmin login="..."/>.
111 // So get a list of admins and filter it. 'data' param holds a pointer to 'login'.
112 std::string login(arg);
113 return proto.GetAdmins(GetAdminCallback, &login) == STG::st_ok;
116 bool DelAdminFunction(const SGCONF::CONFIG & config,
117 const std::string & arg,
118 const std::map<std::string, std::string> & /*options*/)
120 STG::SERVCONF proto(config.server.data(),
122 config.userName.data(),
123 config.userPass.data());
124 return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
127 bool AddAdminFunction(const SGCONF::CONFIG & config,
128 const std::string & arg,
129 const std::map<std::string, std::string> & /*options*/)
132 std::cerr << "Unimplemented.\n";
136 bool ChgAdminFunction(const SGCONF::CONFIG & config,
137 const std::string & arg,
138 const std::map<std::string, std::string> & options)
141 std::cerr << "Unimplemented.\n";
145 } // namespace anonymous
147 void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
149 blocks.Add("Admin management options")
150 .Add("get-admins", SGCONF::MakeAPIAction(commands, GetAdminsFunction), "\tget admin list")
151 .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", true, GetAdminFunction), "get admin")
152 .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", true, AddAdminFunction), "add admin")
153 .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", true, DelAdminFunction), "del admin")
154 .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", true, ChgAdminFunction), "change admin");