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.
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.
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
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
30 #pragma GCC diagnostic push
31 #pragma GCC diagnostic ignored "-Wshadow"
32 #include <jthread.hpp>
33 #pragma GCC diagnostic pop
37 #include "stg/store.h"
38 #include "stg/users.h"
40 #include "stg/tariffs.h"
41 #include "stg/logger.h"
42 #include "stg/notifer.h"
43 #include "stg/noncopyable.h"
45 #include "eventloop.h"
46 #include "settings_impl.h"
47 #include "user_impl.h"
52 const int userDeleteDelayTime = 120;
54 typedef std::list<UserImpl>::iterator user_iter;
55 typedef std::list<UserImpl>::const_iterator const_user_iter;
58 //-----------------------------------------------------------------------------
65 std::list<UserImpl>::iterator iter;
68 //-----------------------------------------------------------------------------
69 class UsersImpl : public Users {
70 friend class PROPERTY_NOTIFER_IP_BEFORE;
71 friend class PROPERTY_NOTIFER_IP_AFTER;
74 using UserImplPtr = UserImpl*;
76 UsersImpl(SettingsImpl * s, Store * store,
77 Tariffs * tariffs, Services & svcs,
78 const Admin& sysAdmin);
81 int FindByName(const std::string & login, UserPtr * user) override;
82 int FindByName(const std::string & login, ConstUserPtr * user) const override;
83 bool Exists(const std::string & login) const override;
85 bool TariffInUse(const std::string & tariffName) const override;
87 void AddNotifierUserAdd(NotifierBase<UserPtr> *) override;
88 void DelNotifierUserAdd(NotifierBase<UserPtr> *) override;
90 void AddNotifierUserDel(NotifierBase<UserPtr> *) override;
91 void DelNotifierUserDel(NotifierBase<UserPtr> *) override;
93 void AddNotifierUserAdd(NotifierBase<UserImplPtr> *);
94 void DelNotifierUserAdd(NotifierBase<UserImplPtr> *);
96 void AddNotifierUserDel(NotifierBase<UserImplPtr> *);
97 void DelNotifierUserDel(NotifierBase<UserImplPtr> *);
99 int Add(const std::string & login, const Admin * admin) override;
100 void Del(const std::string & login, const Admin * admin) override;
102 bool Authorize(const std::string & login, uint32_t ip,
103 uint32_t enabledDirs, const Auth * auth) override;
104 bool Unauthorize(const std::string & login,
106 const std::string & reason) override;
108 int ReadUsers() override;
109 size_t Count() const override { return users.size(); }
111 int FindByIPIdx(uint32_t ip, UserPtr * user) const override;
112 int FindByIPIdx(uint32_t ip, UserImpl ** user) const;
113 bool IsIPInIndex(uint32_t ip) const override;
114 bool IsIPInUse(uint32_t ip, const std::string & login, ConstUserPtr * user) const override;
116 int OpenSearch() override;
117 int SearchNext(int handler, UserPtr * user) override;
118 int SearchNext(int handler, UserImpl ** user);
119 int CloseSearch(int handler) override;
121 int Start() override;
125 UsersImpl(const UsersImpl & rvalue);
126 UsersImpl & operator=(const UsersImpl & rvalue);
128 void AddToIPIdx(user_iter user);
129 void DelFromIPIdx(uint32_t ip);
130 bool FindByIPIdx(uint32_t ip, user_iter & iter) const;
132 int FindByNameNonLock(const std::string & login, user_iter * user);
133 int FindByNameNonLock(const std::string & login, const_user_iter * user) const;
136 void ProcessActions();
138 void AddUserIntoIndexes(user_iter user);
139 void DelUserFromIndexes(user_iter user);
141 void Run(std::stop_token token);
142 void NewMinute(const struct tm & t);
143 void NewDay(const struct tm & t);
144 void DayResetTraff(const struct tm & t);
146 bool TimeToWriteDetailStat(const struct tm & t);
148 std::list<UserImpl> users;
149 std::list<USER_TO_DEL> usersToDelete;
151 std::map<uint32_t, user_iter> ipIndex;
152 std::map<std::string, user_iter> loginIndex;
154 SettingsImpl * settings;
156 Services & m_services;
158 const Admin& sysAdmin;
159 Logger & WriteServLog;
163 mutable std::mutex m_mutex;
164 std::jthread m_thread;
165 mutable unsigned int handle;
167 mutable std::map<int, user_iter> searchDescriptors;
169 std::set<NotifierBase<UserPtr>*> onAddNotifiers;
170 std::set<NotifierBase<UserPtr>*> onDelNotifiers;
171 std::set<NotifierBase<UserImplPtr>*> onAddNotifiersImpl;
172 std::set<NotifierBase<UserImplPtr>*> onDelNotifiersImpl;