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
22 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
27 $Date: 2010/10/04 20:17:12 $
35 #include "stg/common.h"
36 #include "admins_impl.h"
37 #include "admin_impl.h"
41 //-----------------------------------------------------------------------------
42 ADMINS_IMPL::ADMINS_IMPL(STORE * st)
44 stg(0xFFFF, "@stargazer", ""),
45 noAdmin(0xFFFF, "NO-ADMIN", ""),
48 WriteServLog(GetStgLogger()),
54 pthread_mutex_init(&mutex, NULL);
57 //-----------------------------------------------------------------------------
58 int ADMINS_IMPL::Add(const string & login, const ADMIN * admin)
60 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
61 const PRIV * priv = admin->GetPriv();
65 string s = admin->GetLogStr() + " Add administrator \'" + login + "\'. Access denied.";
66 strError = "Access denied.";
67 WriteServLog(s.c_str());
71 ADMIN_IMPL adm(0, login, "");
72 admin_iter ai(find(data.begin(), data.end(), adm));
76 strError = "Administrator \'" + login + "\' cannot not be added. Administrator already exist.";
77 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
84 if (store->AddAdmin(login) == 0)
86 WriteServLog("%s Administrator \'%s\' added.",
87 admin->GetLogStr().c_str(), login.c_str());
91 strError = "Administrator \'" + login + "\' was not added. Error: " + store->GetStrError();
92 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
96 //-----------------------------------------------------------------------------
97 int ADMINS_IMPL::Del(const string & login, const ADMIN * admin)
99 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
100 ADMIN_IMPL adm(0, login, "");
101 const PRIV * priv = admin->GetPriv();
105 string s = admin->GetLogStr() + " Delete administrator \'" + login + "\'. Access denied.";
106 strError = "Access denied.";
107 WriteServLog(s.c_str());
111 admin_iter ai(find(data.begin(), data.end(), adm));
113 if (ai == data.end())
115 strError = "Administrator \'" + login + "\' cannot be deleted. Administrator does not exist.";
116 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
120 map<int, const_admin_iter>::iterator si;
121 si = searchDescriptors.begin();
122 while (si != searchDescriptors.end())
124 if (si->second == ai)
130 if (store->DelAdmin(login) < 0)
132 strError = "Administrator \'" + login + "\' was not deleted. Error: " + store->GetStrError();
133 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
138 WriteServLog("%s Administrator \'%s\' deleted.", admin->GetLogStr().c_str(), login.c_str());
141 //-----------------------------------------------------------------------------
142 int ADMINS_IMPL::Change(const ADMIN_CONF & ac, const ADMIN * admin)
144 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
145 const PRIV * priv = admin->GetPriv();
149 string s = admin->GetLogStr() + " Change administrator \'" + ac.login + "\'. Access denied.";
150 strError = "Access denied.";
151 WriteServLog(s.c_str());
155 ADMIN_IMPL adm(0, ac.login, "");
156 admin_iter ai(find(data.begin(), data.end(), adm));
158 if (ai == data.end())
160 strError = "Administrator \'" + ac.login + "\' cannot be changed " + ". Administrator does not exist.";
161 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
166 if (store->SaveAdmin(ac))
168 WriteServLog("Cannot write admin %s.", ac.login.c_str());
169 WriteServLog("%s", store->GetStrError().c_str());
173 WriteServLog("%s Administrator \'%s\' changed.",
174 admin->GetLogStr().c_str(), ac.login.c_str());
178 //-----------------------------------------------------------------------------
179 int ADMINS_IMPL::Read()
181 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
182 vector<string> adminsList;
183 if (store->GetAdminsList(&adminsList) < 0)
185 WriteServLog(store->GetStrError().c_str());
189 for (unsigned int i = 0; i < adminsList.size(); i++)
191 ADMIN_CONF ac(0, adminsList[i], "");
193 if (store->RestoreAdmin(&ac, adminsList[i]))
195 WriteServLog(store->GetStrError().c_str());
199 data.push_back(ADMIN_IMPL(ac));
203 //-----------------------------------------------------------------------------
204 void ADMINS_IMPL::PrintAdmins() const
206 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
207 const_admin_iter ai(data.begin());
208 while (ai != data.end())
214 //-----------------------------------------------------------------------------
215 bool ADMINS_IMPL::Find(const string & l, ADMIN ** admin)
217 assert(admin != NULL && "Pointer to admin is not null");
219 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
222 printfd(__FILE__, "no admin in system!\n");
227 ADMIN_IMPL adm(0, l, "");
228 admin_iter ai(find(data.begin(), data.end(), adm));
230 if (ai != data.end())
238 //-----------------------------------------------------------------------------
239 bool ADMINS_IMPL::Exists(const string & login) const
241 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
244 printfd(__FILE__, "no admin in system!\n");
248 ADMIN_IMPL adm(0, login, "");
249 const_admin_iter ai(find(data.begin(), data.end(), adm));
251 if (ai != data.end())
256 //-----------------------------------------------------------------------------
257 bool ADMINS_IMPL::Correct(const string & login, const std::string & password, ADMIN ** admin)
259 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
262 printfd(__FILE__, "no admin in system!\n");
266 ADMIN_IMPL adm(0, login, "");
267 admin_iter ai(find(data.begin(), data.end(), adm));
269 if (ai == data.end())
274 if (ai->GetPassword() != password)
283 //-----------------------------------------------------------------------------
284 int ADMINS_IMPL::OpenSearch() const
286 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
288 searchDescriptors[handle] = data.begin();
291 //-----------------------------------------------------------------------------
292 int ADMINS_IMPL::SearchNext(int h, ADMIN_CONF * ac) const
294 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
295 if (searchDescriptors.find(h) == searchDescriptors.end())
297 WriteServLog("ADMINS. Incorrect search handle.");
301 if (searchDescriptors[h] == data.end())
304 ADMIN_IMPL a = *searchDescriptors[h]++;
310 //-----------------------------------------------------------------------------
311 int ADMINS_IMPL::CloseSearch(int h) const
313 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
314 if (searchDescriptors.find(h) != searchDescriptors.end())
316 searchDescriptors.erase(searchDescriptors.find(h));
320 WriteServLog("ADMINS. Incorrect search handle.");
323 //-----------------------------------------------------------------------------