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 //-----------------------------------------------------------------------------
90 onAddUserNotifier(*this),
91 onDelUserNotifier(*this)
94 //-----------------------------------------------------------------------------
95 void AUTH_AO::SetUsers(USERS * u)
99 //-----------------------------------------------------------------------------
100 void AUTH_AO::SetSettings(const MODULE_SETTINGS & s)
104 //-----------------------------------------------------------------------------
105 int AUTH_AO::ParseSettings()
109 //-----------------------------------------------------------------------------
110 const string & AUTH_AO::GetStrError() const
114 //-----------------------------------------------------------------------------
119 list<user_iter>::iterator users_iter;
121 /*onAddUserNotifier.SetAuthorizator(this);
122 onDelUserNotifier.SetAuthorizator(this);*/
123 users->AddNotifierUserAdd(&onAddUserNotifier);
124 users->AddNotifierUserDel(&onDelUserNotifier);
126 users_iter = usersList.begin();
127 while (users_iter != usersList.end())
129 UpdateUserAuthorization(*users_iter);
135 //-----------------------------------------------------------------------------
141 users->DelNotifierUserAdd(&onAddUserNotifier);
142 users->DelNotifierUserDel(&onDelUserNotifier);
144 list<user_iter>::iterator users_iter;
145 users_iter = usersList.begin();
146 while (users_iter != usersList.end())
148 Unauthorize(*users_iter);
149 UnSetUserNotifiers(*users_iter);
155 //-----------------------------------------------------------------------------
156 bool AUTH_AO::IsRunning()
160 //-----------------------------------------------------------------------------
161 uint16_t AUTH_AO::GetStartPosition() const
165 //-----------------------------------------------------------------------------
166 uint16_t AUTH_AO::GetStopPosition() const
170 //-----------------------------------------------------------------------------
171 void AUTH_AO::SetUserNotifiers(user_iter u)
173 // ---------- AlwaysOnline -------------------
174 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
175 CHG_AFTER_NOTIFIER<int> AfterChgAONotifier(*this, u);
177 /*BeforeChgAONotifier.SetAuthorizator(this);
178 BeforeChgAONotifier.SetUser(u);*/
179 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
181 /*AfterChgAONotifier.SetAuthorizator(this);
182 AfterChgAONotifier.SetUser(u);*/
183 AfterChgAONotifierList.push_front(AfterChgAONotifier);
185 u->property.alwaysOnline.AddBeforeNotifier(&(*BeforeChgAONotifierList.begin()));
186 u->property.alwaysOnline.AddAfterNotifier(&(*AfterChgAONotifierList.begin()));
187 // ---------- AlwaysOnline end ---------------
189 // ---------- IP -------------------
190 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier(*this, u);
191 CHG_AFTER_NOTIFIER<USER_IPS> AfterChgIPNotifier(*this, u);
193 /*BeforeChgIPNotifier.SetAuthorizator(this);
194 BeforeChgIPNotifier.SetUser(u);*/
195 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
197 /*AfterChgIPNotifier.SetAuthorizator(this);
198 AfterChgIPNotifier.SetUser(u);*/
199 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
201 u->property.ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
202 u->property.ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
203 // ---------- IP end ---------------
205 //-----------------------------------------------------------------------------
206 void AUTH_AO::UnSetUserNotifiers(user_iter u)
208 // --- AlwaysOnline ---
209 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<int> > IsContainsUserAOB;
210 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<int> > IsContainsUserAOA;
212 list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
213 list<CHG_AFTER_NOTIFIER<int> >::iterator aoAIter;
215 aoBIter = find_if(BeforeChgAONotifierList.begin(),
216 BeforeChgAONotifierList.end(),
217 bind2nd(IsContainsUserAOB, u));
219 if (aoBIter != BeforeChgAONotifierList.end())
221 aoBIter->GetUser()->property.alwaysOnline.DelBeforeNotifier(&(*aoBIter));
222 BeforeChgAONotifierList.erase(aoBIter);
225 aoAIter = find_if(AfterChgAONotifierList.begin(),
226 AfterChgAONotifierList.end(),
227 bind2nd(IsContainsUserAOA, u));
229 if (aoAIter != AfterChgAONotifierList.end())
231 aoAIter->GetUser()->property.alwaysOnline.DelAfterNotifier(&(*aoAIter));
232 AfterChgAONotifierList.erase(aoAIter);
234 // --- AlwaysOnline end ---
237 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
238 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> > IsContainsUserIPA;
240 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
241 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator ipAIter;
243 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
244 BeforeChgIPNotifierList.end(),
245 bind2nd(IsContainsUserIPB, u));
247 if (ipBIter != BeforeChgIPNotifierList.end())
249 ipBIter->GetUser()->property.ips.DelBeforeNotifier(&(*ipBIter));
250 BeforeChgIPNotifierList.erase(ipBIter);
253 ipAIter = find_if(AfterChgIPNotifierList.begin(),
254 AfterChgIPNotifierList.end(),
255 bind2nd(IsContainsUserIPA, u));
257 if (ipAIter != AfterChgIPNotifierList.end())
259 ipAIter->GetUser()->property.ips.DelAfterNotifier(&(*ipAIter));
260 AfterChgIPNotifierList.erase(ipAIter);
264 //-----------------------------------------------------------------------------
265 void AUTH_AO::GetUsers()
268 int h = users->OpenSearch();
271 printfd(__FILE__, "users->OpenSearch() error\n");
277 if (users->SearchNext(h, &u))
281 usersList.push_back(u);
285 users->CloseSearch(h);
287 //-----------------------------------------------------------------------------
288 void AUTH_AO::Unauthorize(user_iter u) const
290 u->Unauthorize(this);
292 //-----------------------------------------------------------------------------
293 void AUTH_AO::UpdateUserAuthorization(user_iter u) const
295 if (u->property.alwaysOnline)
297 USER_IPS ips = u->property.ips;
300 if (u->Authorize(ips[0].ip, "", 0xFFffFFff, this) == 0)
306 //-----------------------------------------------------------------------------
307 void AUTH_AO::AddUser(user_iter u)
310 usersList.push_back(u);
311 UpdateUserAuthorization(u);
313 //-----------------------------------------------------------------------------
314 void AUTH_AO::DelUser(user_iter u)
317 UnSetUserNotifiers(u);
319 list<user_iter>::iterator users_iter;
320 users_iter = usersList.begin();
322 while (users_iter != usersList.end())
324 if (u == *users_iter)
326 usersList.erase(users_iter);
332 //-----------------------------------------------------------------------------
333 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
335 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
338 //-----------------------------------------------------------------------------
339 template <typename varParamType>
340 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
342 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
344 //-----------------------------------------------------------------------------
345 template <typename varParamType>
346 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
348 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
350 //-----------------------------------------------------------------------------