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>
32 #include "stg/store.h"
33 #include "stg/users.h"
35 #include "stg/tariffs.h"
36 #include "stg/logger.h"
37 #include "stg/notifer.h"
38 #include "stg/noncopyable.h"
40 #include "eventloop.h"
41 #include "settings_impl.h"
42 #include "user_impl.h"
47 const int userDeleteDelayTime = 120;
49 typedef std::list<UserImpl>::iterator user_iter;
50 typedef std::list<UserImpl>::const_iterator const_user_iter;
53 //-----------------------------------------------------------------------------
60 std::list<UserImpl>::iterator iter;
63 //-----------------------------------------------------------------------------
64 class UsersImpl : public Users {
65 friend class PROPERTY_NOTIFER_IP_BEFORE;
66 friend class PROPERTY_NOTIFER_IP_AFTER;
69 using UserImplPtr = UserImpl*;
71 UsersImpl(SettingsImpl * s, Store * store,
72 Tariffs * tariffs, Services & svcs,
73 const Admin * sysAdmin);
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;
80 bool TariffInUse(const std::string & tariffName) const override;
82 void AddNotifierUserAdd(NotifierBase<UserPtr> *) override;
83 void DelNotifierUserAdd(NotifierBase<UserPtr> *) override;
85 void AddNotifierUserDel(NotifierBase<UserPtr> *) override;
86 void DelNotifierUserDel(NotifierBase<UserPtr> *) override;
88 void AddNotifierUserAdd(NotifierBase<UserImplPtr> *);
89 void DelNotifierUserAdd(NotifierBase<UserImplPtr> *);
91 void AddNotifierUserDel(NotifierBase<UserImplPtr> *);
92 void DelNotifierUserDel(NotifierBase<UserImplPtr> *);
94 int Add(const std::string & login, const Admin * admin) override;
95 void Del(const std::string & login, const Admin * admin) override;
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,
101 const std::string & reason) override;
103 int ReadUsers() override;
104 size_t Count() const override { return users.size(); }
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;
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;
116 int Start() override;
120 UsersImpl(const UsersImpl & rvalue);
121 UsersImpl & operator=(const UsersImpl & rvalue);
123 void AddToIPIdx(user_iter user);
124 void DelFromIPIdx(uint32_t ip);
125 bool FindByIPIdx(uint32_t ip, user_iter & iter) const;
127 int FindByNameNonLock(const std::string & login, user_iter * user);
128 int FindByNameNonLock(const std::string & login, const_user_iter * user) const;
131 void ProcessActions();
133 void AddUserIntoIndexes(user_iter user);
134 void DelUserFromIndexes(user_iter user);
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);
141 bool TimeToWriteDetailStat(const struct tm & t);
143 std::list<UserImpl> users;
144 std::list<USER_TO_DEL> usersToDelete;
146 std::map<uint32_t, user_iter> ipIndex;
147 std::map<std::string, user_iter> loginIndex;
149 SettingsImpl * settings;
151 Services & m_services;
153 const Admin * sysAdmin;
154 Logger & WriteServLog;
159 mutable pthread_mutex_t mutex;
161 mutable unsigned int handle;
163 mutable std::map<int, user_iter> searchDescriptors;
165 std::set<NotifierBase<UserPtr>*> onAddNotifiers;
166 std::set<NotifierBase<UserPtr>*> onDelNotifiers;
167 std::set<NotifierBase<UserImplPtr>*> onAddNotifiersImpl;
168 std::set<NotifierBase<UserImplPtr>*> onDelNotifiersImpl;
170 //-----------------------------------------------------------------------------
172 void PROPERTY_NOTIFER_IP_BEFORE::Notify(const uint32_t & oldValue,
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);
182 //-----------------------------------------------------------------------------
184 void PROPERTY_NOTIFER_IP_AFTER::Notify(const uint32_t &,
185 const uint32_t & newValue)
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);
194 //-----------------------------------------------------------------------------