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>
25 #include "admins_impl.h"
27 #include "stg/common.h"
29 using STG::AdminsImpl;
31 //-----------------------------------------------------------------------------
32 AdminsImpl::AdminsImpl(Store& st)
33 : m_stg(Priv(0xFFFF), "@stargazer", ""),
34 m_noAdmin(Priv(0xFFFF), "NO-ADMIN", ""),
36 WriteServLog(Logger::get())
40 //-----------------------------------------------------------------------------
41 int AdminsImpl::add(const std::string& login, const Admin& admin)
43 if (!admin.priv().adminChg)
45 const std::string s = admin.logStr() + " Add administrator \'" + login + "\'. Access denied.";
46 m_strError = "Access denied.";
47 WriteServLog(s.c_str());
51 std::lock_guard<std::mutex> lock(m_mutex);
52 const auto it = find(login);
54 if (it != m_data.end())
56 m_strError = "Administrator \'" + login + "\' cannot not be added. Administrator already exists.";
57 WriteServLog("%s %s", admin.logStr().c_str(), m_strError.c_str());
61 m_data.push_back(Admin(Priv(0), login, {}));
63 if (m_store.AddAdmin(login) == 0)
65 WriteServLog("%s Administrator \'%s\' added.",
66 admin.logStr().c_str(), login.c_str());
70 m_strError = "Administrator \'" + login + "\' was not added. Error: " + m_store.GetStrError();
71 WriteServLog("%s %s", admin.logStr().c_str(), m_strError.c_str());
75 //-----------------------------------------------------------------------------
76 int AdminsImpl::del(const std::string& login, const Admin& admin)
78 if (!admin.priv().adminChg)
80 const std::string s = admin.logStr() + " Delete administrator \'" + login + "\'. Access denied.";
81 m_strError = "Access denied.";
82 WriteServLog(s.c_str());
86 std::lock_guard<std::mutex> lock(m_mutex);
87 const auto it = find(login);
89 if (it == m_data.end())
91 m_strError = "Administrator \'" + login + "\' cannot be deleted. Administrator does not exist.";
92 WriteServLog("%s %s", admin.logStr().c_str(), m_strError.c_str());
97 if (m_store.DelAdmin(login) < 0)
99 m_strError = "Administrator \'" + login + "\' was not deleted. Error: " + m_store.GetStrError();
100 WriteServLog("%s %s", admin.logStr().c_str(), m_strError.c_str());
105 WriteServLog("%s Administrator \'%s\' deleted.", admin.logStr().c_str(), login.c_str());
108 //-----------------------------------------------------------------------------
109 int AdminsImpl::change(const AdminConf& ac, const Admin& admin)
111 if (!admin.priv().adminChg)
113 const std::string s = admin.logStr() + " Change administrator \'" + ac.login + "\'. Access denied.";
114 m_strError = "Access denied.";
115 WriteServLog(s.c_str());
119 std::lock_guard<std::mutex> lock(m_mutex);
120 const auto it = find(ac.login);
122 if (it == m_data.end())
124 m_strError = "Administrator \'" + ac.login + "\' cannot be changed " + ". Administrator does not exist.";
125 WriteServLog("%s %s", admin.logStr().c_str(), m_strError.c_str());
130 if (m_store.SaveAdmin(ac))
132 WriteServLog("Cannot write admin %s.", ac.login.c_str());
133 WriteServLog("%s", m_store.GetStrError().c_str());
137 WriteServLog("%s Administrator \'%s\' changed.",
138 admin.logStr().c_str(), ac.login.c_str());
142 //-----------------------------------------------------------------------------
143 void AdminsImpl::read()
145 std::vector<std::string> logins;
146 if (m_store.GetAdminsList(&logins) < 0)
148 WriteServLog(m_store.GetStrError().c_str());
152 std::vector<Admin> admins;
153 for (const auto& login : logins)
155 AdminConf ac(Priv(0), login, "");
157 if (m_store.RestoreAdmin(&ac, login))
159 WriteServLog(m_store.GetStrError().c_str());
163 m_data.push_back(Admin(ac));
166 std::lock_guard<std::mutex> lock(m_mutex);
169 //-----------------------------------------------------------------------------
170 bool AdminsImpl::find(const std::string& login, Admin** admin)
172 std::lock_guard<std::mutex> lock(m_mutex);
175 printfd(__FILE__, "No admin in system!\n");
176 if (admin != nullptr)
181 auto it = find(login);
183 if (it != m_data.end())
185 if (admin != nullptr)
192 //-----------------------------------------------------------------------------
193 bool AdminsImpl::exists(const std::string& login) const
195 std::lock_guard<std::mutex> lock(m_mutex);
198 printfd(__FILE__, "No admin in system!\n");
202 return find(login) != m_data.end();
204 //-----------------------------------------------------------------------------
205 bool AdminsImpl::correct(const std::string& login, const std::string& password, Admin** admin)
207 std::lock_guard<std::mutex> lock(m_mutex);
210 printfd(__FILE__, "No admin in system!\n");
214 const auto it = find(login);
216 if (it == m_data.end() || it->password() != password)
219 if (admin != nullptr)