]> git.stg.codes - stg.git/blob - projects/sgconf/admins.cpp
Fixed setting admin's password.
[stg.git] / projects / 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/os_int.h"
11
12 #include <iostream>
13 #include <string>
14 #include <map>
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.userName.data(),
124                     config.userPass.data());
125 return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
126 }
127
128 bool GetAdminFunction(const SGCONF::CONFIG & config,
129                       const std::string & arg,
130                       const std::map<std::string, std::string> & /*options*/)
131 {
132 STG::SERVCONF proto(config.server.data(),
133                     config.port.data(),
134                     config.userName.data(),
135                     config.userPass.data());
136 // STG currently doesn't support <GetAdmin login="..."/>.
137 // So get a list of admins and filter it. 'data' param holds a pointer to 'login'.
138 std::string login(arg);
139 return proto.GetAdmins(GetAdminCallback, &login) == STG::st_ok;
140 }
141
142 bool DelAdminFunction(const SGCONF::CONFIG & config,
143                       const std::string & arg,
144                       const std::map<std::string, std::string> & /*options*/)
145 {
146 STG::SERVCONF proto(config.server.data(),
147                     config.port.data(),
148                     config.userName.data(),
149                     config.userPass.data());
150 return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
151 }
152
153 bool AddAdminFunction(const SGCONF::CONFIG & config,
154                       const std::string & arg,
155                       const std::map<std::string, std::string> & options)
156 {
157 // TODO
158 ADMIN_CONF_RES conf;
159 conf.login = arg;
160 SGCONF::MaybeSet(options, "priv", conf.priv, ConvPriv);
161 SGCONF::MaybeSet(options, "password", conf.password);
162 STG::SERVCONF proto(config.server.data(),
163                     config.port.data(),
164                     config.userName.data(),
165                     config.userPass.data());
166 return proto.AddAdmin(arg, conf, SimpleCallback, NULL) == STG::st_ok;
167 }
168
169 bool ChgAdminFunction(const SGCONF::CONFIG & config,
170                       const std::string & arg,
171                       const std::map<std::string, std::string> & options)
172 {
173 // TODO
174 std::cerr << "Unimplemented.\n";
175 return false;
176 }
177
178 } // namespace anonymous
179
180 void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
181 {
182 std::vector<API_ACTION::PARAM> params(GetAdminParams());
183 blocks.Add("Admin management options")
184       .Add("get-admins", SGCONF::MakeAPIAction(commands, GetAdminsFunction), "\tget admin list")
185       .Add("get-admin", SGCONF::MakeAPIAction(commands, "<login>", GetAdminFunction), "get admin")
186       .Add("add-admin", SGCONF::MakeAPIAction(commands, "<login>", params, AddAdminFunction), "add admin")
187       .Add("del-admin", SGCONF::MakeAPIAction(commands, "<login>", DelAdminFunction), "del admin")
188       .Add("chg-admin", SGCONF::MakeAPIAction(commands, "<login>", params, ChgAdminFunction), "change admin");
189 }