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