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 $
31 #include <algorithm> // for_each
32 #include <functional> // mem_fun_ref
35 #include "stg/users.h"
36 #include "stg/user_property.h"
37 #include "stg/common.h"
38 #include "stg/plugin_creator.h"
41 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
44 PLUGIN_CREATOR<AUTH_AO> aoc;
45 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
47 //-----------------------------------------------------------------------------
50 return aoc.GetPlugin();
52 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
55 template <typename varType>
56 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
59 bool operator()(varType notifier, USER_PTR user) const
61 return notifier.GetUser() == user;
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 const string AUTH_AO::GetVersion() const
69 return "Always Online authorizator v.1.0";
71 //-----------------------------------------------------------------------------
75 onAddUserNotifier(*this),
76 onDelUserNotifier(*this)
79 //-----------------------------------------------------------------------------
82 printfd(__FILE__, "AUTH_AO::Start()\n");
85 users->AddNotifierUserAdd(&onAddUserNotifier);
86 users->AddNotifierUserDel(&onDelUserNotifier);
88 std::for_each(usersList.begin(), usersList.end(), std::bind1st(std::mem_fun(&AUTH_AO::UpdateUserAuthorization), this));
94 //-----------------------------------------------------------------------------
97 printfd(__FILE__, "AUTH_AO::Stop()\n");
101 users->DelNotifierUserAdd(&onAddUserNotifier);
102 users->DelNotifierUserDel(&onDelUserNotifier);
104 list<USER_PTR>::iterator users_iter;
105 users_iter = usersList.begin();
106 while (users_iter != usersList.end())
108 Unauthorize(*users_iter);
109 UnSetUserNotifiers(*users_iter);
115 //-----------------------------------------------------------------------------
116 void AUTH_AO::SetUserNotifiers(USER_PTR u)
118 // ---------- AlwaysOnline -------------------
119 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
120 CHG_AFTER_NOTIFIER<int> AfterChgAONotifier(*this, u);
122 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
123 AfterChgAONotifierList.push_front(AfterChgAONotifier);
125 u->GetProperty().alwaysOnline.AddBeforeNotifier(&BeforeChgAONotifierList.front());
126 u->GetProperty().alwaysOnline.AddAfterNotifier(&AfterChgAONotifierList.front());
127 // ---------- AlwaysOnline end ---------------
129 // ---------- IP -------------------
130 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier(*this, u);
131 CHG_AFTER_NOTIFIER<USER_IPS> AfterChgIPNotifier(*this, u);
133 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
134 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
136 u->GetProperty().ips.AddBeforeNotifier(&BeforeChgIPNotifierList.front());
137 u->GetProperty().ips.AddAfterNotifier(&AfterChgIPNotifierList.front());
138 // ---------- IP end ---------------
140 //-----------------------------------------------------------------------------
141 void AUTH_AO::UnSetUserNotifiers(USER_PTR u)
143 // --- AlwaysOnline ---
144 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<int> > IsContainsUserAOB;
145 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<int> > IsContainsUserAOA;
147 list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
148 list<CHG_AFTER_NOTIFIER<int> >::iterator aoAIter;
150 aoBIter = find_if(BeforeChgAONotifierList.begin(),
151 BeforeChgAONotifierList.end(),
152 bind2nd(IsContainsUserAOB, u));
154 if (aoBIter != BeforeChgAONotifierList.end())
156 aoBIter->GetUser()->GetProperty().alwaysOnline.DelBeforeNotifier(&(*aoBIter));
157 BeforeChgAONotifierList.erase(aoBIter);
160 aoAIter = find_if(AfterChgAONotifierList.begin(),
161 AfterChgAONotifierList.end(),
162 bind2nd(IsContainsUserAOA, u));
164 if (aoAIter != AfterChgAONotifierList.end())
166 aoAIter->GetUser()->GetProperty().alwaysOnline.DelAfterNotifier(&(*aoAIter));
167 AfterChgAONotifierList.erase(aoAIter);
169 // --- AlwaysOnline end ---
172 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
173 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> > IsContainsUserIPA;
175 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
176 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator ipAIter;
178 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
179 BeforeChgIPNotifierList.end(),
180 bind2nd(IsContainsUserIPB, u));
182 if (ipBIter != BeforeChgIPNotifierList.end())
184 ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
185 BeforeChgIPNotifierList.erase(ipBIter);
188 ipAIter = find_if(AfterChgIPNotifierList.begin(),
189 AfterChgIPNotifierList.end(),
190 bind2nd(IsContainsUserIPA, u));
192 if (ipAIter != AfterChgIPNotifierList.end())
194 ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
195 AfterChgIPNotifierList.erase(ipAIter);
199 //-----------------------------------------------------------------------------
200 void AUTH_AO::GetUsers()
203 int h = users->OpenSearch();
204 assert(h && "USERS::OpenSearch is always correct");
206 while (!users->SearchNext(h, &u))
208 usersList.push_back(u);
212 users->CloseSearch(h);
214 //-----------------------------------------------------------------------------
215 void AUTH_AO::Unauthorize(USER_PTR u) const
217 u->Unauthorize(this);
219 //-----------------------------------------------------------------------------
220 void AUTH_AO::UpdateUserAuthorization(USER_PTR u) const
222 if (u->GetProperty().alwaysOnline)
224 USER_IPS ips = u->GetProperty().ips;
227 if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
233 //-----------------------------------------------------------------------------
234 void AUTH_AO::AddUser(USER_PTR u)
237 usersList.push_back(u);
238 UpdateUserAuthorization(u);
240 //-----------------------------------------------------------------------------
241 void AUTH_AO::DelUser(USER_PTR u)
244 UnSetUserNotifiers(u);
247 //-----------------------------------------------------------------------------
248 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
250 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
253 //-----------------------------------------------------------------------------
254 template <typename varParamType>
255 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
257 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
258 auth.Unauthorize(user);
260 //-----------------------------------------------------------------------------
261 template <typename varParamType>
262 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
264 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
265 auth.UpdateUserAuthorization(user);
267 //-----------------------------------------------------------------------------