]> git.stg.codes - stg.git/blob - sgconf/admins.cpp
Update SMUX library.
[stg.git] / sgconf / admins.cpp
1 #include "admins.h"
2
3 #include "api_action.h"
4 #include "options.h"
5 #include "config.h"
6 #include "utils.h"
7
8 #include "stg/servconf.h"
9 #include "stg/servconf_types.h"
10 #include "stg/admin_conf.h"
11
12 #include <iostream>
13 #include <string>
14 #include <map>
15 #include <cstdint>
16 #include <cassert>
17
18 namespace
19 {
20
21 std::string Indent(size_t level, bool dash = false)
22 {
23 if (level == 0)
24     return "";
25 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
26 }
27
28 std::string PrivToString(const STG::Priv& priv)
29 {
30 return std::string("") +
31        (priv.corpChg ? "1" : "0") +
32        (priv.serviceChg ? "1" : "0") +
33        (priv.tariffChg ? "1" : "0") +
34        (priv.adminChg ? "1" : "0") +
35        (priv.userAddDel ? "1" : "0") +
36        (priv.userPasswd ? "1" : "0") +
37        (priv.userCash ? "1" : "0") +
38        (priv.userConf ? "1" : "0") +
39        (priv.userStat ? "1" : "0");
40 }
41
42 void PrintAdmin(const STG::GetAdmin::Info & info, size_t level = 0)
43 {
44 std::cout << Indent(level, true) << "login: " << info.login << "\n"
45           << Indent(level)       << "priviledges: " << PrivToString(info.priv) << "\n";
46 }
47
48 std::vector<SGCONF::API_ACTION::PARAM> GetAdminParams()
49 {
50 std::vector<SGCONF::API_ACTION::PARAM> params;
51 params.push_back(SGCONF::API_ACTION::PARAM("password", "<password>", "password"));
52 params.push_back(SGCONF::API_ACTION::PARAM("priv", "<priv>", "priviledges"));
53 return params;
54 }
55
56 void ConvPriv(const std::string & value, STG::Optional<STG::Priv> & res)
57 {
58 if (value.length() != 9)
59     throw SGCONF::ACTION::ERROR("Priviledges value should be a 9-digits length binary number.");
60 STG::Priv priv;
61 priv.corpChg = (value[0] == '0' ? 0 : 1);
62 priv.serviceChg = (value[1] == '0' ? 0 : 1);
63 priv.tariffChg = (value[2] == '0' ? 0 : 1);
64 priv.adminChg = (value[3] == '0' ? 0 : 1);
65 priv.userAddDel = (value[4] == '0' ? 0 : 1);
66 priv.userPasswd = (value[5] == '0' ? 0 : 1);
67 priv.userCash = (value[6] == '0' ? 0 : 1);
68 priv.userConf = (value[7] == '0' ? 0 : 1);
69 priv.userStat = (value[8] == '0' ? 0 : 1);
70 res = priv;
71 }
72
73 void SimpleCallback(bool result,
74                     const std::string & reason,
75                     void * /*data*/)
76 {
77 if (!result)
78     {
79     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
80     return;
81     }
82 std::cout << "Success.\n";
83 }
84
85 void GetAdminsCallback(bool result,
86                        const std::string & reason,
87                        const std::vector<STG::GetAdmin::Info> & info,
88                        void * /*data*/)
89 {
90 if (!result)
91     {
92     std::cerr << "Failed to get admin list. Reason: '" << reason << "'." << std::endl;
93     return;
94     }
95 std::cout << "Admins:\n";
96 for (size_t i = 0; i < info.size(); ++i)
97     PrintAdmin(info[i], 1);
98 }
99
100 void GetAdminCallback(bool result,
101                       const std::string & reason,
102                       const std::vector<STG::GetAdmin::Info> & info,
103                       void * data)
104 {
105 assert(data != NULL && "Expecting pointer to std::string with the admin's login.");
106 const std::string & login = *static_cast<const std::string *>(data);
107 if (!result)
108     {
109     std::cerr << "Failed to get admin. Reason: '" << reason << "'." << std::endl;
110     return;
111     }
112 for (size_t i = 0; i < info.size(); ++i)
113     if (info[i].login == login)
114         PrintAdmin(info[i]);
115 }
116
117
118 bool GetAdminsFunction(const SGCONF::CONFIG & config,
119                        const std::string & /*arg*/,
120                        const std::map<std::string, std::string> & /*options*/)
121 {
122 STG::ServConf proto(config.server.data(),
123                     config.port.data(),
124                     config.localAddress.data(),
125                     config.localPort.data(),
126                     config.userName.data(),
127                     config.userPass.data());
128 return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
129 }
130
131 bool GetAdminFunction(const SGCONF::CONFIG & config,
132                       const std::string & arg,
133                       const std::map<std::string, std::string> & /*options*/)
134 {
135 STG::ServConf proto(config.server.data(),
136                     config.port.data(),
137                     config.localAddress.data(),
138                     config.localPort.data(),
139                     config.userName.data(),
140                     config.userPass.data());
141 // STG currently doesn't support <GetAdmin login="..."/>.
142 // So get a list of admins and filter it. 'data' param holds a pointer to 'login'.
143 std::string login(arg);
144 return proto.GetAdmins(GetAdminCallback, &login) == STG::st_ok;
145 }
146
147 bool DelAdminFunction(const SGCONF::CONFIG & config,
148                       const std::string & arg,
149                       const std::map<std::string, std::string> & /*options*/)
150 {
151 STG::ServConf proto(config.server.data(),
152                     config.port.data(),
153                     config.localAddress.data(),
154                     config.localPort.data(),
155                     config.userName.data(),
156                     config.userPass.data());
157 return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
158 }
159
160 bool AddAdminFunction(const SGCONF::CONFIG & config,
161                       const std::string & arg,
162                       const std::map<std::string, std::string> & options)
163 {
164 STG::AdminConfOpt conf;
165 conf.login = arg;
166 SGCONF::MaybeSet(options, "priv", conf.priv, ConvPriv);
167 SGCONF::MaybeSet(options, "password", conf.password);
168 STG::ServConf proto(config.server.data(),
169                     config.port.data(),
170                     config.localAddress.data(),
171                     config.localPort.data(),
172                     config.userName.data(),
173                     config.userPass.data());
174 return proto.AddAdmin(arg, conf, SimpleCallback, NULL) == STG::st_ok;
175 }
176
177 bool ChgAdminFunction(const SGCONF::CONFIG & config,
178                       const std::string & arg,
179                       const std::map<std::string, std::string> & options)
180 {
181 STG::AdminConfOpt conf;
182 conf.login = arg;
183 SGCONF::MaybeSet(options, "priv", conf.priv, ConvPriv);
184 SGCONF::MaybeSet(options, "password", conf.password);
185 STG::ServConf proto(config.server.data(),
186                     config.port.data(),
187                     config.localAddress.data(),
188                     config.localPort.data(),
189                     config.userName.data(),
190                     config.userPass.data());
191 return proto.ChgAdmin(conf, SimpleCallback, NULL) == STG::st_ok;
192 }
193
194 } // namespace anonymous
195
196 void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
197 {
198 std::vector<API_ACTION::PARAM> params(GetAdminParams());
199 blocks.Add("Admin management options")
200       .Add("get-admins", SGCONF::MakeAPIAction(commands, GetAdminsFunction), "\tget admin list")
201       .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", GetAdminFunction), "get admin")
202       .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", params, AddAdminFunction), "add admin")
203       .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", DelAdminFunction), "del admin")
204       .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", params, ChgAdminFunction), "change admin");
205 }