]> git.stg.codes - stg.git/blob - projects/sgconf/admins.cpp
Added suboptions to tariffs and admins.
[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
7 #include "stg/servconf.h"
8 #include "stg/servconf_types.h"
9 #include "stg/os_int.h"
10
11 #include <iostream>
12 #include <string>
13 #include <map>
14 #include <cassert>
15
16 namespace
17 {
18
19 std::string Indent(size_t level, bool dash = false)
20 {
21 if (level == 0)
22     return "";
23 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
24 }
25
26 std::string PrivToString(const PRIV& priv)
27 {
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");
38 }
39
40 void PrintAdmin(const STG::GET_ADMIN::INFO & info, size_t level = 0)
41 {
42 std::cout << Indent(level, true) << "login: " << info.login << "\n"
43           << Indent(level)       << "priviledges: " << PrivToString(info.priv) << "\n";
44 }
45
46 std::vector<SGCONF::API_ACTION::PARAM> GetAdminParams()
47 {
48 std::vector<SGCONF::API_ACTION::PARAM> params;
49 params.push_back({"priv", "<priv>", "priviledges"});
50 return params;
51 }
52
53 void SimpleCallback(bool result,
54                     const std::string & reason,
55                     void * /*data*/)
56 {
57 if (!result)
58     {
59     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
60     return;
61     }
62 std::cout << "Success.\n";
63 }
64
65 void GetAdminsCallback(bool result,
66                        const std::string & reason,
67                        const std::vector<STG::GET_ADMIN::INFO> & info,
68                        void * /*data*/)
69 {
70 if (!result)
71     {
72     std::cerr << "Failed to get admin list. Reason: '" << reason << "'." << std::endl;
73     return;
74     }
75 std::cout << "Admins:\n";
76 for (size_t i = 0; i < info.size(); ++i)
77     PrintAdmin(info[i], 1);
78 }
79
80 void GetAdminCallback(bool result,
81                       const std::string & reason,
82                       const std::vector<STG::GET_ADMIN::INFO> & info,
83                       void * data)
84 {
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);
87 if (!result)
88     {
89     std::cerr << "Failed to get admin. Reason: '" << reason << "'." << std::endl;
90     return;
91     }
92 for (size_t i = 0; i < info.size(); ++i)
93     if (info[i].login == login)
94         PrintAdmin(info[i]);
95 }
96
97
98 bool GetAdminsFunction(const SGCONF::CONFIG & config,
99                        const std::string & /*arg*/,
100                        const std::map<std::string, std::string> & /*options*/)
101 {
102 STG::SERVCONF proto(config.server.data(),
103                     config.port.data(),
104                     config.userName.data(),
105                     config.userPass.data());
106 return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
107 }
108
109 bool GetAdminFunction(const SGCONF::CONFIG & config,
110                       const std::string & arg,
111                       const std::map<std::string, std::string> & /*options*/)
112 {
113 STG::SERVCONF proto(config.server.data(),
114                     config.port.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;
121 }
122
123 bool DelAdminFunction(const SGCONF::CONFIG & config,
124                       const std::string & arg,
125                       const std::map<std::string, std::string> & /*options*/)
126 {
127 STG::SERVCONF proto(config.server.data(),
128                     config.port.data(),
129                     config.userName.data(),
130                     config.userPass.data());
131 return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
132 }
133
134 bool AddAdminFunction(const SGCONF::CONFIG & config,
135                       const std::string & arg,
136                       const std::map<std::string, std::string> & /*options*/)
137 {
138 // TODO
139 std::cerr << "Unimplemented.\n";
140 return false;
141 }
142
143 bool ChgAdminFunction(const SGCONF::CONFIG & config,
144                       const std::string & arg,
145                       const std::map<std::string, std::string> & options)
146 {
147 // TODO
148 std::cerr << "Unimplemented.\n";
149 return false;
150 }
151
152 } // namespace anonymous
153
154 void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
155 {
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");
163 }