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/admins.h"
36 #include "stg/admin.h"
37 #include "stg/common.h"
38 #include "admins_impl.h"
39 #include "admin_impl.h"
43 //-----------------------------------------------------------------------------
44 ADMINS_IMPL::ADMINS_IMPL(STORE * st)
45 : stg(0xFFFF, "@stargazer", ""),
46 noAdmin(0xFFFF, "NO-ADMIN", ""),
49 WriteServLog(GetStgLogger()),
53 pthread_mutex_init(&mutex, NULL);
56 //-----------------------------------------------------------------------------
57 int ADMINS_IMPL::Add(const string & login, const ADMIN * admin)
59 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
60 const PRIV * priv = admin->GetPriv();
64 string s = admin->GetLogStr() + " Add administrator \'" + login + "\'. Access denied.";
65 strError = "Access denied.";
66 WriteServLog(s.c_str());
70 ADMIN_IMPL adm(0, login, "");
71 admin_iter ai(find(data.begin(), data.end(), adm));
75 strError = "Administrator \'" + login + "\' cannot not be added. Administrator already exist.";
76 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
83 if (store->AddAdmin(login) == 0)
85 WriteServLog("%s Administrator \'%s\' added.",
86 admin->GetLogStr().c_str(), login.c_str());
90 strError = "Administrator \'" + login + "\' was not added. Error: " + store->GetStrError();
91 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
95 //-----------------------------------------------------------------------------
96 int ADMINS_IMPL::Del(const string & login, const ADMIN * admin)
98 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
99 ADMIN_IMPL adm(0, login, "");
100 const PRIV * priv = admin->GetPriv();
104 string s = admin->GetLogStr() + " Delete administrator \'" + login + "\'. Access denied.";
105 strError = "Access denied.";
106 WriteServLog(s.c_str());
110 admin_iter ai(find(data.begin(), data.end(), adm));
112 if (ai == data.end())
114 strError = "Administrator \'" + login + "\' cannot be deleted. Administrator does not exist.";
115 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
119 map<int, const_admin_iter>::iterator si;
120 si = searchDescriptors.begin();
121 while (si != searchDescriptors.end())
123 if (si->second == ai)
129 if (store->DelAdmin(login) < 0)
131 strError = "Administrator \'" + login + "\' was not deleted. Error: " + store->GetStrError();
132 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
137 WriteServLog("%s Administrator \'%s\' deleted.", admin->GetLogStr().c_str(), login.c_str());
140 //-----------------------------------------------------------------------------
141 int ADMINS_IMPL::Change(const ADMIN_CONF & ac, const ADMIN * admin)
143 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
144 const PRIV * priv = admin->GetPriv();
148 string s = admin->GetLogStr() + " Change administrator \'" + ac.login + "\'. Access denied.";
149 strError = "Access denied.";
150 WriteServLog(s.c_str());
154 ADMIN_IMPL adm(0, ac.login, "");
155 admin_iter ai(find(data.begin(), data.end(), adm));
157 if (ai == data.end())
159 strError = "Administrator \'" + ac.login + "\' cannot be changed " + ". Administrator does not exist.";
160 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
165 if (store->SaveAdmin(ac))
167 WriteServLog("Cannot write admin %s.", ac.login.c_str());
168 WriteServLog("%s", store->GetStrError().c_str());
172 WriteServLog("%s Administrator \'%s\' changed.",
173 admin->GetLogStr().c_str(), ac.login.c_str());
177 //-----------------------------------------------------------------------------
178 int ADMINS_IMPL::ReadAdmins()
180 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
181 vector<string> adminsList;
182 if (store->GetAdminsList(&adminsList) < 0)
184 WriteServLog(store->GetStrError().c_str());
188 for (unsigned int i = 0; i < adminsList.size(); i++)
190 ADMIN_CONF ac(0, adminsList[i], "");
192 if (store->RestoreAdmin(&ac, adminsList[i]))
194 WriteServLog(store->GetStrError().c_str());
198 data.push_back(ADMIN_IMPL(ac));
202 //-----------------------------------------------------------------------------
203 void ADMINS_IMPL::PrintAdmins() const
205 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
206 const_admin_iter ai(data.begin());
207 while (ai != data.end())
213 //-----------------------------------------------------------------------------
214 bool ADMINS_IMPL::FindAdmin(const string & l, ADMIN ** admin)
216 assert(admin != NULL && "Pointer to admin is not null");
218 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
221 printfd(__FILE__, "no admin in system!\n");
226 ADMIN_IMPL adm(0, l, "");
227 admin_iter ai(find(data.begin(), data.end(), adm));
229 if (ai != data.end())
237 //-----------------------------------------------------------------------------
238 bool ADMINS_IMPL::AdminExists(const string & login) const
240 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
243 printfd(__FILE__, "no admin in system!\n");
247 ADMIN_IMPL adm(0, login, "");
248 const_admin_iter ai(find(data.begin(), data.end(), adm));
250 if (ai != data.end())
255 //-----------------------------------------------------------------------------
256 bool ADMINS_IMPL::AdminCorrect(const string & login, const std::string & password, ADMIN ** admin)
258 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
261 printfd(__FILE__, "no admin in system!\n");
265 ADMIN_IMPL adm(0, login, "");
266 admin_iter ai(find(data.begin(), data.end(), adm));
268 if (ai == data.end())
273 if (ai->GetPassword() != password)
282 //-----------------------------------------------------------------------------
283 int ADMINS_IMPL::OpenSearch() const
285 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
287 searchDescriptors[handle] = data.begin();
290 //-----------------------------------------------------------------------------
291 int ADMINS_IMPL::SearchNext(int h, ADMIN_CONF * ac) const
293 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
294 if (searchDescriptors.find(h) == searchDescriptors.end())
296 WriteServLog("ADMINS. Incorrect search handle.");
300 if (searchDescriptors[h] == data.end())
303 ADMIN_IMPL a = *searchDescriptors[h]++;
309 //-----------------------------------------------------------------------------
310 int ADMINS_IMPL::CloseSearch(int h) const
312 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
313 if (searchDescriptors.find(h) != searchDescriptors.end())
315 searchDescriptors.erase(searchDescriptors.find(h));
319 WriteServLog("ADMINS. Incorrect search handle.");
322 //-----------------------------------------------------------------------------