]> git.stg.codes - stg.git/blob - stargazer/users_impl.h
d8414375c7bfce36517d0f195e617fc38e07502b
[stg.git] / 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 <ctime>
30 #include <cstdint>
31
32 #include "stg/store.h"
33 #include "stg/users.h"
34 #include "stg/user.h"
35 #include "stg/tariffs.h"
36 #include "stg/logger.h"
37 #include "stg/notifer.h"
38 #include "stg/noncopyable.h"
39 #include "actions.h"
40 #include "eventloop.h"
41 #include "settings_impl.h"
42 #include "user_impl.h"
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 class UsersImpl;
53 //-----------------------------------------------------------------------------
54 struct USER_TO_DEL {
55 USER_TO_DEL()
56     : iter(),
57       delTime(0)
58 {}
59
60 std::list<UserImpl>::iterator iter;
61 time_t  delTime;
62 };
63 //-----------------------------------------------------------------------------
64 class UsersImpl : public Users {
65     friend class PROPERTY_NOTIFER_IP_BEFORE;
66     friend class PROPERTY_NOTIFER_IP_AFTER;
67
68 public:
69     using UserImplPtr = UserImpl*;
70
71     UsersImpl(SettingsImpl * s, Store * store,
72               Tariffs * tariffs, Services & svcs,
73               const Admin * sysAdmin);
74     virtual ~UsersImpl();
75
76     int             FindByName(const std::string & login, UserPtr * user) override;
77     int             FindByName(const std::string & login, ConstUserPtr * user) const override;
78     bool            Exists(const std::string & login) const override;
79
80     bool            TariffInUse(const std::string & tariffName) const override;
81
82     void            AddNotifierUserAdd(NotifierBase<UserPtr> *) override;
83     void            DelNotifierUserAdd(NotifierBase<UserPtr> *) override;
84
85     void            AddNotifierUserDel(NotifierBase<UserPtr> *) override;
86     void            DelNotifierUserDel(NotifierBase<UserPtr> *) override;
87
88     void            AddNotifierUserAdd(NotifierBase<UserImplPtr> *);
89     void            DelNotifierUserAdd(NotifierBase<UserImplPtr> *);
90
91     void            AddNotifierUserDel(NotifierBase<UserImplPtr> *);
92     void            DelNotifierUserDel(NotifierBase<UserImplPtr> *);
93
94     int             Add(const std::string & login, const Admin * admin) override;
95     void            Del(const std::string & login, const Admin * admin) override;
96
97     bool            Authorize(const std::string & login, uint32_t ip,
98                               uint32_t enabledDirs, const Auth * auth) override;
99     bool            Unauthorize(const std::string & login,
100                                 const Auth * auth,
101                                 const std::string & reason) override;
102
103     int             ReadUsers() override;
104     size_t          Count() const override { return users.size(); }
105
106     int             FindByIPIdx(uint32_t ip, UserPtr * user) const override;
107     int             FindByIPIdx(uint32_t ip, UserImpl ** user) const;
108     bool            IsIPInIndex(uint32_t ip) const override;
109     bool            IsIPInUse(uint32_t ip, const std::string & login, ConstUserPtr * user) const override;
110
111     int             OpenSearch() override;
112     int             SearchNext(int handler, UserPtr * user) override;
113     int             SearchNext(int handler, UserImpl ** user);
114     int             CloseSearch(int handler) override;
115
116     int             Start() override;
117     int             Stop() override;
118
119 private:
120     UsersImpl(const UsersImpl & rvalue);
121     UsersImpl & operator=(const UsersImpl & rvalue);
122
123     void            AddToIPIdx(user_iter user);
124     void            DelFromIPIdx(uint32_t ip);
125     bool            FindByIPIdx(uint32_t ip, user_iter & iter) const;
126
127     int             FindByNameNonLock(const std::string & login, user_iter * user);
128     int             FindByNameNonLock(const std::string & login, const_user_iter * user) const;
129
130     void            RealDelUser();
131     void            ProcessActions();
132
133     void            AddUserIntoIndexes(user_iter user);
134     void            DelUserFromIndexes(user_iter user);
135
136     static void *   Run(void *);
137     void            NewMinute(const struct tm & t);
138     void            NewDay(const struct tm & t);
139     void            DayResetTraff(const struct tm & t);
140
141     bool            TimeToWriteDetailStat(const struct tm & t);
142
143     std::list<UserImpl>                  users;
144     std::list<USER_TO_DEL>                usersToDelete;
145
146     std::map<uint32_t, user_iter>         ipIndex;
147     std::map<std::string, user_iter>      loginIndex;
148
149     SettingsImpl *     settings;
150     Tariffs *           tariffs;
151     Services &          m_services;
152     Store *             store;
153     const Admin *       sysAdmin;
154     Logger &        WriteServLog;
155
156     bool                nonstop;
157     bool                isRunning;
158
159     mutable pthread_mutex_t mutex;
160     pthread_t               thread;
161     mutable unsigned int    handle;
162
163     mutable std::map<int, user_iter>  searchDescriptors;
164
165     std::set<NotifierBase<UserPtr>*> onAddNotifiers;
166     std::set<NotifierBase<UserPtr>*> onDelNotifiers;
167     std::set<NotifierBase<UserImplPtr>*> onAddNotifiersImpl;
168     std::set<NotifierBase<UserImplPtr>*> onDelNotifiersImpl;
169 };
170 //-----------------------------------------------------------------------------
171 /*inline
172 void PROPERTY_NOTIFER_IP_BEFORE::Notify(const uint32_t & oldValue,
173                                         const uint32_t &)
174 {
175 if (!oldValue)
176     return;
177
178 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(users, &Users::DelFromIPIdx, oldValue);
179 // Using explicit call to assure that index is valid, because fast reconnect with delayed call can result in authorization error
180 users.DelFromIPIdx(oldValue);
181 }
182 //-----------------------------------------------------------------------------
183 inline
184 void PROPERTY_NOTIFER_IP_AFTER::Notify(const uint32_t &,
185                                        const uint32_t & newValue)
186 {
187 if (!newValue)
188     return;
189
190 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(users, &Users::AddToIPIdx, user);
191 // Using explicit call to assure that index is valid, because fast reconnect with delayed call can result in authorization error
192 users.AddToIPIdx(user);
193 }*/
194 //-----------------------------------------------------------------------------
195 }