]> git.stg.codes - stg.git/blob - stargazer/admins_impl.cpp
Non-virtual admin.
[stg.git] / stargazer / admins_impl.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Date: 27.10.2002
19  */
20
21 /*
22  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23  */
24
25 #include "admins_impl.h"
26
27 #include "stg/common.h"
28
29 using STG::AdminsImpl;
30
31 //-----------------------------------------------------------------------------
32 AdminsImpl::AdminsImpl(Store& st)
33     : m_stg(Priv(0xFFFF), "@stargazer", ""),
34       m_noAdmin(Priv(0xFFFF), "NO-ADMIN", ""),
35       m_store(st),
36       WriteServLog(Logger::get())
37 {
38     read();
39 }
40 //-----------------------------------------------------------------------------
41 int AdminsImpl::add(const std::string& login, const Admin& admin)
42 {
43     if (!admin.priv().adminChg)
44     {
45         const std::string s = admin.logStr() + " Add administrator \'" + login + "\'. Access denied.";
46         m_strError = "Access denied.";
47         WriteServLog(s.c_str());
48         return -1;
49     }
50
51     std::lock_guard<std::mutex> lock(m_mutex);
52     const auto it = find(login);
53
54     if (it != m_data.end())
55     {
56         m_strError = "Administrator \'" + login + "\' cannot not be added. Administrator already exists.";
57         WriteServLog("%s %s", admin.logStr().c_str(), m_strError.c_str());
58         return -1;
59     }
60
61     m_data.push_back(Admin(Priv(0), login, {}));
62
63     if (m_store.AddAdmin(login) == 0)
64     {
65         WriteServLog("%s Administrator \'%s\' added.",
66                      admin.logStr().c_str(), login.c_str());
67         return 0;
68     }
69
70     m_strError = "Administrator \'" + login + "\' was not added. Error: " + m_store.GetStrError();
71     WriteServLog("%s %s", admin.logStr().c_str(), m_strError.c_str());
72
73     return -1;
74 }
75 //-----------------------------------------------------------------------------
76 int AdminsImpl::del(const std::string& login, const Admin& admin)
77 {
78     if (!admin.priv().adminChg)
79     {
80         const std::string s = admin.logStr() + " Delete administrator \'" + login + "\'. Access denied.";
81         m_strError = "Access denied.";
82         WriteServLog(s.c_str());
83         return -1;
84     }
85
86     std::lock_guard<std::mutex> lock(m_mutex);
87     const auto it = find(login);
88
89     if (it == m_data.end())
90     {
91         m_strError = "Administrator \'" + login + "\' cannot be deleted. Administrator does not exist.";
92         WriteServLog("%s %s", admin.logStr().c_str(), m_strError.c_str());
93         return -1;
94     }
95
96     m_data.erase(it);
97     if (m_store.DelAdmin(login) < 0)
98     {
99         m_strError = "Administrator \'" + login + "\' was not deleted. Error: " + m_store.GetStrError();
100         WriteServLog("%s %s", admin.logStr().c_str(), m_strError.c_str());
101
102         return -1;
103     }
104
105     WriteServLog("%s Administrator \'%s\' deleted.", admin.logStr().c_str(), login.c_str());
106     return 0;
107 }
108 //-----------------------------------------------------------------------------
109 int AdminsImpl::change(const AdminConf& ac, const Admin& admin)
110 {
111     if (!admin.priv().adminChg)
112     {
113         const std::string s = admin.logStr() + " Change administrator \'" + ac.login + "\'. Access denied.";
114         m_strError = "Access denied.";
115         WriteServLog(s.c_str());
116         return -1;
117     }
118
119     std::lock_guard<std::mutex> lock(m_mutex);
120     const auto it = find(ac.login);
121
122     if (it == m_data.end())
123     {
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());
126         return -1;
127     }
128
129     *it = ac;
130     if (m_store.SaveAdmin(ac))
131     {
132         WriteServLog("Cannot write admin %s.", ac.login.c_str());
133         WriteServLog("%s", m_store.GetStrError().c_str());
134         return -1;
135     }
136
137     WriteServLog("%s Administrator \'%s\' changed.",
138                  admin.logStr().c_str(), ac.login.c_str());
139
140     return 0;
141 }
142 //-----------------------------------------------------------------------------
143 void AdminsImpl::read()
144 {
145     std::vector<std::string> logins;
146     if (m_store.GetAdminsList(&logins) < 0)
147     {
148         WriteServLog(m_store.GetStrError().c_str());
149         return;
150     }
151
152     std::vector<Admin> admins;
153     for (const auto& login : logins)
154     {
155         AdminConf ac(Priv(0), login, "");
156
157         if (m_store.RestoreAdmin(&ac, login))
158         {
159             WriteServLog(m_store.GetStrError().c_str());
160             return;
161         }
162
163         m_data.push_back(Admin(ac));
164     }
165
166     std::lock_guard<std::mutex> lock(m_mutex);
167     m_data.swap(admins);
168 }
169 //-----------------------------------------------------------------------------
170 bool AdminsImpl::find(const std::string& login, Admin** admin)
171 {
172     std::lock_guard<std::mutex> lock(m_mutex);
173     if (m_data.empty())
174     {
175         printfd(__FILE__, "No admin in system!\n");
176         if (admin != nullptr)
177             *admin = &m_noAdmin;
178         return false;
179     }
180
181     auto it = find(login);
182
183     if (it != m_data.end())
184     {
185         if (admin != nullptr)
186             *admin = &(*it);
187         return false;
188     }
189
190     return true;
191 }
192 //-----------------------------------------------------------------------------
193 bool AdminsImpl::exists(const std::string& login) const
194 {
195     std::lock_guard<std::mutex> lock(m_mutex);
196     if (m_data.empty())
197     {
198         printfd(__FILE__, "No admin in system!\n");
199         return true;
200     }
201
202     return find(login) != m_data.end();
203 }
204 //-----------------------------------------------------------------------------
205 bool AdminsImpl::correct(const std::string& login, const std::string& password, Admin** admin)
206 {
207     std::lock_guard<std::mutex> lock(m_mutex);
208     if (m_data.empty())
209     {
210         printfd(__FILE__, "No admin in system!\n");
211         return true;
212     }
213
214     const auto it = find(login);
215
216     if (it == m_data.end() || it->password() != password)
217         return false;
218
219     if (admin != nullptr)
220         *admin = &(*it);
221
222     return true;
223 }