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 $
34 #include "../../../user_property.h"
35 #include "../../../eventloop.h"
57 //-----------------------------------------------------------------------------
58 //-----------------------------------------------------------------------------
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
64 // ëÌÁÓÓ ÄÌÑ ÐÏÉÓËÁ ÀÚÅÒÁ × ÓÐÉÓËÅ ÎÏÔÉÆÉËÁÔÏÒÏ×
65 template <typename varType>
66 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
69 bool operator()(varType notifier, USER_PTR user) const
71 return notifier.GetUser() == user;
74 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
79 return aoc.GetPlugin();
81 //-----------------------------------------------------------------------------
82 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
84 const string AUTH_AO::GetVersion() const
86 return "Always Online authorizator v.1.0";
88 //-----------------------------------------------------------------------------
92 onAddUserNotifier(*this),
93 onDelUserNotifier(*this)
96 //-----------------------------------------------------------------------------
97 void AUTH_AO::SetUsers(USERS * u)
101 //-----------------------------------------------------------------------------
102 void AUTH_AO::SetSettings(const MODULE_SETTINGS & s)
106 //-----------------------------------------------------------------------------
107 int AUTH_AO::ParseSettings()
111 //-----------------------------------------------------------------------------
112 const string & AUTH_AO::GetStrError() const
116 //-----------------------------------------------------------------------------
121 list<USER_PTR>::iterator users_iter;
123 /*onAddUserNotifier.SetAuthorizator(this);
124 onDelUserNotifier.SetAuthorizator(this);*/
125 users->AddNotifierUserAdd(&onAddUserNotifier);
126 users->AddNotifierUserDel(&onDelUserNotifier);
128 users_iter = usersList.begin();
129 while (users_iter != usersList.end())
131 UpdateUserAuthorization(*users_iter);
137 //-----------------------------------------------------------------------------
143 users->DelNotifierUserAdd(&onAddUserNotifier);
144 users->DelNotifierUserDel(&onDelUserNotifier);
146 list<USER_PTR>::iterator users_iter;
147 users_iter = usersList.begin();
148 while (users_iter != usersList.end())
150 Unauthorize(*users_iter);
151 UnSetUserNotifiers(*users_iter);
157 //-----------------------------------------------------------------------------
158 bool AUTH_AO::IsRunning()
162 //-----------------------------------------------------------------------------
163 uint16_t AUTH_AO::GetStartPosition() const
167 //-----------------------------------------------------------------------------
168 uint16_t AUTH_AO::GetStopPosition() const
172 //-----------------------------------------------------------------------------
173 void AUTH_AO::SetUserNotifiers(USER_PTR u)
175 // ---------- AlwaysOnline -------------------
176 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
177 CHG_AFTER_NOTIFIER<int> AfterChgAONotifier(*this, u);
179 /*BeforeChgAONotifier.SetAuthorizator(this);
180 BeforeChgAONotifier.SetUser(u);*/
181 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
183 /*AfterChgAONotifier.SetAuthorizator(this);
184 AfterChgAONotifier.SetUser(u);*/
185 AfterChgAONotifierList.push_front(AfterChgAONotifier);
187 u->GetProperty().alwaysOnline.AddBeforeNotifier(&(*BeforeChgAONotifierList.begin()));
188 u->GetProperty().alwaysOnline.AddAfterNotifier(&(*AfterChgAONotifierList.begin()));
189 // ---------- AlwaysOnline end ---------------
191 // ---------- IP -------------------
192 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier(*this, u);
193 CHG_AFTER_NOTIFIER<USER_IPS> AfterChgIPNotifier(*this, u);
195 /*BeforeChgIPNotifier.SetAuthorizator(this);
196 BeforeChgIPNotifier.SetUser(u);*/
197 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
199 /*AfterChgIPNotifier.SetAuthorizator(this);
200 AfterChgIPNotifier.SetUser(u);*/
201 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
203 u->GetProperty().ips.AddBeforeNotifier(&(*BeforeChgIPNotifierList.begin()));
204 u->GetProperty().ips.AddAfterNotifier(&(*AfterChgIPNotifierList.begin()));
205 // ---------- IP end ---------------
207 //-----------------------------------------------------------------------------
208 void AUTH_AO::UnSetUserNotifiers(USER_PTR u)
210 // --- AlwaysOnline ---
211 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<int> > IsContainsUserAOB;
212 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<int> > IsContainsUserAOA;
214 list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
215 list<CHG_AFTER_NOTIFIER<int> >::iterator aoAIter;
217 aoBIter = find_if(BeforeChgAONotifierList.begin(),
218 BeforeChgAONotifierList.end(),
219 bind2nd(IsContainsUserAOB, u));
221 if (aoBIter != BeforeChgAONotifierList.end())
223 aoBIter->GetUser()->GetProperty().alwaysOnline.DelBeforeNotifier(&(*aoBIter));
224 BeforeChgAONotifierList.erase(aoBIter);
227 aoAIter = find_if(AfterChgAONotifierList.begin(),
228 AfterChgAONotifierList.end(),
229 bind2nd(IsContainsUserAOA, u));
231 if (aoAIter != AfterChgAONotifierList.end())
233 aoAIter->GetUser()->GetProperty().alwaysOnline.DelAfterNotifier(&(*aoAIter));
234 AfterChgAONotifierList.erase(aoAIter);
236 // --- AlwaysOnline end ---
239 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
240 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> > IsContainsUserIPA;
242 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
243 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator ipAIter;
245 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
246 BeforeChgIPNotifierList.end(),
247 bind2nd(IsContainsUserIPB, u));
249 if (ipBIter != BeforeChgIPNotifierList.end())
251 ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
252 BeforeChgIPNotifierList.erase(ipBIter);
255 ipAIter = find_if(AfterChgIPNotifierList.begin(),
256 AfterChgIPNotifierList.end(),
257 bind2nd(IsContainsUserIPA, u));
259 if (ipAIter != AfterChgIPNotifierList.end())
261 ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
262 AfterChgIPNotifierList.erase(ipAIter);
266 //-----------------------------------------------------------------------------
267 void AUTH_AO::GetUsers()
270 int h = users->OpenSearch();
273 printfd(__FILE__, "users->OpenSearch() error\n");
279 if (users->SearchNext(h, &u))
283 usersList.push_back(u);
287 users->CloseSearch(h);
289 //-----------------------------------------------------------------------------
290 void AUTH_AO::Unauthorize(USER_PTR u) const
292 u->Unauthorize(this);
294 //-----------------------------------------------------------------------------
295 void AUTH_AO::UpdateUserAuthorization(USER_PTR u) const
297 if (u->GetProperty().alwaysOnline)
299 USER_IPS ips = u->GetProperty().ips;
302 if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
308 //-----------------------------------------------------------------------------
309 void AUTH_AO::AddUser(USER_PTR u)
312 usersList.push_back(u);
313 UpdateUserAuthorization(u);
315 //-----------------------------------------------------------------------------
316 void AUTH_AO::DelUser(USER_PTR u)
319 UnSetUserNotifiers(u);
321 list<USER_PTR>::iterator users_iter;
322 users_iter = usersList.begin();
324 while (users_iter != usersList.end())
326 if (u == *users_iter)
328 usersList.erase(users_iter);
334 //-----------------------------------------------------------------------------
335 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
337 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
340 //-----------------------------------------------------------------------------
341 template <typename varParamType>
342 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
344 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
346 //-----------------------------------------------------------------------------
347 template <typename varParamType>
348 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
350 EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
352 //-----------------------------------------------------------------------------