]> git.stg.codes - stg.git/blob - projects/stargazer/users_impl.h
Complete replacement notifiers with subscriptions.
[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 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 {
66     friend class PROPERTY_NOTIFER_IP_BEFORE;
67     friend class PROPERTY_NOTIFER_IP_AFTER;
68
69     public:
70         using UserImplPtr = UserImpl*;
71
72         UsersImpl(SettingsImpl * s, Store * store,
73                   Tariffs * tariffs, Services & svcs,
74                   const Admin& sysAdmin);
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         template <typename F>
83         auto            onImplAdd(F&& f) { return m_onAddImplCallbacks.add(std::forward<F>(f)); }
84         template <typename F>
85         auto            onImplDel(F&& f) { return m_onDelImplCallbacks.add(std::forward<F>(f)); }
86
87         int             Add(const std::string & login, const Admin * admin) override;
88         void            Del(const std::string & login, const Admin * admin) override;
89
90         bool            Authorize(const std::string & login, uint32_t ip,
91                                   uint32_t enabledDirs, const Auth * auth) override;
92         bool            Unauthorize(const std::string & login,
93                                     const Auth * auth,
94                                     const std::string & reason) override;
95
96         int             ReadUsers() override;
97         size_t          Count() const override { return users.size(); }
98
99         int             FindByIPIdx(uint32_t ip, UserPtr * user) const override;
100         int             FindByIPIdx(uint32_t ip, UserImpl ** user) const;
101         bool            IsIPInIndex(uint32_t ip) const override;
102         bool            IsIPInUse(uint32_t ip, const std::string & login, ConstUserPtr * user) const override;
103
104         unsigned int    OpenSearch() override;
105         int             SearchNext(int handler, UserPtr * user) override;
106         int             SearchNext(int handler, UserImpl ** user);
107         int             CloseSearch(int handler) override;
108
109         int             Start() override;
110         int             Stop() override;
111
112     private:
113         UsersImpl(const UsersImpl & rvalue);
114         UsersImpl & operator=(const UsersImpl & rvalue);
115
116         void            AddToIPIdx(user_iter user);
117         void            DelFromIPIdx(uint32_t ip);
118         bool            FindByIPIdx(uint32_t ip, user_iter & iter) const;
119
120         bool            FindByNameNonLock(const std::string & login, user_iter * user);
121         bool            FindByNameNonLock(const std::string & login, const_user_iter * user) const;
122
123         void            RealDelUser();
124         void            ProcessActions();
125
126         void            AddUserIntoIndexes(user_iter user);
127         void            DelUserFromIndexes(user_iter user);
128
129         void            Run(std::stop_token token);
130         void            NewMinute(const struct tm & t);
131         void            NewDay(const struct tm & t);
132         void            DayResetTraff(const struct tm & t);
133
134         bool            TimeToWriteDetailStat(const struct tm & t);
135
136         std::list<UserImpl>                  users;
137         std::list<USER_TO_DEL>                usersToDelete;
138
139         std::map<uint32_t, user_iter>         ipIndex;
140         std::map<std::string, user_iter>      loginIndex;
141
142         SettingsImpl *     settings;
143         Tariffs *           m_tariffs;
144         Services &          m_services;
145         Store *             m_store;
146         const Admin&        m_sysAdmin;
147         Logger &        WriteServLog;
148
149         bool                isRunning;
150
151         mutable std::mutex      m_mutex;
152         std::jthread            m_thread;
153         mutable unsigned int    handle;
154
155         mutable std::map<unsigned int, user_iter>  searchDescriptors;
156
157         Subscriptions<UserImplPtr> m_onAddImplCallbacks;
158         Subscriptions<UserImplPtr> m_onDelImplCallbacks;
159 };
160
161 }