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 $
32 #include "../../../user.h"
33 #include "../../../eventloop.h"
55 //-----------------------------------------------------------------------------
56 //-----------------------------------------------------------------------------
57 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 // ëÌÁÓÓ ÄÌÑ ÐÏÉÓËÁ ÀÚÅÒÁ × ÓÐÉÓËÅ ÎÏÔÉÆÉËÁÔÏÒÏ×
63 template <typename varType>
64 class IS_CONTAINS_USER: public binary_function<varType, user_iter, bool>
67 bool operator()(varType notifier, user_iter user) const
69 return notifier.GetUser() == user;
72 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
75 BASE_PLUGIN * GetPlugin()
77 return aoc.GetPlugin();
79 //-----------------------------------------------------------------------------
80 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
82 const string AUTH_AO::GetVersion() const
84 return "Always Online authorizator v.1.0";
86 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
92 void AUTH_AO::SetUsers(USERS * u)
96 //-----------------------------------------------------------------------------
97 void AUTH_AO::SetSettings(const MODULE_SETTINGS & s)
101 //-----------------------------------------------------------------------------
102 int AUTH_AO::ParseSettings()
106 //-----------------------------------------------------------------------------
107 const string & AUTH_AO::GetStrError() const
111 //-----------------------------------------------------------------------------
116 list<user_iter>::iterator users_iter;
118 onAddUserNotifier.SetAuthorizator(this);
119 onDelUserNotifier.SetAuthorizator(this);
120 users->AddNotifierUserAdd(&onAddUserNotifier);
121 users->AddNotifierUserDel(&onDelUserNotifier);
123 users_iter = usersList.begin();
124 while (users_iter != usersList.end())
126 UpdateUserAuthorization(*users_iter);
132 //-----------------------------------------------------------------------------
138 users->DelNotifierUserAdd(&onAddUserNotifier);
139 users->DelNotifierUserDel(&onDelUserNotifier);
141 list<user_iter>::iterator users_iter;
142 users_iter = usersList.begin();
143 while (users_iter != usersList.end())
145 Unauthorize(*users_iter);
146 UnSetUserNotifiers(*users_iter);
152 //-----------------------------------------------------------------------------
153 bool AUTH_AO::IsRunning()
157 //-----------------------------------------------------------------------------
158 uint16_t AUTH_AO::GetStartPosition() const
162 //-----------------------------------------------------------------------------
163 uint16_t AUTH_AO::GetStopPosition() const
167 //-----------------------------------------------------------------------------
168 void AUTH_AO::SetUserNotifiers(user_iter u)
170 // ---------- AlwaysOnline -------------------
171 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier;
172 CHG_AFTER_NOTIFIER<int> AfterChgAONotifier;
174 BeforeChgAONotifier.SetAuthorizator(this);
175 BeforeChgAONotifier.SetUser(u);
176 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
178 AfterChgAONotifier.SetAuthorizator(this);
179 AfterChgAONotifier.SetUser(u);
180 AfterChgAONotifierList.push_front(AfterChgAONotifier);
182 u->property.alwaysOnline.AddBeforeNotifier(&(*BeforeChgAONotifierList.begin()));
183 u->property.alwaysOnline.AddAfterNotifier(&(*AfterChgAONotifierList.begin()));
184 // ---------- AlwaysOnline end ---------------
186 // ---------- IP -------------------
187 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier;
188 CHG_AFTER_NOTIFIER<USER_IPS> AfterChgIPNotifier;
190 BeforeChgIPNotifier.SetAuthorizator(this);
191 BeforeChgIPNotifier.SetUser(u);
192 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
194 AfterChgIPNotifier.SetAuthorizator(this);
195 AfterChgIPNotifier.SetUser(u);
196 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
198 u->property.ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
199 u->property.ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
200 // ---------- IP end ---------------
202 //-----------------------------------------------------------------------------
203 void AUTH_AO::UnSetUserNotifiers(user_iter u)
205 // --- AlwaysOnline ---
206 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<int> > IsContainsUserAOB;
207 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<int> > IsContainsUserAOA;
209 list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
210 list<CHG_AFTER_NOTIFIER<int> >::iterator aoAIter;
212 aoBIter = find_if(BeforeChgAONotifierList.begin(),
213 BeforeChgAONotifierList.end(),
214 bind2nd(IsContainsUserAOB, u));
216 if (aoBIter != BeforeChgAONotifierList.end())
218 aoBIter->GetUser()->property.alwaysOnline.DelBeforeNotifier(&(*aoBIter));
219 BeforeChgAONotifierList.erase(aoBIter);
222 aoAIter = find_if(AfterChgAONotifierList.begin(),
223 AfterChgAONotifierList.end(),
224 bind2nd(IsContainsUserAOA, u));
226 if (aoAIter != AfterChgAONotifierList.end())
228 aoAIter->GetUser()->property.alwaysOnline.DelAfterNotifier(&(*aoAIter));
229 AfterChgAONotifierList.erase(aoAIter);
231 // --- AlwaysOnline end ---
234 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
235 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> > IsContainsUserIPA;
237 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
238 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator ipAIter;
240 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
241 BeforeChgIPNotifierList.end(),
242 bind2nd(IsContainsUserIPB, u));
244 if (ipBIter != BeforeChgIPNotifierList.end())
246 ipBIter->GetUser()->property.ips.DelBeforeNotifier(&(*ipBIter));
247 BeforeChgIPNotifierList.erase(ipBIter);
250 ipAIter = find_if(AfterChgIPNotifierList.begin(),
251 AfterChgIPNotifierList.end(),
252 bind2nd(IsContainsUserIPA, u));
254 if (ipAIter != AfterChgIPNotifierList.end())
256 ipAIter->GetUser()->property.ips.DelAfterNotifier(&(*ipAIter));
257 AfterChgIPNotifierList.erase(ipAIter);
261 //-----------------------------------------------------------------------------
262 void AUTH_AO::GetUsers()
265 int h = users->OpenSearch();
268 printfd(__FILE__, "users->OpenSearch() error\n");
274 if (users->SearchNext(h, &u))
278 usersList.push_back(u);
282 users->CloseSearch(h);
284 //-----------------------------------------------------------------------------
285 void AUTH_AO::Unauthorize(user_iter u) const
287 u->Unauthorize(this);
289 //-----------------------------------------------------------------------------
290 void AUTH_AO::UpdateUserAuthorization(user_iter u) const
292 if (u->property.alwaysOnline)
294 USER_IPS ips = u->property.ips;
297 if (u->Authorize(ips[0].ip, "", 0xFFffFFff, this) == 0)
303 //-----------------------------------------------------------------------------
304 void AUTH_AO::AddUser(user_iter u)
307 usersList.push_back(u);
308 UpdateUserAuthorization(u);
310 //-----------------------------------------------------------------------------
311 void AUTH_AO::DelUser(user_iter u)
314 UnSetUserNotifiers(u);
316 list<user_iter>::iterator users_iter;
317 users_iter = usersList.begin();
319 while (users_iter != usersList.end())
321 if (u == *users_iter)
323 usersList.erase(users_iter);
329 //-----------------------------------------------------------------------------
330 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
332 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
335 //-----------------------------------------------------------------------------
336 template <typename varParamType>
337 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
339 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(*auth, &AUTH_AO::Unauthorize, user);
341 //-----------------------------------------------------------------------------
342 template <typename varParamType>
343 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
345 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(*auth, &AUTH_AO::UpdateUserAuthorization, user);
347 //-----------------------------------------------------------------------------