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 $
39 #include "stg/os_int.h"
40 #include "stg/store.h"
41 #include "stg/users.h"
43 #include "stg/tariffs.h"
44 #include "stg/logger.h"
45 #include "stg/notifer.h"
46 #include "stg/noncopyable.h"
48 #include "eventloop.h"
49 #include "settings_impl.h"
50 #include "user_impl.h"
52 const int userDeleteDelayTime = 120;
54 typedef std::list<USER_IMPL>::iterator user_iter;
55 typedef std::list<USER_IMPL>::const_iterator const_user_iter;
58 //-----------------------------------------------------------------------------
65 std::list<USER_IMPL>::iterator iter;
68 //-----------------------------------------------------------------------------
69 class USERS_IMPL : private NONCOPYABLE, public USERS {
70 friend class PROPERTY_NOTIFER_IP_BEFORE;
71 friend class PROPERTY_NOTIFER_IP_AFTER;
74 USERS_IMPL(SETTINGS_IMPL * s, STORE * store, TARIFFS * tariffs, const ADMIN * sysAdmin);
75 virtual ~USERS_IMPL();
77 int FindByName(const std::string & login, USER_PTR * user);
79 bool TariffInUse(const std::string & tariffName) const;
81 void AddNotifierUserAdd(NOTIFIER_BASE<USER_PTR> *);
82 void DelNotifierUserAdd(NOTIFIER_BASE<USER_PTR> *);
84 void AddNotifierUserDel(NOTIFIER_BASE<USER_PTR> *);
85 void DelNotifierUserDel(NOTIFIER_BASE<USER_PTR> *);
87 void AddNotifierUserAdd(NOTIFIER_BASE<USER_IMPL_PTR> *);
88 void DelNotifierUserAdd(NOTIFIER_BASE<USER_IMPL_PTR> *);
90 void AddNotifierUserDel(NOTIFIER_BASE<USER_IMPL_PTR> *);
91 void DelNotifierUserDel(NOTIFIER_BASE<USER_IMPL_PTR> *);
93 int Add(const std::string & login, const ADMIN * admin);
94 void Del(const std::string & login, const ADMIN * admin);
96 bool Authorize(const std::string & login, uint32_t ip,
97 uint32_t enabledDirs, const AUTH * auth);
98 bool Unauthorize(const std::string & login, const AUTH * auth);
101 size_t Count() const { return users.size(); }
103 int FindByIPIdx(uint32_t ip, USER_PTR * user) const;
104 int FindByIPIdx(uint32_t ip, USER_IMPL ** user) const;
105 bool IsIPInIndex(uint32_t ip) const;
106 bool IsIPInUse(uint32_t ip, const std::string & login, CONST_USER_PTR * user) const;
109 int SearchNext(int handler, USER_PTR * user);
110 int SearchNext(int handler, USER_IMPL ** user);
111 int CloseSearch(int handler);
117 USERS_IMPL(const USERS_IMPL & rvalue);
118 USERS_IMPL & operator=(const USERS_IMPL & rvalue);
120 void AddToIPIdx(user_iter user);
121 void DelFromIPIdx(uint32_t ip);
122 bool FindByIPIdx(uint32_t ip, user_iter & iter) const;
124 int FindByNameNonLock(const std::string & login, user_iter * user);
127 void ProcessActions();
129 void AddUserIntoIndexes(user_iter user);
130 void DelUserFromIndexes(user_iter user);
132 static void * Run(void *);
133 void NewMinute(const struct tm & t);
134 void NewDay(const struct tm & t);
135 void DayResetTraff(const struct tm & t);
137 bool TimeToWriteDetailStat(const struct tm & t);
139 std::list<USER_IMPL> users;
140 std::list<USER_TO_DEL> usersToDelete;
142 std::map<uint32_t, user_iter> ipIndex;
143 std::map<std::string, user_iter> loginIndex;
145 SETTINGS_IMPL * settings;
148 const ADMIN * sysAdmin;
149 STG_LOGGER & WriteServLog;
154 mutable pthread_mutex_t mutex;
156 mutable unsigned int handle;
158 mutable std::map<int, user_iter> searchDescriptors;
160 std::set<NOTIFIER_BASE<USER_PTR>*> onAddNotifiers;
161 std::set<NOTIFIER_BASE<USER_PTR>*> onDelNotifiers;
162 std::set<NOTIFIER_BASE<USER_IMPL_PTR>*> onAddNotifiersImpl;
163 std::set<NOTIFIER_BASE<USER_IMPL_PTR>*> onDelNotifiersImpl;
165 //-----------------------------------------------------------------------------
167 void PROPERTY_NOTIFER_IP_BEFORE::Notify(const uint32_t & oldValue,
173 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(users, &USERS::DelFromIPIdx, oldValue);
174 // Using explicit call to assure that index is valid, because fast reconnect with delayed call can result in authorization error
175 users.DelFromIPIdx(oldValue);
177 //-----------------------------------------------------------------------------
179 void PROPERTY_NOTIFER_IP_AFTER::Notify(const uint32_t &,
180 const uint32_t & newValue)
185 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(users, &USERS::AddToIPIdx, user);
186 // Using explicit call to assure that index is valid, because fast reconnect with delayed call can result in authorization error
187 users.AddToIPIdx(user);
189 //-----------------------------------------------------------------------------