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"
59 //-----------------------------------------------------------------------------
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
63 //-----------------------------------------------------------------------------
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 template <typename varType>
67 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
70 bool operator()(varType notifier, USER_PTR user) const
72 return notifier.GetUser() == user;
75 //-----------------------------------------------------------------------------
76 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
80 return aoc.GetPlugin();
82 //-----------------------------------------------------------------------------
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 const string AUTH_AO::GetVersion() const
87 return "Always Online authorizator v.1.0";
89 //-----------------------------------------------------------------------------
93 onAddUserNotifier(*this),
94 onDelUserNotifier(*this)
97 //-----------------------------------------------------------------------------
100 printfd(__FILE__, "AUTH_AO::Start()\n");
103 users->AddNotifierUserAdd(&onAddUserNotifier);
104 users->AddNotifierUserDel(&onDelUserNotifier);
106 std::for_each(usersList.begin(), usersList.end(), std::bind1st(std::mem_fun(&AUTH_AO::UpdateUserAuthorization), this));
112 //-----------------------------------------------------------------------------
115 printfd(__FILE__, "AUTH_AO::Stop()\n");
119 users->DelNotifierUserAdd(&onAddUserNotifier);
120 users->DelNotifierUserDel(&onDelUserNotifier);
122 list<USER_PTR>::iterator users_iter;
123 users_iter = usersList.begin();
124 while (users_iter != usersList.end())
126 Unauthorize(*users_iter);
127 UnSetUserNotifiers(*users_iter);
133 //-----------------------------------------------------------------------------
134 void AUTH_AO::SetUserNotifiers(USER_PTR u)
136 // ---------- AlwaysOnline -------------------
137 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
138 CHG_AFTER_NOTIFIER<int> AfterChgAONotifier(*this, u);
140 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
141 AfterChgAONotifierList.push_front(AfterChgAONotifier);
143 u->GetProperty().alwaysOnline.AddBeforeNotifier(&BeforeChgAONotifierList.front());
144 u->GetProperty().alwaysOnline.AddAfterNotifier(&AfterChgAONotifierList.front());
145 // ---------- AlwaysOnline end ---------------
147 // ---------- IP -------------------
148 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier(*this, u);
149 CHG_AFTER_NOTIFIER<USER_IPS> AfterChgIPNotifier(*this, u);
151 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
152 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
154 u->GetProperty().ips.AddBeforeNotifier(&BeforeChgIPNotifierList.front());
155 u->GetProperty().ips.AddAfterNotifier(&AfterChgIPNotifierList.front());
156 // ---------- IP end ---------------
158 //-----------------------------------------------------------------------------
159 void AUTH_AO::UnSetUserNotifiers(USER_PTR u)
161 // --- AlwaysOnline ---
162 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<int> > IsContainsUserAOB;
163 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<int> > IsContainsUserAOA;
165 list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
166 list<CHG_AFTER_NOTIFIER<int> >::iterator aoAIter;
168 aoBIter = find_if(BeforeChgAONotifierList.begin(),
169 BeforeChgAONotifierList.end(),
170 bind2nd(IsContainsUserAOB, u));
172 if (aoBIter != BeforeChgAONotifierList.end())
174 aoBIter->GetUser()->GetProperty().alwaysOnline.DelBeforeNotifier(&(*aoBIter));
175 BeforeChgAONotifierList.erase(aoBIter);
178 aoAIter = find_if(AfterChgAONotifierList.begin(),
179 AfterChgAONotifierList.end(),
180 bind2nd(IsContainsUserAOA, u));
182 if (aoAIter != AfterChgAONotifierList.end())
184 aoAIter->GetUser()->GetProperty().alwaysOnline.DelAfterNotifier(&(*aoAIter));
185 AfterChgAONotifierList.erase(aoAIter);
187 // --- AlwaysOnline end ---
190 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
191 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> > IsContainsUserIPA;
193 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
194 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator ipAIter;
196 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
197 BeforeChgIPNotifierList.end(),
198 bind2nd(IsContainsUserIPB, u));
200 if (ipBIter != BeforeChgIPNotifierList.end())
202 ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
203 BeforeChgIPNotifierList.erase(ipBIter);
206 ipAIter = find_if(AfterChgIPNotifierList.begin(),
207 AfterChgIPNotifierList.end(),
208 bind2nd(IsContainsUserIPA, u));
210 if (ipAIter != AfterChgIPNotifierList.end())
212 ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
213 AfterChgIPNotifierList.erase(ipAIter);
217 //-----------------------------------------------------------------------------
218 void AUTH_AO::GetUsers()
221 int h = users->OpenSearch();
224 printfd(__FILE__, "users->OpenSearch() error\n");
228 while (!users->SearchNext(h, &u))
230 usersList.push_back(u);
234 users->CloseSearch(h);
236 //-----------------------------------------------------------------------------
237 void AUTH_AO::Unauthorize(USER_PTR u) const
239 printfd(__FILE__, "AUTH_AO::Unauthorize - login: '%s'\n", u->GetLogin().c_str());
240 u->Unauthorize(this);
242 //-----------------------------------------------------------------------------
243 void AUTH_AO::UpdateUserAuthorization(USER_PTR u) const
245 printfd(__FILE__, "AUTH_AO::UpdateUserAuthorization - login: '%s'\n", u->GetLogin().c_str());
246 if (u->GetProperty().alwaysOnline)
248 USER_IPS ips = u->GetProperty().ips;
251 if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
257 //-----------------------------------------------------------------------------
258 void AUTH_AO::AddUser(USER_PTR u)
260 printfd(__FILE__, "AUTH_AO::AddUser - login: '%s'\n", u->GetLogin().c_str());
262 usersList.push_back(u);
263 UpdateUserAuthorization(u);
265 //-----------------------------------------------------------------------------
266 void AUTH_AO::DelUser(USER_PTR u)
268 printfd(__FILE__, "AUTH_AO::DelUser - login: '%s'\n", u->GetLogin().c_str());
270 UnSetUserNotifiers(u);
273 //-----------------------------------------------------------------------------
274 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
276 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
279 //-----------------------------------------------------------------------------
280 template <typename varParamType>
281 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
283 printfd(__FILE__, "CHG_BEFORE_NOTIFIER::Notify\n");
284 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
285 auth.Unauthorize(user);
287 //-----------------------------------------------------------------------------
288 template <typename varParamType>
289 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
291 printfd(__FILE__, "CHG_AFTER_NOTIFIER::Notify\n");
292 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
293 auth.UpdateUserAuthorization(user);
295 //-----------------------------------------------------------------------------