]> git.stg.codes - stg.git/blob - projects/stargazer/admins_impl.h
Move projects back into subfolder.
[stg.git] / projects / stargazer / admins_impl.h
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 #pragma once
26
27 #include "stg/admins.h"
28 #include "stg/admin.h"
29 #include "stg/store.h"
30 #include "stg/logger.h"
31
32 #include <vector>
33 #include <string>
34 #include <algorithm>
35 #include <mutex>
36
37 namespace STG
38 {
39
40 class AdminsImpl : public Admins
41 {
42     public:
43         explicit AdminsImpl(Store& st);
44
45         AdminsImpl(const AdminsImpl&) = delete;
46         AdminsImpl& operator=(const AdminsImpl&) = delete;
47
48         int          add(const std::string& login, const Admin& admin) override;
49         int          del(const std::string& login, const Admin& admin) override;
50         int          change(const AdminConf& ac, const Admin& admin) override;
51         const Admin& sysAdmin() const override { return m_stg; }
52         const Admin& noAdmin() const override { return m_noAdmin; }
53         bool         find(const std::string& login, Admin** admin) override;
54         bool         exists(const std::string& login) const override;
55         bool         correct(const std::string& login,
56                              const std::string& password,
57                              Admin** admin) override;
58
59         const std::string& strError() const override { return m_strError; }
60
61         size_t       count() const override { return m_data.size(); }
62
63         void fmap(std::function<void (const Admin&)> callback) const
64         {
65             for (const auto& admin : m_data)
66                 callback(admin);
67         }
68
69     private:
70         void         read();
71         auto         find(const std::string& login) { return std::find(m_data.begin(), m_data.end(), Admin(Priv(0), login, "")); }
72         auto         find(const std::string& login) const { return std::find(m_data.begin(), m_data.end(), Admin(Priv(0), login, "")); }
73
74         Admin              m_stg;
75         Admin              m_noAdmin;
76         std::vector<Admin> m_data;
77         Store&             m_store;
78         Logger&            WriteServLog;
79         mutable std::mutex m_mutex;
80         std::string        m_strError;
81 };
82
83 }