]> git.stg.codes - stg.git/blob - projects/sgconf/admins.cpp
Simplified module interfaces.
[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 void SimpleCallback(bool result,
47                     const std::string & reason,
48                     void * /*data*/)
49 {
50 if (!result)
51     {
52     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
53     return;
54     }
55 std::cout << "Success.\n";
56 }
57
58 void GetAdminsCallback(bool result,
59                        const std::string & reason,
60                        const std::vector<STG::GET_ADMIN::INFO> & info,
61                        void * /*data*/)
62 {
63 if (!result)
64     {
65     std::cerr << "Failed to get admin list. Reason: '" << reason << "'." << std::endl;
66     return;
67     }
68 std::cout << "Admins:\n";
69 for (size_t i = 0; i < info.size(); ++i)
70     PrintAdmin(info[i], 1);
71 }
72
73 void GetAdminCallback(bool result,
74                       const std::string & reason,
75                       const std::vector<STG::GET_ADMIN::INFO> & info,
76                       void * data)
77 {
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);
80 if (!result)
81     {
82     std::cerr << "Failed to get admin. Reason: '" << reason << "'." << std::endl;
83     return;
84     }
85 for (size_t i = 0; i < info.size(); ++i)
86     if (info[i].login == login)
87         PrintAdmin(info[i]);
88 }
89
90
91 bool GetAdminsFunction(const SGCONF::CONFIG & config,
92                        const std::string & /*arg*/,
93                        const std::map<std::string, std::string> & /*options*/)
94 {
95 STG::SERVCONF proto(config.server.data(),
96                     config.port.data(),
97                     config.userName.data(),
98                     config.userPass.data());
99 return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
100 }
101
102 bool GetAdminFunction(const SGCONF::CONFIG & config,
103                       const std::string & arg,
104                       const std::map<std::string, std::string> & /*options*/)
105 {
106 STG::SERVCONF proto(config.server.data(),
107                     config.port.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;
114 }
115
116 bool DelAdminFunction(const SGCONF::CONFIG & config,
117                       const std::string & arg,
118                       const std::map<std::string, std::string> & /*options*/)
119 {
120 STG::SERVCONF proto(config.server.data(),
121                     config.port.data(),
122                     config.userName.data(),
123                     config.userPass.data());
124 return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
125 }
126
127 bool AddAdminFunction(const SGCONF::CONFIG & config,
128                       const std::string & arg,
129                       const std::map<std::string, std::string> & /*options*/)
130 {
131 // TODO
132 std::cerr << "Unimplemented.\n";
133 return false;
134 }
135
136 bool ChgAdminFunction(const SGCONF::CONFIG & config,
137                       const std::string & arg,
138                       const std::map<std::string, std::string> & options)
139 {
140 // TODO
141 std::cerr << "Unimplemented.\n";
142 return false;
143 }
144
145 } // namespace anonymous
146
147 void SGCONF::AppendAdminsOptionBlock(COMMANDS & commands, OPTION_BLOCKS & blocks)
148 {
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");
155 }