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 //-----------------------------------------------------------------------------
101 printfd(__FILE__, "AUTH_AO::Start()\n");
104 users->AddNotifierUserAdd(&onAddUserNotifier);
105 users->AddNotifierUserDel(&onDelUserNotifier);
107 std::for_each(usersList.begin(), usersList.end(), std::bind1st(std::mem_fun(&AUTH_AO::UpdateUserAuthorization), this));
113 //-----------------------------------------------------------------------------
116 printfd(__FILE__, "AUTH_AO::Stop()\n");
120 users->DelNotifierUserAdd(&onAddUserNotifier);
121 users->DelNotifierUserDel(&onDelUserNotifier);
123 list<USER_PTR>::iterator users_iter;
124 users_iter = usersList.begin();
125 while (users_iter != usersList.end())
127 Unauthorize(*users_iter);
128 UnSetUserNotifiers(*users_iter);
134 //-----------------------------------------------------------------------------
135 void AUTH_AO::SetUserNotifiers(USER_PTR u)
137 // ---------- AlwaysOnline -------------------
138 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
139 CHG_AFTER_NOTIFIER<int> AfterChgAONotifier(*this, u);
141 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
142 AfterChgAONotifierList.push_front(AfterChgAONotifier);
144 u->GetProperty().alwaysOnline.AddBeforeNotifier(&BeforeChgAONotifierList.front());
145 u->GetProperty().alwaysOnline.AddAfterNotifier(&AfterChgAONotifierList.front());
146 // ---------- AlwaysOnline end ---------------
148 // ---------- IP -------------------
149 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier(*this, u);
150 CHG_AFTER_NOTIFIER<USER_IPS> AfterChgIPNotifier(*this, u);
152 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
153 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
155 u->GetProperty().ips.AddBeforeNotifier(&BeforeChgIPNotifierList.front());
156 u->GetProperty().ips.AddAfterNotifier(&AfterChgIPNotifierList.front());
157 // ---------- IP end ---------------
159 //-----------------------------------------------------------------------------
160 void AUTH_AO::UnSetUserNotifiers(USER_PTR u)
162 // --- AlwaysOnline ---
163 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<int> > IsContainsUserAOB;
164 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<int> > IsContainsUserAOA;
166 list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
167 list<CHG_AFTER_NOTIFIER<int> >::iterator aoAIter;
169 aoBIter = find_if(BeforeChgAONotifierList.begin(),
170 BeforeChgAONotifierList.end(),
171 bind2nd(IsContainsUserAOB, u));
173 if (aoBIter != BeforeChgAONotifierList.end())
175 aoBIter->GetUser()->GetProperty().alwaysOnline.DelBeforeNotifier(&(*aoBIter));
176 BeforeChgAONotifierList.erase(aoBIter);
179 aoAIter = find_if(AfterChgAONotifierList.begin(),
180 AfterChgAONotifierList.end(),
181 bind2nd(IsContainsUserAOA, u));
183 if (aoAIter != AfterChgAONotifierList.end())
185 aoAIter->GetUser()->GetProperty().alwaysOnline.DelAfterNotifier(&(*aoAIter));
186 AfterChgAONotifierList.erase(aoAIter);
188 // --- AlwaysOnline end ---
191 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
192 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> > IsContainsUserIPA;
194 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
195 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator ipAIter;
197 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
198 BeforeChgIPNotifierList.end(),
199 bind2nd(IsContainsUserIPB, u));
201 if (ipBIter != BeforeChgIPNotifierList.end())
203 ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
204 BeforeChgIPNotifierList.erase(ipBIter);
207 ipAIter = find_if(AfterChgIPNotifierList.begin(),
208 AfterChgIPNotifierList.end(),
209 bind2nd(IsContainsUserIPA, u));
211 if (ipAIter != AfterChgIPNotifierList.end())
213 ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
214 AfterChgIPNotifierList.erase(ipAIter);
218 //-----------------------------------------------------------------------------
219 void AUTH_AO::GetUsers()
222 int h = users->OpenSearch();
225 printfd(__FILE__, "users->OpenSearch() error\n");
229 while (!users->SearchNext(h, &u))
231 usersList.push_back(u);
235 users->CloseSearch(h);
237 //-----------------------------------------------------------------------------
238 void AUTH_AO::Unauthorize(USER_PTR u) const
240 printfd(__FILE__, "AUTH_AO::Unauthorize - login: '%s'\n", u->GetLogin().c_str());
241 u->Unauthorize(this);
243 //-----------------------------------------------------------------------------
244 void AUTH_AO::UpdateUserAuthorization(USER_PTR u) const
246 printfd(__FILE__, "AUTH_AO::UpdateUserAuthorization - login: '%s'\n", u->GetLogin().c_str());
247 if (u->GetProperty().alwaysOnline)
249 USER_IPS ips = u->GetProperty().ips;
252 if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
258 //-----------------------------------------------------------------------------
259 void AUTH_AO::AddUser(USER_PTR u)
261 printfd(__FILE__, "AUTH_AO::AddUser - login: '%s'\n", u->GetLogin().c_str());
263 usersList.push_back(u);
264 UpdateUserAuthorization(u);
266 //-----------------------------------------------------------------------------
267 void AUTH_AO::DelUser(USER_PTR u)
269 printfd(__FILE__, "AUTH_AO::DelUser - login: '%s'\n", u->GetLogin().c_str());
271 UnSetUserNotifiers(u);
274 //-----------------------------------------------------------------------------
275 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
277 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
280 //-----------------------------------------------------------------------------
281 template <typename varParamType>
282 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
284 printfd(__FILE__, "CHG_BEFORE_NOTIFIER::Notify\n");
285 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
286 auth.Unauthorize(user);
288 //-----------------------------------------------------------------------------
289 template <typename varParamType>
290 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
292 printfd(__FILE__, "CHG_AFTER_NOTIFIER::Notify\n");
293 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
294 auth.UpdateUserAuthorization(user);
296 //-----------------------------------------------------------------------------