]> git.stg.codes - stg.git/blob - projects/stargazer/admins_impl.cpp
Fix compilation errors after splitting classes into interface and
[stg.git] / projects / stargazer / admins_impl.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  *    Date: 27.10.2002
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23  */
24
25  /*
26  $Revision: 1.15 $
27  $Date: 2010/10/04 20:17:12 $
28  $Author: faust $
29  */
30
31 #include <cerrno>
32 #include <cassert>
33 #include <algorithm>
34
35 #include "admins_impl.h"
36 #include "admins.h"
37 #include "admin.h"
38 #include "admin_impl.h"
39 #include "common.h"
40
41 using namespace std;
42
43 //-----------------------------------------------------------------------------
44 ADMINS_IMPL::ADMINS_IMPL(BASE_STORE * st)
45     : stg(0xFFFF, "@stargazer", ""),
46       noAdmin(0xFFFF, "NO-ADMIN", ""),
47       data(),
48       store(st),
49       WriteServLog(GetStgLogger()),
50       searchDescriptors(),
51       handle(0)
52 {
53 pthread_mutex_init(&mutex, NULL);
54 ReadAdmins();
55 }
56 //-----------------------------------------------------------------------------
57 int ADMINS_IMPL::Add(const string & login, const ADMIN & admin)
58 {
59 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
60 const PRIV * priv = admin.GetPriv();
61
62 if (!priv->adminChg)
63     {
64     string s = admin.GetLogStr() + " Add administrator \'" + login + "\'. Access denied.";
65     strError = "Access denied.";
66     WriteServLog(s.c_str());
67     return -1;
68     }
69
70 ADMIN_IMPL adm(0, login, "");
71 admin_iter ai(find(data.begin(), data.end(), adm));
72
73 if (ai != data.end())
74     {
75     strError = "Administrator \'" + login + "\' cannot not be added. Administrator alredy exist.";
76     WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str());
77
78     return -1;
79     }
80
81 data.push_back(adm);
82
83 if (store->AddAdmin(login) == 0)
84     {
85     WriteServLog("%s Administrator \'%s\' added.",
86                  admin.GetLogStr().c_str(), login.c_str());
87     return 0;
88     }
89
90 strError = "Administrator \'" + login + "\' was not added. Error: " + store->GetStrError();
91 WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str());
92
93 return -1;
94 }
95 //-----------------------------------------------------------------------------
96 int ADMINS_IMPL::Del(const string & login, const ADMIN & admin)
97 {
98 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
99 ADMIN_IMPL adm(0, login, "");
100 const PRIV * priv = admin.GetPriv();
101
102 if (!priv->adminChg)
103     {
104     string s = admin.GetLogStr() + " Delete administrator \'" + login + "\'. Access denied.";
105     strError = "Access denied.";
106     WriteServLog(s.c_str());
107     return -1;
108     }
109
110 admin_iter ai(find(data.begin(), data.end(), adm));
111
112 if (ai == data.end())
113     {
114     strError = "Administrator \'" + login + "\' cannot be deleted. Administrator does not exist.";
115     WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str());
116     return -1;
117     }
118
119 map<int, const_admin_iter>::iterator si;
120 si = searchDescriptors.begin();
121 while (si != searchDescriptors.end())
122     {
123     if (si->second == ai)
124         (si->second)++;
125     si++;
126     }
127
128 data.remove(*ai);
129 if (store->DelAdmin(login) < 0)
130     {
131     strError = "Administrator \'" + login + "\' was not deleted. Error: " + store->GetStrError();
132     WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str());
133
134     return -1;
135     }
136
137 WriteServLog("%s Administrator \'%s\' deleted.", admin.GetLogStr().c_str(), login.c_str());
138 return 0;
139 }
140 //-----------------------------------------------------------------------------
141 int ADMINS_IMPL::Change(const ADMIN_CONF & ac, const ADMIN & admin)
142 {
143 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
144 const PRIV * priv = admin.GetPriv();
145
146 if (!priv->adminChg)
147     {
148     string s = admin.GetLogStr() + " Change administrator \'" + ac.login + "\'. Access denied.";
149     strError = "Access denied.";
150     WriteServLog(s.c_str());
151     return -1;
152     }
153
154 ADMIN_IMPL adm(0, ac.login, "");
155 admin_iter ai(find(data.begin(), data.end(), adm));
156
157 if (ai == data.end())
158     {
159     strError = "Administrator \'" + ac.login + "\' cannot be changed " + ". Administrator does not exist.";
160     WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str());
161     return -1;
162     }
163
164 *ai = ac;
165 if (store->SaveAdmin(ac))
166     {
167     WriteServLog("Cannot write admin %s.", ac.login.c_str());
168     WriteServLog("%s", store->GetStrError().c_str());
169     return -1;
170     }
171
172 WriteServLog("%s Administrator \'%s\' changed.",
173              admin.GetLogStr().c_str(), ac.login.c_str());
174
175 return 0;
176 }
177 //-----------------------------------------------------------------------------
178 int ADMINS_IMPL::ReadAdmins()
179 {
180 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
181 vector<string> adminsList;
182 if (store->GetAdminsList(&adminsList) < 0)
183     {
184     WriteServLog(store->GetStrError().c_str());
185     return -1;
186     }
187
188 for (unsigned int i = 0; i < adminsList.size(); i++)
189     {
190     ADMIN_CONF ac(0, adminsList[i], "");
191
192     if (store->RestoreAdmin(&ac, adminsList[i]))
193         {
194         WriteServLog(store->GetStrError().c_str());
195         return -1;
196         }
197
198     data.push_back(ADMIN_IMPL(ac));
199     }
200 return 0;
201 }
202 //-----------------------------------------------------------------------------
203 void ADMINS_IMPL::PrintAdmins() const
204 {
205 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
206 const_admin_iter ai(data.begin());
207 while (ai != data.end())
208     {
209     ai->Print();
210     ai++;
211     }
212 }
213 //-----------------------------------------------------------------------------
214 bool ADMINS_IMPL::FindAdmin(const string & l, ADMIN ** admin)
215 {
216 assert(admin != NULL && "Pointer to admin is not null");
217
218 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
219 if (data.empty())
220     {
221     printfd(__FILE__, "no admin in system!\n");
222     *admin = &noAdmin;
223     return false;
224     }
225
226 ADMIN_IMPL adm(0, l, "");
227 admin_iter ai(find(data.begin(), data.end(), adm));
228
229 if (ai != data.end())
230     {
231     *admin = &(*ai);
232     return false;
233     }
234
235 return true;
236 }
237 //-----------------------------------------------------------------------------
238 bool ADMINS_IMPL::AdminExists(const string & login) const
239 {
240 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
241 if (data.empty())
242     {
243     printfd(__FILE__, "no admin in system!\n");
244     return true;
245     }
246
247 ADMIN_IMPL adm(0, login, "");
248 const_admin_iter ai(find(data.begin(), data.end(), adm));
249
250 if (ai != data.end())
251     return true;
252
253 return false;
254 }
255 //-----------------------------------------------------------------------------
256 bool ADMINS_IMPL::AdminCorrect(const string & login, const std::string & password, ADMIN * admin) const
257 {
258 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
259 if (data.empty())
260     {
261     printfd(__FILE__, "no admin in system!\n");
262     return true;
263     }
264
265 ADMIN_IMPL adm(0, login, "");
266 const_admin_iter ai(find(data.begin(), data.end(), adm));
267
268 if (ai == data.end())
269     {
270     return false;
271     }
272
273 if (ai->GetPassword() != password)
274     {
275     return false;
276     }
277
278 *admin = *ai;
279
280 return true;
281 }
282 //-----------------------------------------------------------------------------
283 int ADMINS_IMPL::OpenSearch() const
284 {
285 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
286 handle++;
287 searchDescriptors[handle] = data.begin();
288 return handle;
289 }
290 //-----------------------------------------------------------------------------
291 int ADMINS_IMPL::SearchNext(int h, ADMIN_CONF * ac) const
292 {
293 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
294 if (searchDescriptors.find(h) == searchDescriptors.end())
295     {
296     WriteServLog("ADMINS. Incorrect search handle.");
297     return -1;
298     }
299
300 if (searchDescriptors[h] == data.end())
301     return -1;
302
303 ADMIN_IMPL a = *searchDescriptors[h]++;
304
305 *ac = a.GetConf();
306
307 return 0;
308 }
309 //-----------------------------------------------------------------------------
310 int ADMINS_IMPL::CloseSearch(int h) const
311 {
312 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
313 if (searchDescriptors.find(h) != searchDescriptors.end())
314     {
315     searchDescriptors.erase(searchDescriptors.find(h));
316     return 0;
317     }
318
319 WriteServLog("ADMINS. Incorrect search handle.");
320 return -1;
321 }
322 //-----------------------------------------------------------------------------