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"
38 #include "../../../eventloop.h"
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 template <typename varType>
68 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
71 bool operator()(varType notifier, USER_PTR user) const
73 return notifier.GetUser() == user;
76 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
81 return aoc.GetPlugin();
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
86 const string AUTH_AO::GetVersion() const
88 return "Always Online authorizator v.1.0";
90 //-----------------------------------------------------------------------------
94 onAddUserNotifier(*this),
95 onDelUserNotifier(*this)
98 //-----------------------------------------------------------------------------
103 users->AddNotifierUserAdd(&onAddUserNotifier);
104 users->AddNotifierUserDel(&onDelUserNotifier);
106 /*list<USER_PTR>::iterator it = usersList.begin();
107 while (it != usersList.end())
109 UpdateUserAuthorization(*it);
112 std::for_each(usersList.begin(), usersList.end(), std::bind1st(std::mem_fun(&AUTH_AO::UpdateUserAuthorization), this));
118 //-----------------------------------------------------------------------------
124 users->DelNotifierUserAdd(&onAddUserNotifier);
125 users->DelNotifierUserDel(&onDelUserNotifier);
127 list<USER_PTR>::iterator users_iter;
128 users_iter = usersList.begin();
129 while (users_iter != usersList.end())
131 Unauthorize(*users_iter);
132 UnSetUserNotifiers(*users_iter);
138 //-----------------------------------------------------------------------------
139 void AUTH_AO::SetUserNotifiers(USER_PTR u)
141 // ---------- AlwaysOnline -------------------
142 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
143 CHG_AFTER_NOTIFIER<int> AfterChgAONotifier(*this, u);
145 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
146 AfterChgAONotifierList.push_front(AfterChgAONotifier);
148 u->GetProperty().alwaysOnline.AddBeforeNotifier(&(*BeforeChgAONotifierList.begin()));
149 u->GetProperty().alwaysOnline.AddAfterNotifier(&(*AfterChgAONotifierList.begin()));
150 // ---------- AlwaysOnline end ---------------
152 // ---------- IP -------------------
153 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier(*this, u);
154 CHG_AFTER_NOTIFIER<USER_IPS> AfterChgIPNotifier(*this, u);
156 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
157 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
159 u->GetProperty().ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
160 u->GetProperty().ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
161 // ---------- IP end ---------------
163 //-----------------------------------------------------------------------------
164 void AUTH_AO::UnSetUserNotifiers(USER_PTR u)
166 // --- AlwaysOnline ---
167 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<int> > IsContainsUserAOB;
168 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<int> > IsContainsUserAOA;
170 list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
171 list<CHG_AFTER_NOTIFIER<int> >::iterator aoAIter;
173 aoBIter = find_if(BeforeChgAONotifierList.begin(),
174 BeforeChgAONotifierList.end(),
175 bind2nd(IsContainsUserAOB, u));
177 if (aoBIter != BeforeChgAONotifierList.end())
179 aoBIter->GetUser()->GetProperty().alwaysOnline.DelBeforeNotifier(&(*aoBIter));
180 BeforeChgAONotifierList.erase(aoBIter);
183 aoAIter = find_if(AfterChgAONotifierList.begin(),
184 AfterChgAONotifierList.end(),
185 bind2nd(IsContainsUserAOA, u));
187 if (aoAIter != AfterChgAONotifierList.end())
189 aoAIter->GetUser()->GetProperty().alwaysOnline.DelAfterNotifier(&(*aoAIter));
190 AfterChgAONotifierList.erase(aoAIter);
192 // --- AlwaysOnline end ---
195 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
196 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> > IsContainsUserIPA;
198 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
199 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator ipAIter;
201 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
202 BeforeChgIPNotifierList.end(),
203 bind2nd(IsContainsUserIPB, u));
205 if (ipBIter != BeforeChgIPNotifierList.end())
207 ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
208 BeforeChgIPNotifierList.erase(ipBIter);
211 ipAIter = find_if(AfterChgIPNotifierList.begin(),
212 AfterChgIPNotifierList.end(),
213 bind2nd(IsContainsUserIPA, u));
215 if (ipAIter != AfterChgIPNotifierList.end())
217 ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
218 AfterChgIPNotifierList.erase(ipAIter);
222 //-----------------------------------------------------------------------------
223 void AUTH_AO::GetUsers()
226 int h = users->OpenSearch();
229 printfd(__FILE__, "users->OpenSearch() error\n");
233 while (users->SearchNext(h, &u))
235 usersList.push_back(u);
239 users->CloseSearch(h);
241 //-----------------------------------------------------------------------------
242 void AUTH_AO::Unauthorize(USER_PTR u) const
244 u->Unauthorize(this);
246 //-----------------------------------------------------------------------------
247 void AUTH_AO::UpdateUserAuthorization(USER_PTR u) const
249 if (u->GetProperty().alwaysOnline)
251 USER_IPS ips = u->GetProperty().ips;
254 if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
260 //-----------------------------------------------------------------------------
261 void AUTH_AO::AddUser(USER_PTR u)
264 usersList.push_back(u);
265 UpdateUserAuthorization(u);
267 //-----------------------------------------------------------------------------
268 void AUTH_AO::DelUser(USER_PTR u)
271 UnSetUserNotifiers(u);
274 /*list<USER_PTR>::iterator users_iter;
275 users_iter = usersList.begin();
277 while (users_iter != usersList.end())
279 if (u == *users_iter)
281 usersList.erase(users_iter);
287 //-----------------------------------------------------------------------------
288 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
290 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
293 //-----------------------------------------------------------------------------
294 template <typename varParamType>
295 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
297 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
299 //-----------------------------------------------------------------------------
300 template <typename varParamType>
301 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
303 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
305 //-----------------------------------------------------------------------------