]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/configuration/sgconfig/parser_admins.cpp
Finished new implementation of sgconfig plugin.
[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 = "<Admins>";
49     ADMIN_CONF ac;
50     int h = m_admins.OpenSearch();
51
52     while (m_admins.SearchNext(h, &ac) == 0)
53     {
54         unsigned int p = (ac.priv.userStat << 0) +
55                          (ac.priv.userConf << 2) +
56                          (ac.priv.userCash << 4) +
57                          (ac.priv.userPasswd << 6) +
58                          (ac.priv.userAddDel << 8) +
59                          (ac.priv.adminChg << 10) +
60                          (ac.priv.tariffChg << 12);
61         m_answer += "<admin login=\"" + ac.login + "\" priv=\"" + x2str(p) + "\"/>";
62     }
63     m_admins.CloseSearch(h);
64     m_answer += "</Admins>";
65 }
66
67 int DEL_ADMIN::Start(void *, const char * el, const char ** attr)
68 {
69     if (strcasecmp(el, m_tag.c_str()) == 0)
70     {
71         m_admin = attr[1];
72         return 0;
73     }
74     return -1;
75 }
76
77 void DEL_ADMIN::CreateAnswer()
78 {
79     if (m_admins.Del(m_admin, &m_currAdmin) == 0)
80         m_answer = "<" + m_tag + " Result=\"Ok\"/>";
81     else
82         m_answer = "<" + m_tag + " Result=\"Error. " + m_admins.GetStrError() + "\"/>";
83 }
84
85 int ADD_ADMIN::Start(void *, const char *el, const char **attr)
86 {
87     if (strcasecmp(el, m_tag.c_str()) == 0)
88     {
89         m_admin = attr[1];
90         return 0;
91     }
92     return -1;
93 }
94
95 void ADD_ADMIN::CreateAnswer()
96 {
97     if (m_admins.Add(m_admin, &m_currAdmin) == 0)
98         m_answer = "<" + m_tag + " Result=\"Ok\"/>";
99     else
100         m_answer = "<" + m_tag + " Result=\"Error. " + m_admins.GetStrError() + "\"/>";
101 }
102
103 int CHG_ADMIN::Start(void *, const char * el, const char ** attr)
104 {
105     if (strcasecmp(el, m_tag.c_str()) == 0)
106     {
107         for (size_t i = 0; i < 6; i += 2)
108         {
109             printfd(__FILE__, "PARSER_CHG_ADMIN::attr[%d] = %s\n", i, attr[i]);
110             if (attr[i] == NULL)
111                 break;
112
113             if (strcasecmp(attr[i], "Login") == 0)
114             {
115                 login = attr[i + 1];
116                 continue;
117             }
118
119             if (strcasecmp(attr[i], "Priv") == 0)
120             {
121                 privAsString = attr[i + 1];
122                 continue;
123             }
124
125             if (strcasecmp(attr[i], "Password") == 0)
126             {
127                 password = attr[i + 1];
128                 continue;
129             }
130         }
131
132         return 0;
133     }
134     return -1;
135 }
136
137 void CHG_ADMIN::CreateAnswer()
138 {
139     if (!login.empty())
140     {
141         ADMIN * origAdmin = NULL;
142
143         if (m_admins.Find(login, &origAdmin))
144         {
145             m_answer = "<" + m_tag + " Result = \"Admin '" + login + "' is not found.\"/>";
146             return;
147         }
148
149         ADMIN_CONF conf(origAdmin->GetConf());
150
151         if (!password.empty())
152             conf.password = password.data();
153
154         if (!privAsString.empty())
155         {
156             int p = 0;
157             if (str2x(privAsString.data().c_str(), p) < 0)
158             {
159                 m_answer = "<" + m_tag + " Result = \"Incorrect parameter Priv.\"/>";
160                 return;
161             }
162
163             conf.priv.FromInt(p);
164         }
165
166         if (m_admins.Change(conf, &m_currAdmin) != 0)
167             m_answer = "<" + m_tag + " Result = \"" + m_admins.GetStrError() + "\"/>";
168         else
169             m_answer = "<" + m_tag + " Result = \"Ok\"/>";
170     }
171     else
172         m_answer = "<" + m_tag + " Result = \"Incorrect parameter login.\"/>";
173 }