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>
23 $Date: 2010/10/07 20:04:48 $
45 #include "user_impl.h"
47 #include "stg_logger.h"
50 #include "noncopyable.h"
51 #include "eventloop.h"
53 const int userDeleteDelayTime = 120;
55 typedef std::list<USER_IMPL>::iterator user_iter;
56 typedef std::list<USER_IMPL>::const_iterator const_user_iter;
59 //-----------------------------------------------------------------------------
60 class PROPERTY_NOTIFER_IP_BEFORE: public PROPERTY_NOTIFIER_BASE<uint32_t> {
62 PROPERTY_NOTIFER_IP_BEFORE(USERS_IMPL & us, user_iter u) : users(us), user(u) {}
63 void Notify(const uint32_t & oldValue, const uint32_t & newValue);
64 user_iter GetUser() const { return user; }
69 //-----------------------------------------------------------------------------
70 class PROPERTY_NOTIFER_IP_AFTER: public PROPERTY_NOTIFIER_BASE<uint32_t> {
72 PROPERTY_NOTIFER_IP_AFTER(USERS_IMPL & us, user_iter u) : users(us), user(u) {}
73 void Notify(const uint32_t & oldValue, const uint32_t & newValue);
74 user_iter GetUser() const { return user; }
79 //-----------------------------------------------------------------------------
86 std::list<USER_IMPL>::iterator iter;
89 //-----------------------------------------------------------------------------
90 class USERS_IMPL : private NONCOPYABLE, public USERS {
91 friend class PROPERTY_NOTIFER_IP_BEFORE;
92 friend class PROPERTY_NOTIFER_IP_AFTER;
95 USERS_IMPL(SETTINGS * s, STORE * store, TARIFFS * tariffs, const ADMIN & sysAdmin);
96 virtual ~USERS_IMPL();
98 int FindByName(const std::string & login, USER_PTR * user);
100 bool TariffInUse(const std::string & tariffName) const;
102 void AddNotifierUserAdd(NOTIFIER_BASE<USER_PTR> *);
103 void DelNotifierUserAdd(NOTIFIER_BASE<USER_PTR> *);
105 void AddNotifierUserDel(NOTIFIER_BASE<USER_PTR> *);
106 void DelNotifierUserDel(NOTIFIER_BASE<USER_PTR> *);
108 int Add(const std::string & login, const ADMIN & admin);
109 void Del(const std::string & login, const ADMIN & admin);
112 int GetUserNum() const;
114 int FindByIPIdx(uint32_t ip, USER_PTR * user) const;
115 bool IsIPInIndex(uint32_t ip) const;
118 int SearchNext(int handler, USER_PTR * user);
119 int CloseSearch(int handler);
125 void AddToIPIdx(user_iter user);
126 void DelFromIPIdx(uint32_t ip);
128 int FindByNameNonLock(const std::string & login, user_iter * user);
131 void ProcessActions();
133 void SetUserNotifiers(user_iter user);
134 void UnSetUserNotifiers(user_iter user);
136 void AddUserIntoIndexes(user_iter user);
137 void DelUserFromIndexes(user_iter user);
139 static void * Run(void *);
140 void NewMinute(const struct tm & t);
141 void NewDay(const struct tm & t);
142 void DayResetTraff(const struct tm & t);
144 bool TimeToWriteDetailStat(const struct tm & t);
146 std::list<USER_IMPL> users;
147 std::list<USER_TO_DEL> usersToDelete;
148 std::list<PROPERTY_NOTIFER_IP_BEFORE> userIPNotifiersBefore;
149 std::list<PROPERTY_NOTIFER_IP_AFTER> userIPNotifiersAfter;
151 std::map<uint32_t, user_iter> ipIndex;
152 std::map<std::string, user_iter> loginIndex;
157 const ADMIN & sysAdmin;
158 STG_LOGGER & WriteServLog;
163 mutable pthread_mutex_t mutex;
165 mutable unsigned int handle;
167 mutable std::map<int, user_iter> searchDescriptors;
169 std::set<NOTIFIER_BASE<USER_PTR>*> onAddNotifiers;
170 std::set<NOTIFIER_BASE<USER_PTR>*> onDelNotifiers;
172 //-----------------------------------------------------------------------------
174 void PROPERTY_NOTIFER_IP_BEFORE::Notify(const uint32_t & oldValue,
180 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(users, &USERS::DelFromIPIdx, oldValue);
181 // Using explicit call to assure that index is valid, because fast reconnect with delayed call can result in authorization error
182 users.DelFromIPIdx(oldValue);
184 //-----------------------------------------------------------------------------
186 void PROPERTY_NOTIFER_IP_AFTER::Notify(const uint32_t &,
187 const uint32_t & newValue)
192 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(users, &USERS::AddToIPIdx, user);
193 // Using explicit call to assure that index is valid, because fast reconnect with delayed call can result in authorization error
194 users.AddToIPIdx(user);
196 //-----------------------------------------------------------------------------