]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig2/parser_admin.cpp
Added forgotten 'break'.
[stg.git] / projects / stargazer / plugins / configuration / sgconfig2 / 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 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 unsigned int p;
49 while (admins->SearchNext(h, &ac) == 0)
50     {
51     //memcpy(&p, &ac.priv, sizeof(unsigned int));
52     p = (ac.priv.userStat << 0) +
53         (ac.priv.userConf << 2) +
54         (ac.priv.userCash << 4) +
55         (ac.priv.userPasswd << 6) +
56         (ac.priv.userAddDel << 8) +
57         (ac.priv.adminChg << 10) +
58         (ac.priv.tariffChg << 12);
59     strprintf(&s, "<admin login=\"%s\" priv=\"%d\"/>", ac.login.c_str(), p);
60     answerList->push_back(s);
61     }
62 admins->CloseSearch(h);
63 answerList->push_back("</Admins>");
64 }
65 //-----------------------------------------------------------------------------
66
67 //-----------------------------------------------------------------------------
68 //  DEL ADMIN
69 //-----------------------------------------------------------------------------
70 int PARSER_DEL_ADMIN::ParseStart(void *, const char *el, const char **attr)
71 {
72 strError = "";
73 if (strcasecmp(el, "DelAdmin") == 0)
74     {
75     adminToDel = attr[1];
76     return 0;
77     }
78 return -1;
79 }
80 //-----------------------------------------------------------------------------
81 int PARSER_DEL_ADMIN::ParseEnd(void *, const char *el)
82 {
83 if (strcasecmp(el, "DelAdmin") == 0)
84     {
85     CreateAnswer();
86     return 0;
87     }
88 return -1;
89 }
90 //-----------------------------------------------------------------------------
91 void PARSER_DEL_ADMIN::CreateAnswer()
92 {
93 //answerList->clear();
94 answerList->erase(answerList->begin(), answerList->end());
95
96 if (admins->Del(adminToDel, *currAdmin) == 0)
97     {
98     answerList->push_back("<DelAdmin Result=\"Ok\"/>");
99     }
100 else
101     {
102     string s;
103     strprintf(&s, "<DelAdmin Result=\"Error. %s\"/>", admins->GetStrError().c_str());
104     answerList->push_back(s);
105     }
106 }
107 //-----------------------------------------------------------------------------
108 int PARSER_DEL_ADMIN::CheckAttr(const char **attr)
109 {
110 /*  <DelAdmin login=\"admin\">
111  *  attr[0] = "login" (word login)
112  *  attr[1] = login, value of login
113  *  attr[2] = NULL                  */
114
115 if (strcasecmp(attr[0], "login") == 0 && attr[1] && !attr[2])
116     {
117     return 0;
118     }
119 return -1;
120 }
121 //-----------------------------------------------------------------------------
122 //  ADD ADMIN
123 //-----------------------------------------------------------------------------
124 int PARSER_ADD_ADMIN::ParseStart(void *, const char *el, const char **attr)
125 {
126 if (strcasecmp(el, "AddAdmin") == 0)
127     {
128     adminToAdd = attr[1];
129     return 0;
130     }
131 return -1;
132 }
133 //-----------------------------------------------------------------------------
134 int PARSER_ADD_ADMIN::ParseEnd(void *, const char *el)
135 {
136 //answerList->clear();
137 answerList->erase(answerList->begin(), answerList->end());
138
139 if (strcasecmp(el, "AddAdmin") == 0)
140     {
141     CreateAnswer();
142     return 0;
143     }
144 return -1;
145 }
146 //-----------------------------------------------------------------------------
147 void PARSER_ADD_ADMIN::CreateAnswer()
148 {
149 //answerList->clear();
150 answerList->erase(answerList->begin(), answerList->end());
151
152 if (admins->Add(adminToAdd, *currAdmin) == 0)
153     {
154     answerList->push_back("<AddAdmin Result=\"Ok\"/>");
155     }
156 else
157     {
158     string s;
159     strprintf(&s, "<AddAdmin Result=\"Error. %s\"/>", admins->GetStrError().c_str());
160     answerList->push_back(s);
161     }
162 }
163 //-----------------------------------------------------------------------------
164 //-----------------------------------------------------------------------------
165 //  CHG ADMIN
166 //-----------------------------------------------------------------------------
167 int PARSER_CHG_ADMIN::ParseStart(void *, const char *el, const char **attr)
168 {
169 if (strcasecmp(el, "ChgAdmin") == 0)
170     {
171     for (int i = 0; i < 6; i+=2)
172         {
173         printfd(__FILE__, "PARSER_CHG_ADMIN::attr[%d] = %s\n", i, attr[i]);
174         if (attr[i] == NULL)
175             break;
176
177         if (strcasecmp(attr[i], "Login") == 0)
178             {
179             login = attr[i + 1];
180             continue;
181             }
182
183         if (strcasecmp(attr[i], "Priv") == 0)
184             {
185             privAsString = attr[i + 1];
186             continue;
187             }
188
189         if (strcasecmp(attr[i], "Password") == 0)
190             {
191             password = attr[i + 1];
192             continue;
193             }
194         }
195
196     return 0;
197     }
198 return -1;
199 }
200 //-----------------------------------------------------------------------------
201 int PARSER_CHG_ADMIN::ParseEnd(void *, const char *el)
202 {
203 if (strcasecmp(el, "ChgAdmin") == 0)
204     {
205     CreateAnswer();
206     return 0;
207     }
208 return -1;
209 }
210 //-----------------------------------------------------------------------------
211 void PARSER_CHG_ADMIN::CreateAnswer()
212 {
213 answerList->erase(answerList->begin(), answerList->end());
214
215 ADMIN_CONF conf;
216 conf.login = login;
217 if (!login.res_empty())
218     {
219     string s;
220     //if (admins->FindAdmin(login.data()) != NULL)
221     //    {
222         if (!password.res_empty())
223             conf.password = password.data();
224
225         if (!privAsString.res_empty())
226             {
227             int p = 0;
228             if (str2x(privAsString.data().c_str(), p) < 0)
229                 {
230                 strprintf(&s, "<ChgAdmin Result = \"Incorrect parameter Priv.\"/>" );
231                 answerList->push_back(s);
232                 return;
233                 }
234             //memcpy(&conf.priv, &p, sizeof(conf.priv));
235             conf.priv.userStat      = (p & 0x0003) >> 0x00; // 1+2
236             conf.priv.userConf      = (p & 0x000C) >> 0x02; // 4+8
237             conf.priv.userCash      = (p & 0x0030) >> 0x04; // 10+20
238             conf.priv.userPasswd    = (p & 0x00C0) >> 0x06; // 40+80
239             conf.priv.userAddDel    = (p & 0x0300) >> 0x08; // 100+200
240             conf.priv.adminChg      = (p & 0x0C00) >> 0x0A; // 400+800
241             conf.priv.tariffChg     = (p & 0x3000) >> 0x0C; // 1000+2000
242             }
243
244         if (admins->Change(conf, *currAdmin) != 0)
245             {
246             strprintf(&s, "<ChgAdmin Result = \"%s\"/>", admins->GetStrError().c_str());
247             answerList->push_back(s);
248             }
249         else
250             {
251             answerList->push_back("<ChgAdmin Result = \"Ok\"/>");
252             }
253         return;
254     //    }
255     //strprintf(&s, "<ChgAdmin Result = \"%s\"/>", admins->GetStrError().c_str());
256     //answerList->push_back(s);
257     //return;
258     }
259 else
260     {
261     answerList->push_back("<ChgAdmin Result = \"Incorrect parameter login.\"/>");
262     }
263 }
264 //-----------------------------------------------------------------------------*/
265