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