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/03/04 12:29:06 $
30 #include <algorithm> // for_each
31 #include <functional> // mem_fun_ref
36 #include "user_property.h"
37 #include "../../../eventloop.h"
59 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 template <typename varType>
67 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
70 bool operator()(varType notifier, USER_PTR user) const
72 return notifier.GetUser() == user;
75 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
80 return aoc.GetPlugin();
82 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 const string AUTH_AO::GetVersion() const
87 return "Always Online authorizator v.1.0";
89 //-----------------------------------------------------------------------------
93 onAddUserNotifier(*this),
94 onDelUserNotifier(*this)
97 //-----------------------------------------------------------------------------
102 users->AddNotifierUserAdd(&onAddUserNotifier);
103 users->AddNotifierUserDel(&onDelUserNotifier);
105 /*list<USER_PTR>::iterator it = usersList.begin();
106 while (it != usersList.end())
108 UpdateUserAuthorization(*it);
111 std::for_each(usersList.begin(), usersList.end(), std::bind1st(std::mem_fun(&AUTH_AO::UpdateUserAuthorization), this));
117 //-----------------------------------------------------------------------------
123 users->DelNotifierUserAdd(&onAddUserNotifier);
124 users->DelNotifierUserDel(&onDelUserNotifier);
126 list<USER_PTR>::iterator users_iter;
127 users_iter = usersList.begin();
128 while (users_iter != usersList.end())
130 Unauthorize(*users_iter);
131 UnSetUserNotifiers(*users_iter);
137 //-----------------------------------------------------------------------------
138 bool AUTH_AO::IsRunning()
142 //-----------------------------------------------------------------------------
143 uint16_t AUTH_AO::GetStartPosition() const
147 //-----------------------------------------------------------------------------
148 uint16_t AUTH_AO::GetStopPosition() const
152 //-----------------------------------------------------------------------------
153 void AUTH_AO::SetUserNotifiers(USER_PTR u)
155 // ---------- AlwaysOnline -------------------
156 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
157 CHG_AFTER_NOTIFIER<int> AfterChgAONotifier(*this, u);
159 /*BeforeChgAONotifier.SetAuthorizator(this);
160 BeforeChgAONotifier.SetUser(u);*/
161 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
163 /*AfterChgAONotifier.SetAuthorizator(this);
164 AfterChgAONotifier.SetUser(u);*/
165 AfterChgAONotifierList.push_front(AfterChgAONotifier);
167 u->GetProperty().alwaysOnline.AddBeforeNotifier(&(*BeforeChgAONotifierList.begin()));
168 u->GetProperty().alwaysOnline.AddAfterNotifier(&(*AfterChgAONotifierList.begin()));
169 // ---------- AlwaysOnline end ---------------
171 // ---------- IP -------------------
172 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier(*this, u);
173 CHG_AFTER_NOTIFIER<USER_IPS> AfterChgIPNotifier(*this, u);
175 /*BeforeChgIPNotifier.SetAuthorizator(this);
176 BeforeChgIPNotifier.SetUser(u);*/
177 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
179 /*AfterChgIPNotifier.SetAuthorizator(this);
180 AfterChgIPNotifier.SetUser(u);*/
181 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
183 u->GetProperty().ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
184 u->GetProperty().ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
185 // ---------- IP end ---------------
187 //-----------------------------------------------------------------------------
188 void AUTH_AO::UnSetUserNotifiers(USER_PTR u)
190 // --- AlwaysOnline ---
191 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<int> > IsContainsUserAOB;
192 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<int> > IsContainsUserAOA;
194 list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
195 list<CHG_AFTER_NOTIFIER<int> >::iterator aoAIter;
197 aoBIter = find_if(BeforeChgAONotifierList.begin(),
198 BeforeChgAONotifierList.end(),
199 bind2nd(IsContainsUserAOB, u));
201 if (aoBIter != BeforeChgAONotifierList.end())
203 aoBIter->GetUser()->GetProperty().alwaysOnline.DelBeforeNotifier(&(*aoBIter));
204 BeforeChgAONotifierList.erase(aoBIter);
207 aoAIter = find_if(AfterChgAONotifierList.begin(),
208 AfterChgAONotifierList.end(),
209 bind2nd(IsContainsUserAOA, u));
211 if (aoAIter != AfterChgAONotifierList.end())
213 aoAIter->GetUser()->GetProperty().alwaysOnline.DelAfterNotifier(&(*aoAIter));
214 AfterChgAONotifierList.erase(aoAIter);
216 // --- AlwaysOnline end ---
219 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
220 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> > IsContainsUserIPA;
222 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
223 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator ipAIter;
225 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
226 BeforeChgIPNotifierList.end(),
227 bind2nd(IsContainsUserIPB, u));
229 if (ipBIter != BeforeChgIPNotifierList.end())
231 ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
232 BeforeChgIPNotifierList.erase(ipBIter);
235 ipAIter = find_if(AfterChgIPNotifierList.begin(),
236 AfterChgIPNotifierList.end(),
237 bind2nd(IsContainsUserIPA, u));
239 if (ipAIter != AfterChgIPNotifierList.end())
241 ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
242 AfterChgIPNotifierList.erase(ipAIter);
246 //-----------------------------------------------------------------------------
247 void AUTH_AO::GetUsers()
250 int h = users->OpenSearch();
253 printfd(__FILE__, "users->OpenSearch() error\n");
259 if (users->SearchNext(h, &u))
263 usersList.push_back(u);
267 users->CloseSearch(h);
269 //-----------------------------------------------------------------------------
270 void AUTH_AO::Unauthorize(USER_PTR u) const
272 u->Unauthorize(this);
274 //-----------------------------------------------------------------------------
275 void AUTH_AO::UpdateUserAuthorization(USER_PTR u) const
277 if (u->GetProperty().alwaysOnline)
279 USER_IPS ips = u->GetProperty().ips;
282 if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
288 //-----------------------------------------------------------------------------
289 void AUTH_AO::AddUser(USER_PTR u)
292 usersList.push_back(u);
293 UpdateUserAuthorization(u);
295 //-----------------------------------------------------------------------------
296 void AUTH_AO::DelUser(USER_PTR u)
299 UnSetUserNotifiers(u);
301 list<USER_PTR>::iterator users_iter;
302 users_iter = usersList.begin();
304 while (users_iter != usersList.end())
306 if (u == *users_iter)
308 usersList.erase(users_iter);
314 //-----------------------------------------------------------------------------
315 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
317 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
320 //-----------------------------------------------------------------------------
321 template <typename varParamType>
322 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
324 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
326 //-----------------------------------------------------------------------------
327 template <typename varParamType>
328 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
330 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
332 //-----------------------------------------------------------------------------