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 $
41 //-----------------------------------------------------------------------------
42 ADMINS::ADMINS(BASE_STORE * st)
43 : stg(0xFFFF, "@stargazer", ""),
44 noAdmin(0xFFFF, "NO-ADMIN", ""),
47 WriteServLog(GetStgLogger()),
51 pthread_mutex_init(&mutex, NULL);
54 //-----------------------------------------------------------------------------
55 int ADMINS::Add(const string & login, const ADMIN & admin)
57 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
58 const PRIV * priv = admin.GetPriv();
62 string s = admin.GetLogStr() + " Add administrator \'" + login + "\'. Access denied.";
63 strError = "Access denied.";
64 WriteServLog(s.c_str());
68 ADMIN adm(0, login, "");
69 admin_iter ai(find(data.begin(), data.end(), adm));
73 strError = "Administrator \'" + login + "\' cannot not be added. Administrator alredy exist.";
74 WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str());
82 if (store->AddAdmin(login) == 0 /*&& store->SaveAdmin(ac) == 0*/)
84 WriteServLog("%s Administrator \'%s\' added.",
85 admin.GetLogStr().c_str(), login.c_str());
89 strError = "Administrator \'" + login + "\' was not added. Error: " + store->GetStrError();
90 WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str());
94 //-----------------------------------------------------------------------------
95 int ADMINS::Del(const string & login, const ADMIN & admin)
97 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
98 ADMIN adm(0, login, "");
99 const PRIV * priv = admin.GetPriv();
103 string s = admin.GetLogStr() + " Delete administrator \'" + login + "\'. Access denied.";
104 strError = "Access denied.";
105 WriteServLog(s.c_str());
109 admin_iter ai(find(data.begin(), data.end(), adm));
111 if (ai == data.end())
113 strError = "Administrator \'" + login + "\' cannot be deleted. Administrator does not exist.";
114 WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str());
118 map<int, const_admin_iter>::iterator si;
119 si = searchDescriptors.begin();
120 while (si != searchDescriptors.end())
122 if (si->second == ai)
128 if (store->DelAdmin(login) < 0)
130 strError = "Administrator \'" + login + "\' was not deleted. Error: " + store->GetStrError();
131 WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str());
136 WriteServLog("%s Administrator \'%s\' deleted.", admin.GetLogStr().c_str(), login.c_str());
139 //-----------------------------------------------------------------------------
140 int ADMINS::Change(const ADMIN_CONF & ac, const ADMIN & admin)
142 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
143 const PRIV * priv = admin.GetPriv();
147 string s = admin.GetLogStr() + " Change administrator \'" + ac.login + "\'. Access denied.";
148 strError = "Access denied.";
149 WriteServLog(s.c_str());
153 ADMIN adm(0, ac.login, "");
154 admin_iter ai(find(data.begin(), data.end(), adm));
156 if (ai == data.end())
158 strError = "Administrator \'" + ac.login + "\' cannot be changed " + ". Administrator does not exist.";
159 WriteServLog("%s %s", admin.GetLogStr().c_str(), strError.c_str());
164 if (store->SaveAdmin(ac))
166 WriteServLog("Cannot write admin %s.", ac.login.c_str());
167 WriteServLog("%s", store->GetStrError().c_str());
171 WriteServLog("%s Administrator \'%s\' changed.",
172 admin.GetLogStr().c_str(), ac.login.c_str());
176 //-----------------------------------------------------------------------------
177 int ADMINS::ReadAdmins()
179 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
180 vector<string> adminsList;
181 if (store->GetAdminsList(&adminsList) < 0)
183 WriteServLog(store->GetStrError().c_str());
187 for (unsigned int i = 0; i < adminsList.size(); i++)
189 ADMIN_CONF ac(0, adminsList[i], "");
191 if (store->RestoreAdmin(&ac, adminsList[i]))
193 WriteServLog(store->GetStrError().c_str());
197 data.push_back(ADMIN(ac));
201 //-----------------------------------------------------------------------------
202 void ADMINS::PrintAdmins() const
204 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
205 const_admin_iter ai(data.begin());
206 while (ai != data.end())
212 //-----------------------------------------------------------------------------
213 bool ADMINS::FindAdmin(const string & l, ADMIN * admin) const
215 assert(admin != NULL && "Pointer to admin is not null");
217 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
220 printfd(__FILE__, "no admin in system!\n");
226 const_admin_iter ai(find(data.begin(), data.end(), adm));
228 if (ai != data.end())
236 //-----------------------------------------------------------------------------
237 bool ADMINS::AdminExists(const string & login) const
239 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
242 printfd(__FILE__, "no admin in system!\n");
246 ADMIN adm(0, login, "");
247 const_admin_iter ai(find(data.begin(), data.end(), adm));
249 if (ai != data.end())
254 //-----------------------------------------------------------------------------
255 bool ADMINS::AdminCorrect(const string & login, const std::string & password, ADMIN * admin) const
257 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
260 printfd(__FILE__, "no admin in system!\n");
264 ADMIN adm(0, login, "");
265 const_admin_iter ai(find(data.begin(), data.end(), adm));
267 if (ai == data.end())
272 if (ai->GetPassword() != password)
281 //-----------------------------------------------------------------------------
282 int ADMINS::OpenSearch() const
284 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
286 searchDescriptors[handle] = data.begin();
289 //-----------------------------------------------------------------------------
290 int ADMINS::SearchNext(int h, ADMIN_CONF * ac) const
292 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
293 if (searchDescriptors.find(h) == searchDescriptors.end())
295 WriteServLog("ADMINS. Incorrect search handle.");
299 if (searchDescriptors[h] == data.end())
302 ADMIN a = *searchDescriptors[h]++;
308 //-----------------------------------------------------------------------------
309 int ADMINS::CloseSearch(int h) const
311 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
312 if (searchDescriptors.find(h) != searchDescriptors.end())
314 searchDescriptors.erase(searchDescriptors.find(h));
318 WriteServLog("ADMINS. Incorrect search handle.");
321 //-----------------------------------------------------------------------------