]> git.stg.codes - stg.git/blob - projects/stargazer/users_impl.h
Fight CLang warnings.
[stg.git] / projects / stargazer / users_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  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 #pragma once
22
23 #include "settings_impl.h"
24 #include "user_impl.h"
25 #include "stg/store.h"
26 #include "stg/users.h"
27 #include "stg/user.h"
28 #include "stg/tariffs.h"
29 #include "stg/logger.h"
30 #include "stg/noncopyable.h"
31
32 #include <string>
33 #include <map>
34 #include <list>
35 #include <set>
36 #include <mutex>
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wshadow"
39 #include <jthread.hpp>
40 #pragma GCC diagnostic pop
41 #include <ctime>
42 #include <cstdint>
43
44 namespace STG
45 {
46
47 const int userDeleteDelayTime = 120;
48
49 typedef std::list<UserImpl>::iterator user_iter;
50 typedef std::list<UserImpl>::const_iterator const_user_iter;
51
52 //-----------------------------------------------------------------------------
53 struct USER_TO_DEL {
54 USER_TO_DEL()
55     : iter(),
56       delTime(0)
57 {}
58
59 std::list<UserImpl>::iterator iter;
60 time_t  delTime;
61 };
62 //-----------------------------------------------------------------------------
63 class UsersImpl : public Users
64 {
65     public:
66         using UserImplPtr = UserImpl*;
67
68         UsersImpl(SettingsImpl * s, Store * store,
69                   Tariffs * tariffs, Services & svcs,
70                   const Admin& sysAdmin);
71
72         int             FindByName(const std::string & login, UserPtr * user) override;
73         int             FindByName(const std::string & login, ConstUserPtr * user) const override;
74         bool            Exists(const std::string & login) const override;
75
76         bool            TariffInUse(const std::string & tariffName) const override;
77
78         template <typename F>
79         auto            onImplAdd(F&& f) { return m_onAddImplCallbacks.add(std::forward<F>(f)); }
80         template <typename F>
81         auto            onImplDel(F&& f) { return m_onDelImplCallbacks.add(std::forward<F>(f)); }
82
83         int             Add(const std::string & login, const Admin * admin) override;
84         void            Del(const std::string & login, const Admin * admin) override;
85
86         bool            Authorize(const std::string & login, uint32_t ip,
87                                   uint32_t enabledDirs, const Auth * auth) override;
88         bool            Unauthorize(const std::string & login,
89                                     const Auth * auth,
90                                     const std::string & reason) override;
91
92         int             ReadUsers() override;
93         size_t          Count() const override { return users.size(); }
94
95         int             FindByIPIdx(uint32_t ip, UserPtr * user) const override;
96         int             FindByIPIdx(uint32_t ip, UserImpl ** user) const;
97         bool            IsIPInIndex(uint32_t ip) const override;
98         bool            IsIPInUse(uint32_t ip, const std::string & login, ConstUserPtr * user) const override;
99
100         unsigned int    OpenSearch() override;
101         int             SearchNext(int handler, UserPtr * user) override;
102         int             SearchNext(int handler, UserImpl ** user);
103         int             CloseSearch(int handler) override;
104
105         int             Start() override;
106         int             Stop() override;
107
108     private:
109         UsersImpl(const UsersImpl & rvalue);
110         UsersImpl & operator=(const UsersImpl & rvalue);
111
112         void            AddToIPIdx(user_iter user);
113         void            DelFromIPIdx(uint32_t ip);
114         bool            FindByIPIdx(uint32_t ip, user_iter & iter) const;
115
116         bool            FindByNameNonLock(const std::string & login, user_iter * user);
117         bool            FindByNameNonLock(const std::string & login, const_user_iter * user) const;
118
119         void            RealDelUser();
120         void            ProcessActions();
121
122         void            AddUserIntoIndexes(user_iter user);
123         void            DelUserFromIndexes(user_iter user);
124
125         void            Run(std::stop_token token);
126         void            NewMinute(const struct tm & t);
127         void            NewDay(const struct tm & t);
128         void            DayResetTraff(const struct tm & t);
129
130         bool            TimeToWriteDetailStat(const struct tm & t);
131
132         std::list<UserImpl>                  users;
133         std::list<USER_TO_DEL>                usersToDelete;
134
135         std::map<uint32_t, user_iter>         ipIndex;
136         std::map<std::string, user_iter>      loginIndex;
137
138         SettingsImpl *     settings;
139         Tariffs *           m_tariffs;
140         Services &          m_services;
141         Store *             m_store;
142         const Admin&        m_sysAdmin;
143         Logger &        WriteServLog;
144
145         bool                isRunning;
146
147         mutable std::mutex      m_mutex;
148         std::jthread            m_thread;
149         mutable unsigned int    handle;
150
151         mutable std::map<unsigned int, user_iter>  searchDescriptors;
152
153         Subscriptions<UserImplPtr> m_onAddImplCallbacks;
154         Subscriptions<UserImplPtr> m_onDelImplCallbacks;
155 };
156
157 }