]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser_admins.cpp
Implemented parser registry.
[stg.git] / projects / stargazer / plugins / configuration / sgconfig / parser_admins.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  *    Author : Maxim Mamontov <faust@stargazer.dp.ua>
20  */
21
22 #include "parser_admins.h"
23
24 #include "stg/admins.h"
25 #include "stg/common.h"
26
27 #include <strings.h> // strcasecmp
28
29 using STG::PARSER::GET_ADMINS;
30 using STG::PARSER::ADD_ADMIN;
31 using STG::PARSER::DEL_ADMIN;
32 using STG::PARSER::CHG_ADMIN;
33
34 const char * GET_ADMINS::tag = "GetAdmins";
35 const char * ADD_ADMIN::tag  = "AddAdmin";
36 const char * DEL_ADMIN::tag  = "DelAdmin";
37 const char * CHG_ADMIN::tag  = "ChgAdmin";
38
39 void GET_ADMINS::CreateAnswer()
40 {
41     const PRIV * priv = m_currAdmin.GetPriv();
42     if (!priv->adminChg)
43     {
44         m_answer = "<Error Result=\"Error. Access denied.\"/>";
45         return;
46     }
47
48     m_answer.clear();
49
50     m_answer += GetOpenTag();
51     ADMIN_CONF ac;
52     int h = m_admins.OpenSearch();
53
54     while (m_admins.SearchNext(h, &ac) == 0)
55     {
56         unsigned int p = (ac.priv.userStat << 0) +
57                          (ac.priv.userConf << 2) +
58                          (ac.priv.userCash << 4) +
59                          (ac.priv.userPasswd << 6) +
60                          (ac.priv.userAddDel << 8) +
61                          (ac.priv.adminChg << 10) +
62                          (ac.priv.tariffChg << 12);
63         m_answer += "<admin login=\"" + ac.login + "\" priv=\"" + x2str(p) + "\"/>";
64     }
65     m_admins.CloseSearch(h);
66     m_answer += GetCloseTag();
67 }
68
69 int DEL_ADMIN::Start(void *, const char * el, const char ** attr)
70 {
71     if (strcasecmp(el, m_tag.c_str()) == 0)
72     {
73         m_admin = attr[1];
74         return 0;
75     }
76     return -1;
77 }
78
79 void DEL_ADMIN::CreateAnswer()
80 {
81     if (m_admins.Del(m_admin, &m_currAdmin) == 0)
82         m_answer = "<" + m_tag + " Result=\"Ok\"/>";
83     else
84         m_answer = "<" + m_tag + " Result=\"Error. " + m_admins.GetStrError() + "\"/>";
85 }
86
87 int ADD_ADMIN::Start(void *, const char *el, const char **attr)
88 {
89     if (strcasecmp(el, m_tag.c_str()) == 0)
90     {
91         m_admin = attr[1];
92         return 0;
93     }
94     return -1;
95 }
96
97 void ADD_ADMIN::CreateAnswer()
98 {
99     if (m_admins.Add(m_admin, &m_currAdmin) == 0)
100         m_answer = "<" + m_tag + " Result=\"Ok\"/>";
101     else
102         m_answer = "<" + m_tag + " Result=\"Error. " + m_admins.GetStrError() + "\"/>";
103 }
104
105 int CHG_ADMIN::Start(void *, const char * el, const char ** attr)
106 {
107     if (strcasecmp(el, m_tag.c_str()) == 0)
108     {
109         for (size_t i = 0; i < 6; i += 2)
110         {
111             printfd(__FILE__, "PARSER_CHG_ADMIN::attr[%d] = %s\n", i, attr[i]);
112             if (attr[i] == NULL)
113                 break;
114
115             if (strcasecmp(attr[i], "Login") == 0)
116             {
117                 login = attr[i + 1];
118                 continue;
119             }
120
121             if (strcasecmp(attr[i], "Priv") == 0)
122             {
123                 privAsString = attr[i + 1];
124                 continue;
125             }
126
127             if (strcasecmp(attr[i], "Password") == 0)
128             {
129                 password = attr[i + 1];
130                 continue;
131             }
132         }
133
134         return 0;
135     }
136     return -1;
137 }
138
139 void CHG_ADMIN::CreateAnswer()
140 {
141     if (!login.empty())
142     {
143         ADMIN * origAdmin = NULL;
144
145         if (m_admins.Find(login, &origAdmin))
146         {
147             m_answer = "<" + m_tag + " Result = \"Admin '" + login + "' is not found.\"/>";
148             return;
149         }
150
151         ADMIN_CONF conf(origAdmin->GetConf());
152
153         if (!password.empty())
154             conf.password = password.data();
155
156         if (!privAsString.empty())
157         {
158             int p = 0;
159             if (str2x(privAsString.data().c_str(), p) < 0)
160             {
161                 m_answer = "<" + m_tag + " Result = \"Incorrect parameter Priv.\"/>";
162                 return;
163             }
164
165             conf.priv.FromInt(p);
166         }
167
168         if (m_admins.Change(conf, &m_currAdmin) != 0)
169             m_answer = "<" + m_tag + " Result = \"" + m_admins.GetStrError() + "\"/>";
170         else
171             m_answer = "<" + m_tag + " Result = \"Ok\"/>";
172     }
173     else
174         m_answer = "<" + m_tag + " Result = \"Incorrect parameter login.\"/>";
175 }