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