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
34 #include "stg/users.h"
35 #include "stg/user_property.h"
36 #include "stg/common.h"
37 #include "stg/plugin_creator.h"
40 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
42 //-----------------------------------------------------------------------------
43 PLUGIN_CREATOR<AUTH_AO> aoc;
44 //-----------------------------------------------------------------------------
45 //-----------------------------------------------------------------------------
46 //-----------------------------------------------------------------------------
49 return aoc.GetPlugin();
51 //-----------------------------------------------------------------------------
52 //-----------------------------------------------------------------------------
53 //-----------------------------------------------------------------------------
54 template <typename varType>
55 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
58 bool operator()(varType notifier, USER_PTR user) const
60 return notifier.GetUser() == user;
63 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 const string AUTH_AO::GetVersion() const
68 return "Always Online authorizator v.1.0";
70 //-----------------------------------------------------------------------------
74 onAddUserNotifier(*this),
75 onDelUserNotifier(*this)
78 //-----------------------------------------------------------------------------
81 printfd(__FILE__, "AUTH_AO::Start()\n");
84 users->AddNotifierUserAdd(&onAddUserNotifier);
85 users->AddNotifierUserDel(&onDelUserNotifier);
87 std::for_each(usersList.begin(), usersList.end(), std::bind1st(std::mem_fun(&AUTH_AO::UpdateUserAuthorization), this));
93 //-----------------------------------------------------------------------------
96 printfd(__FILE__, "AUTH_AO::Stop()\n");
100 users->DelNotifierUserAdd(&onAddUserNotifier);
101 users->DelNotifierUserDel(&onDelUserNotifier);
103 list<USER_PTR>::iterator users_iter;
104 users_iter = usersList.begin();
105 while (users_iter != usersList.end())
107 Unauthorize(*users_iter);
108 UnSetUserNotifiers(*users_iter);
114 //-----------------------------------------------------------------------------
115 void AUTH_AO::SetUserNotifiers(USER_PTR u)
117 // ---------- AlwaysOnline -------------------
118 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
119 CHG_AFTER_NOTIFIER<int> AfterChgAONotifier(*this, u);
121 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
122 AfterChgAONotifierList.push_front(AfterChgAONotifier);
124 u->GetProperty().alwaysOnline.AddBeforeNotifier(&BeforeChgAONotifierList.front());
125 u->GetProperty().alwaysOnline.AddAfterNotifier(&AfterChgAONotifierList.front());
126 // ---------- AlwaysOnline end ---------------
128 // ---------- IP -------------------
129 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier(*this, u);
130 CHG_AFTER_NOTIFIER<USER_IPS> AfterChgIPNotifier(*this, u);
132 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
133 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
135 u->GetProperty().ips.AddBeforeNotifier(&BeforeChgIPNotifierList.front());
136 u->GetProperty().ips.AddAfterNotifier(&AfterChgIPNotifierList.front());
137 // ---------- IP end ---------------
139 //-----------------------------------------------------------------------------
140 void AUTH_AO::UnSetUserNotifiers(USER_PTR u)
142 // --- AlwaysOnline ---
143 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<int> > IsContainsUserAOB;
144 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<int> > IsContainsUserAOA;
146 list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
147 list<CHG_AFTER_NOTIFIER<int> >::iterator aoAIter;
149 aoBIter = find_if(BeforeChgAONotifierList.begin(),
150 BeforeChgAONotifierList.end(),
151 bind2nd(IsContainsUserAOB, u));
153 if (aoBIter != BeforeChgAONotifierList.end())
155 aoBIter->GetUser()->GetProperty().alwaysOnline.DelBeforeNotifier(&(*aoBIter));
156 BeforeChgAONotifierList.erase(aoBIter);
159 aoAIter = find_if(AfterChgAONotifierList.begin(),
160 AfterChgAONotifierList.end(),
161 bind2nd(IsContainsUserAOA, u));
163 if (aoAIter != AfterChgAONotifierList.end())
165 aoAIter->GetUser()->GetProperty().alwaysOnline.DelAfterNotifier(&(*aoAIter));
166 AfterChgAONotifierList.erase(aoAIter);
168 // --- AlwaysOnline end ---
171 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
172 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> > IsContainsUserIPA;
174 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
175 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator ipAIter;
177 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
178 BeforeChgIPNotifierList.end(),
179 bind2nd(IsContainsUserIPB, u));
181 if (ipBIter != BeforeChgIPNotifierList.end())
183 ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
184 BeforeChgIPNotifierList.erase(ipBIter);
187 ipAIter = find_if(AfterChgIPNotifierList.begin(),
188 AfterChgIPNotifierList.end(),
189 bind2nd(IsContainsUserIPA, u));
191 if (ipAIter != AfterChgIPNotifierList.end())
193 ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
194 AfterChgIPNotifierList.erase(ipAIter);
198 //-----------------------------------------------------------------------------
199 void AUTH_AO::GetUsers()
202 int h = users->OpenSearch();
205 printfd(__FILE__, "users->OpenSearch() error\n");
209 while (!users->SearchNext(h, &u))
211 usersList.push_back(u);
215 users->CloseSearch(h);
217 //-----------------------------------------------------------------------------
218 void AUTH_AO::Unauthorize(USER_PTR u) const
220 u->Unauthorize(this);
222 //-----------------------------------------------------------------------------
223 void AUTH_AO::UpdateUserAuthorization(USER_PTR u) const
225 if (u->GetProperty().alwaysOnline)
227 USER_IPS ips = u->GetProperty().ips;
230 if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
236 //-----------------------------------------------------------------------------
237 void AUTH_AO::AddUser(USER_PTR u)
240 usersList.push_back(u);
241 UpdateUserAuthorization(u);
243 //-----------------------------------------------------------------------------
244 void AUTH_AO::DelUser(USER_PTR u)
247 UnSetUserNotifiers(u);
250 //-----------------------------------------------------------------------------
251 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
253 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
256 //-----------------------------------------------------------------------------
257 template <typename varParamType>
258 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
260 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
261 auth.Unauthorize(user);
263 //-----------------------------------------------------------------------------
264 template <typename varParamType>
265 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
267 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
268 auth.UpdateUserAuthorization(user);
270 //-----------------------------------------------------------------------------