2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
22 #include "parser_admins.h"
24 #include "stg/admins.h"
25 #include "stg/admin.h"
26 #include "stg/admin_conf.h"
28 #include <strings.h> // strcasecmp
30 using STG::PARSER::GET_ADMINS;
31 using STG::PARSER::ADD_ADMIN;
32 using STG::PARSER::DEL_ADMIN;
33 using STG::PARSER::CHG_ADMIN;
35 const char * GET_ADMINS::tag = "GetAdmins";
36 const char * ADD_ADMIN::tag = "AddAdmin";
37 const char * DEL_ADMIN::tag = "DelAdmin";
38 const char * CHG_ADMIN::tag = "ChgAdmin";
40 void GET_ADMINS::CreateAnswer()
42 const auto priv = m_currAdmin.GetPriv();
45 m_answer = "<Error Result=\"Error. Access denied.\"/>";
49 m_answer = "<Admins>";
51 int h = m_admins.OpenSearch();
53 while (m_admins.SearchNext(h, &ac) == 0)
55 unsigned int p = (ac.priv.userStat << 0) +
56 (ac.priv.userConf << 2) +
57 (ac.priv.userCash << 4) +
58 (ac.priv.userPasswd << 6) +
59 (ac.priv.userAddDel << 8) +
60 (ac.priv.adminChg << 10) +
61 (ac.priv.tariffChg << 12);
62 m_answer += "<admin login=\"" + ac.login + "\" priv=\"" + std::to_string(p) + "\"/>";
64 m_admins.CloseSearch(h);
65 m_answer += "</Admins>";
68 int DEL_ADMIN::Start(void *, const char * el, const char ** attr)
70 if (strcasecmp(el, m_tag.c_str()) == 0)
78 void DEL_ADMIN::CreateAnswer()
80 if (m_admins.Del(m_admin, &m_currAdmin) == 0)
81 m_answer = "<" + m_tag + " Result=\"Ok\"/>";
83 m_answer = "<" + m_tag + " Result=\"Error. " + m_admins.GetStrError() + "\"/>";
86 int ADD_ADMIN::Start(void *, const char *el, const char **attr)
88 if (strcasecmp(el, m_tag.c_str()) == 0)
96 void ADD_ADMIN::CreateAnswer()
98 if (m_admins.Add(m_admin, &m_currAdmin) == 0)
99 m_answer = "<" + m_tag + " Result=\"Ok\"/>";
101 m_answer = "<" + m_tag + " Result=\"Error. " + m_admins.GetStrError() + "\"/>";
104 int CHG_ADMIN::Start(void *, const char * el, const char ** attr)
106 if (strcasecmp(el, m_tag.c_str()) == 0)
108 for (size_t i = 0; i < 6; i += 2)
110 printfd(__FILE__, "PARSER_CHG_ADMIN::attr[%d] = %s\n", i, attr[i]);
114 if (strcasecmp(attr[i], "Login") == 0)
120 if (strcasecmp(attr[i], "Priv") == 0)
122 privAsString = attr[i + 1];
126 if (strcasecmp(attr[i], "Password") == 0)
128 password = attr[i + 1];
138 void CHG_ADMIN::CreateAnswer()
142 Admin * origAdmin = NULL;
144 if (m_admins.Find(login, &origAdmin))
146 m_answer = "<" + m_tag + " Result = \"Admin '" + login + "' is not found.\"/>";
150 AdminConf conf(origAdmin->GetConf());
152 if (!password.empty())
153 conf.password = password.data();
155 if (!privAsString.empty())
158 if (str2x(privAsString.data().c_str(), p) < 0)
160 m_answer = "<" + m_tag + " Result = \"Incorrect parameter Priv.\"/>";
167 if (m_admins.Change(conf, &m_currAdmin) != 0)
168 m_answer = "<" + m_tag + " Result = \"" + m_admins.GetStrError() + "\"/>";
170 m_answer = "<" + m_tag + " Result = \"Ok\"/>";
173 m_answer = "<" + m_tag + " Result = \"Incorrect parameter login.\"/>";