]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser_admin.cpp
d93b3448f013efa4d8d6223afa39b6e02c182f95
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / parser_admin.cpp
1 #include <stdio.h>
2 #include <string.h>
3
4 #include "parser.h"
5
6 //-----------------------------------------------------------------------------
7 //  GET ADMINS
8 //-----------------------------------------------------------------------------
9 int PARSER_GET_ADMINS::ParseStart(void *, const char *el, const char **)
10 {
11 if (strcasecmp(el, "GetAdmins") == 0)
12     {
13     return 0;
14     }
15 return -1;
16 }
17 //-----------------------------------------------------------------------------
18 int PARSER_GET_ADMINS::ParseEnd(void *, const char *el)
19 {
20 if (strcasecmp(el, "GetAdmins") == 0)
21     {
22     CreateAnswer();
23     return 0;
24     }
25 return -1;
26 }
27 //-----------------------------------------------------------------------------
28 void PARSER_GET_ADMINS::CreateAnswer()
29 {
30 const PRIV * priv = currAdmin->GetPriv();
31 if (!priv->adminChg)
32     {
33     //answerList->clear();
34     answerList->erase(answerList->begin(), answerList->end());
35
36     answerList->push_back("<Error Result=\"Error. Access denied.\"/>");
37     return;
38     }
39
40 std::string s;
41 //answerList->clear();
42 answerList->erase(answerList->begin(), answerList->end());
43
44 answerList->push_back("<Admins>");
45 ADMIN_CONF ac;
46 int h = admins->OpenSearch();
47
48 while (admins->SearchNext(h, &ac) == 0)
49     {
50     //memcpy(&p, &ac.priv, sizeof(unsigned int));
51     unsigned int p = (ac.priv.userStat << 0) +
52                      (ac.priv.userConf << 2) +
53                      (ac.priv.userCash << 4) +
54                      (ac.priv.userPasswd << 6) +
55                      (ac.priv.userAddDel << 8) +
56                      (ac.priv.adminChg << 10) +
57                      (ac.priv.tariffChg << 12);
58     strprintf(&s, "<admin login=\"%s\" priv=\"%d\"/>", ac.login.c_str(), p);
59     answerList->push_back(s);
60     }
61 admins->CloseSearch(h);
62 answerList->push_back("</Admins>");
63 }
64 //-----------------------------------------------------------------------------
65
66 //-----------------------------------------------------------------------------
67 //  DEL ADMIN
68 //-----------------------------------------------------------------------------
69 int PARSER_DEL_ADMIN::ParseStart(void *, const char *el, const char **attr)
70 {
71 strError = "";
72 if (strcasecmp(el, "DelAdmin") == 0)
73     {
74     adminToDel = attr[1];
75     return 0;
76     }
77 return -1;
78 }
79 //-----------------------------------------------------------------------------
80 int PARSER_DEL_ADMIN::ParseEnd(void *, const char *el)
81 {
82 if (strcasecmp(el, "DelAdmin") == 0)
83     {
84     CreateAnswer();
85     return 0;
86     }
87 return -1;
88 }
89 //-----------------------------------------------------------------------------
90 void PARSER_DEL_ADMIN::CreateAnswer()
91 {
92 //answerList->clear();
93 answerList->erase(answerList->begin(), answerList->end());
94
95 if (admins->Del(adminToDel, currAdmin) == 0)
96     {
97     answerList->push_back("<DelAdmin Result=\"Ok\"/>");
98     }
99 else
100     {
101     std::string s;
102     strprintf(&s, "<DelAdmin Result=\"Error. %s\"/>", admins->GetStrError().c_str());
103     answerList->push_back(s);
104     }
105 }
106 //-----------------------------------------------------------------------------
107 //  ADD ADMIN
108 //-----------------------------------------------------------------------------
109 int PARSER_ADD_ADMIN::ParseStart(void *, const char *el, const char **attr)
110 {
111 if (strcasecmp(el, "AddAdmin") == 0)
112     {
113     adminToAdd = attr[1];
114     return 0;
115     }
116 return -1;
117 }
118 //-----------------------------------------------------------------------------
119 int PARSER_ADD_ADMIN::ParseEnd(void *, const char *el)
120 {
121 //answerList->clear();
122 answerList->erase(answerList->begin(), answerList->end());
123
124 if (strcasecmp(el, "AddAdmin") == 0)
125     {
126     CreateAnswer();
127     return 0;
128     }
129 return -1;
130 }
131 //-----------------------------------------------------------------------------
132 void PARSER_ADD_ADMIN::CreateAnswer()
133 {
134 //answerList->clear();
135 answerList->erase(answerList->begin(), answerList->end());
136
137 if (admins->Add(adminToAdd, currAdmin) == 0)
138     {
139     answerList->push_back("<AddAdmin Result=\"Ok\"/>");
140     }
141 else
142     {
143     std::string s;
144     strprintf(&s, "<AddAdmin Result=\"Error. %s\"/>", admins->GetStrError().c_str());
145     answerList->push_back(s);
146     }
147 }
148 //-----------------------------------------------------------------------------
149 //-----------------------------------------------------------------------------
150 //  CHG ADMIN
151 //-----------------------------------------------------------------------------
152 int PARSER_CHG_ADMIN::ParseStart(void *, const char *el, const char **attr)
153 {
154 if (strcasecmp(el, "ChgAdmin") == 0)
155     {
156     for (int i = 0; i < 6; i+=2)
157         {
158         printfd(__FILE__, "PARSER_CHG_ADMIN::attr[%d] = %s\n", i, attr[i]);
159         if (attr[i] == NULL)
160             break;
161
162         if (strcasecmp(attr[i], "Login") == 0)
163             {
164             login = attr[i + 1];
165             continue;
166             }
167
168         if (strcasecmp(attr[i], "Priv") == 0)
169             {
170             privAsString = attr[i + 1];
171             continue;
172             }
173
174         if (strcasecmp(attr[i], "Password") == 0)
175             {
176             password = attr[i + 1];
177             continue;
178             }
179         }
180
181     return 0;
182     }
183 return -1;
184 }
185 //-----------------------------------------------------------------------------
186 int PARSER_CHG_ADMIN::ParseEnd(void *, const char *el)
187 {
188 if (strcasecmp(el, "ChgAdmin") == 0)
189     {
190     CreateAnswer();
191     return 0;
192     }
193 return -1;
194 }
195 //-----------------------------------------------------------------------------
196 void PARSER_CHG_ADMIN::CreateAnswer()
197 {
198 answerList->erase(answerList->begin(), answerList->end());
199
200
201 if (!login.empty())
202     {
203     ADMIN * origAdmin = NULL;
204
205     if (admins->Find(login.data(), &origAdmin))
206         {
207         answerList->push_back(std::string("<ChgAdmin Result = \"Admin '") + login.data() + "' is not found.\"/>");
208         return;
209         }
210
211     ADMIN_CONF conf(origAdmin->GetConf());
212
213     if (!password.empty())
214         conf.password = password.data();
215
216     if (!privAsString.empty())
217         {
218         int p = 0;
219         if (str2x(privAsString.data().c_str(), p) < 0)
220             {
221             answerList->push_back("<ChgAdmin Result = \"Incorrect parameter Priv.\"/>");
222             return;
223             }
224
225         conf.priv.FromInt(p);
226         }
227
228     if (admins->Change(conf, currAdmin) != 0)
229         {
230         std::string s;
231         strprintf(&s, "<ChgAdmin Result = \"%s\"/>", admins->GetStrError().c_str());
232         answerList->push_back(s);
233         }
234     else
235         {
236         answerList->push_back("<ChgAdmin Result = \"Ok\"/>");
237         }
238     }
239 else
240     {
241     answerList->push_back("<ChgAdmin Result = \"Incorrect parameter login.\"/>");
242     }
243 }
244 //-----------------------------------------------------------------------------*/
245