]> git.stg.codes - stg.git/blob - projects/sgconf/admins.cpp
Implemented some operations with tariffs.
[stg.git] / projects / sgconf / admins.cpp
1 #include "admins.h"
2
3 #include "config.h"
4
5 #include "stg/servconf.h"
6 #include "stg/servconf_types.h"
7 #include "stg/os_int.h"
8
9 #include <iostream>
10 #include <cassert>
11
12 namespace
13 {
14
15 std::string Indent(size_t level, bool dash = false)
16 {
17 if (level == 0)
18     return "";
19 return dash ? std::string(level * 4 - 2, ' ') + "- " : std::string(level * 4, ' ');
20 }
21
22 std::string PrivToString(const PRIV& priv)
23 {
24 return std::string("") +
25        (priv.corpChg ? "1" : "0") +
26        (priv.serviceChg ? "1" : "0") +
27        (priv.tariffChg ? "1" : "0") +
28        (priv.adminChg ? "1" : "0") +
29        (priv.userAddDel ? "1" : "0") +
30        (priv.userPasswd ? "1" : "0") +
31        (priv.userCash ? "1" : "0") +
32        (priv.userConf ? "1" : "0") +
33        (priv.userStat ? "1" : "0");
34 }
35
36 void PrintAdmin(const STG::GET_ADMIN::INFO & info, size_t level = 0)
37 {
38 std::cout << Indent(level, true) << "login: " << info.login << "\n"
39           << Indent(level)       << "priviledges: " << PrivToString(info.priv) << "\n";
40 }
41
42 void SimpleCallback(bool result,
43                     const std::string & reason,
44                     void * /*data*/)
45 {
46 if (!result)
47     {
48     std::cerr << "Operation failed. Reason: '" << reason << "'." << std::endl;
49     return;
50     }
51 std::cout << "Success.\n";
52 }
53
54 void GetAdminsCallback(bool result,
55                        const std::string & reason,
56                        const std::vector<STG::GET_ADMIN::INFO> & info,
57                        void * /*data*/)
58 {
59 if (!result)
60     {
61     std::cerr << "Failed to get admin list. Reason: '" << reason << "'." << std::endl;
62     return;
63     }
64 std::cout << "Admins:\n";
65 for (size_t i = 0; i < info.size(); ++i)
66     PrintAdmin(info[i], 1);
67 }
68
69 void GetAdminCallback(bool result,
70                       const std::string & reason,
71                       const std::vector<STG::GET_ADMIN::INFO> & info,
72                       void * data)
73 {
74 assert(data != NULL && "Expecting pointer to std::string with the admin's login.");
75 const std::string & login = *static_cast<const std::string *>(data);
76 if (!result)
77     {
78     std::cerr << "Failed to get admin. Reason: '" << reason << "'." << std::endl;
79     return;
80     }
81 for (size_t i = 0; i < info.size(); ++i)
82     if (info[i].login == login)
83         PrintAdmin(info[i]);
84 }
85
86 }
87
88
89 bool SGCONF::GetAdminsFunction(const SGCONF::CONFIG & config,
90                                const std::string & /*arg*/,
91                                const std::map<std::string, std::string> & /*options*/)
92 {
93 STG::SERVCONF proto(config.server.data(),
94                     config.port.data(),
95                     config.userName.data(),
96                     config.userPass.data());
97 return proto.GetAdmins(GetAdminsCallback, NULL) == STG::st_ok;
98 }
99
100 bool SGCONF::GetAdminFunction(const SGCONF::CONFIG & config,
101                               const std::string & arg,
102                               const std::map<std::string, std::string> & /*options*/)
103 {
104 STG::SERVCONF proto(config.server.data(),
105                     config.port.data(),
106                     config.userName.data(),
107                     config.userPass.data());
108 // STG currently doesn't support <GetAdmin login="..."/>.
109 // So get a list of admins and filter it. 'data' param holds a pointer to 'login'.
110 std::string login(arg);
111 return proto.GetAdmins(GetAdminCallback, &login) == STG::st_ok;
112 }
113
114 bool SGCONF::DelAdminFunction(const SGCONF::CONFIG & config,
115                               const std::string & arg,
116                               const std::map<std::string, std::string> & /*options*/)
117 {
118 STG::SERVCONF proto(config.server.data(),
119                     config.port.data(),
120                     config.userName.data(),
121                     config.userPass.data());
122 return proto.DelAdmin(arg, SimpleCallback, NULL) == STG::st_ok;
123 }
124
125 bool SGCONF::AddAdminFunction(const SGCONF::CONFIG & config,
126                               const std::string & arg,
127                               const std::map<std::string, std::string> & /*options*/)
128 {
129 // TODO
130 std::cerr << "Unimplemented.\n";
131 return false;
132 }
133
134 bool SGCONF::ChgAdminFunction(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 }