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
18 * Author : Maxim Mamontov <faust@stargazer.dp.ua>
25 #include "stg/admin.h"
26 #include "stg/common.h"
27 #include "services_impl.h"
29 //-----------------------------------------------------------------------------
30 SERVICES_IMPL::SERVICES_IMPL(STORE * st)
34 WriteServLog(GetStgLogger()),
38 pthread_mutex_init(&mutex, NULL);
41 //-----------------------------------------------------------------------------
42 int SERVICES_IMPL::Add(const SERVICE_CONF & service, const ADMIN * admin)
44 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
45 const PRIV * priv = admin->GetPriv();
49 string s = admin->GetLogStr() + " Add administrator \'" + login + "\'. Access denied.";
50 strError = "Access denied.";
51 WriteServLog(s.c_str());
55 ADMIN_IMPL adm(0, login, "");
56 admin_iter ai(find(data.begin(), data.end(), adm));
60 strError = "Administrator \'" + login + "\' cannot not be added. Administrator already exist.";
61 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
68 if (store->AddAdmin(login) == 0)
70 WriteServLog("%s Administrator \'%s\' added.",
71 admin->GetLogStr().c_str(), login.c_str());
75 strError = "Administrator \'" + login + "\' was not added. Error: " + store->GetStrError();
76 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
80 //-----------------------------------------------------------------------------
81 int SERVICES_IMPL::Del(const string & login, const ADMIN * admin)
83 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
84 ADMIN_IMPL adm(0, login, "");
85 const PRIV * priv = admin->GetPriv();
89 string s = admin->GetLogStr() + " Delete administrator \'" + login + "\'. Access denied.";
90 strError = "Access denied.";
91 WriteServLog(s.c_str());
95 admin_iter ai(find(data.begin(), data.end(), adm));
99 strError = "Administrator \'" + login + "\' cannot be deleted. Administrator does not exist.";
100 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
104 map<int, const_admin_iter>::iterator si;
105 si = searchDescriptors.begin();
106 while (si != searchDescriptors.end())
108 if (si->second == ai)
114 if (store->DelAdmin(login) < 0)
116 strError = "Administrator \'" + login + "\' was not deleted. Error: " + store->GetStrError();
117 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
122 WriteServLog("%s Administrator \'%s\' deleted.", admin->GetLogStr().c_str(), login.c_str());
125 //-----------------------------------------------------------------------------
126 int SERVICES_IMPL::Change(const ADMIN_CONF & ac, const ADMIN * admin)
128 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
129 const PRIV * priv = admin->GetPriv();
133 string s = admin->GetLogStr() + " Change administrator \'" + ac.login + "\'. Access denied.";
134 strError = "Access denied.";
135 WriteServLog(s.c_str());
139 ADMIN_IMPL adm(0, ac.login, "");
140 admin_iter ai(find(data.begin(), data.end(), adm));
142 if (ai == data.end())
144 strError = "Administrator \'" + ac.login + "\' cannot be changed " + ". Administrator does not exist.";
145 WriteServLog("%s %s", admin->GetLogStr().c_str(), strError.c_str());
150 if (store->SaveAdmin(ac))
152 WriteServLog("Cannot write admin %s.", ac.login.c_str());
153 WriteServLog("%s", store->GetStrError().c_str());
157 WriteServLog("%s Administrator \'%s\' changed.",
158 admin->GetLogStr().c_str(), ac.login.c_str());
162 //-----------------------------------------------------------------------------
163 int SERVICES_IMPL::ReadAdmins()
165 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
166 vector<string> adminsList;
167 if (store->GetAdminsList(&adminsList) < 0)
169 WriteServLog(store->GetStrError().c_str());
173 for (unsigned int i = 0; i < adminsList.size(); i++)
175 ADMIN_CONF ac(0, adminsList[i], "");
177 if (store->RestoreAdmin(&ac, adminsList[i]))
179 WriteServLog(store->GetStrError().c_str());
183 data.push_back(ADMIN_IMPL(ac));
187 //-----------------------------------------------------------------------------
188 void SERVICES_IMPL::PrintAdmins() const
190 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
191 const_admin_iter ai(data.begin());
192 while (ai != data.end())
198 //-----------------------------------------------------------------------------
199 bool SERVICES_IMPL::FindAdmin(const string & l, ADMIN ** admin)
201 assert(admin != NULL && "Pointer to admin is not null");
203 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
206 printfd(__FILE__, "no admin in system!\n");
211 ADMIN_IMPL adm(0, l, "");
212 admin_iter ai(find(data.begin(), data.end(), adm));
214 if (ai != data.end())
222 //-----------------------------------------------------------------------------
223 bool SERVICES_IMPL::AdminExists(const string & login) const
225 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
228 printfd(__FILE__, "no admin in system!\n");
232 ADMIN_IMPL adm(0, login, "");
233 const_admin_iter ai(find(data.begin(), data.end(), adm));
235 if (ai != data.end())
240 //-----------------------------------------------------------------------------
241 bool SERVICES_IMPL::AdminCorrect(const string & login, const std::string & password, ADMIN ** admin)
243 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
246 printfd(__FILE__, "no admin in system!\n");
250 ADMIN_IMPL adm(0, login, "");
251 admin_iter ai(find(data.begin(), data.end(), adm));
253 if (ai == data.end())
258 if (ai->GetPassword() != password)
267 //-----------------------------------------------------------------------------
268 int SERVICES_IMPL::OpenSearch() const
270 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
272 searchDescriptors[handle] = data.begin();
275 //-----------------------------------------------------------------------------
276 int SERVICES_IMPL::SearchNext(int h, ADMIN_CONF * ac) const
278 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
279 if (searchDescriptors.find(h) == searchDescriptors.end())
281 WriteServLog("SERVICES. Incorrect search handle.");
285 if (searchDescriptors[h] == data.end())
288 ADMIN_IMPL a = *searchDescriptors[h]++;
294 //-----------------------------------------------------------------------------
295 int SERVICES_IMPL::CloseSearch(int h) const
297 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
298 if (searchDescriptors.find(h) != searchDescriptors.end())
300 searchDescriptors.erase(searchDescriptors.find(h));
304 WriteServLog("SERVICES. Incorrect search handle.");
307 //-----------------------------------------------------------------------------