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 static 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 std::binary_function<varType, USER_PTR, bool>
59 bool operator()(const varType& notifier, USER_PTR user) const
61 return notifier.GetUser() == user;
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 std::string AUTH_AO::GetVersion() const
69 return "Always Online authorizator v.1.0";
71 //-----------------------------------------------------------------------------
75 onAddUserNotifier(*this),
76 onDelUserNotifier(*this),
77 logger(GetPluginLogger(GetStgLogger(), "auth_ao"))
80 //-----------------------------------------------------------------------------
83 printfd(__FILE__, "AUTH_AO::Start()\n");
86 users->AddNotifierUserAdd(&onAddUserNotifier);
87 users->AddNotifierUserDel(&onDelUserNotifier);
89 std::for_each(usersList.begin(), usersList.end(), std::bind1st(std::mem_fun(&AUTH_AO::UpdateUserAuthorization), this));
95 //-----------------------------------------------------------------------------
98 printfd(__FILE__, "AUTH_AO::Stop()\n");
102 users->DelNotifierUserAdd(&onAddUserNotifier);
103 users->DelNotifierUserDel(&onDelUserNotifier);
105 std::list<USER_PTR>::iterator users_iter;
106 users_iter = usersList.begin();
107 while (users_iter != usersList.end())
109 if ((*users_iter)->IsAuthorizedBy(this))
110 users->Unauthorize((*users_iter)->GetLogin(), this);
111 UnSetUserNotifiers(*users_iter);
117 //-----------------------------------------------------------------------------
118 void AUTH_AO::SetUserNotifiers(USER_PTR u)
120 // ---------- AlwaysOnline -------------------
121 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
122 CHG_AFTER_NOTIFIER<int> AfterChgAONotifier(*this, u);
124 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
125 AfterChgAONotifierList.push_front(AfterChgAONotifier);
127 u->GetProperty().alwaysOnline.AddBeforeNotifier(&BeforeChgAONotifierList.front());
128 u->GetProperty().alwaysOnline.AddAfterNotifier(&AfterChgAONotifierList.front());
129 // ---------- AlwaysOnline end ---------------
131 // ---------- IP -------------------
132 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier(*this, u);
133 CHG_AFTER_NOTIFIER<USER_IPS> AfterChgIPNotifier(*this, u);
135 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
136 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
138 u->GetProperty().ips.AddBeforeNotifier(&BeforeChgIPNotifierList.front());
139 u->GetProperty().ips.AddAfterNotifier(&AfterChgIPNotifierList.front());
140 // ---------- IP end ---------------
142 //-----------------------------------------------------------------------------
143 void AUTH_AO::UnSetUserNotifiers(USER_PTR u)
145 // --- AlwaysOnline ---
146 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<int> > IsContainsUserAOB;
147 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<int> > IsContainsUserAOA;
149 std::list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
150 std::list<CHG_AFTER_NOTIFIER<int> >::iterator aoAIter;
152 aoBIter = find_if(BeforeChgAONotifierList.begin(),
153 BeforeChgAONotifierList.end(),
154 bind2nd(IsContainsUserAOB, u));
156 if (aoBIter != BeforeChgAONotifierList.end())
158 aoBIter->GetUser()->GetProperty().alwaysOnline.DelBeforeNotifier(&(*aoBIter));
159 BeforeChgAONotifierList.erase(aoBIter);
162 aoAIter = find_if(AfterChgAONotifierList.begin(),
163 AfterChgAONotifierList.end(),
164 bind2nd(IsContainsUserAOA, u));
166 if (aoAIter != AfterChgAONotifierList.end())
168 aoAIter->GetUser()->GetProperty().alwaysOnline.DelAfterNotifier(&(*aoAIter));
169 AfterChgAONotifierList.erase(aoAIter);
171 // --- AlwaysOnline end ---
174 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
175 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> > IsContainsUserIPA;
177 std::list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
178 std::list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator ipAIter;
180 ipBIter = std::find_if(BeforeChgIPNotifierList.begin(),
181 BeforeChgIPNotifierList.end(),
182 bind2nd(IsContainsUserIPB, u));
184 if (ipBIter != BeforeChgIPNotifierList.end())
186 ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
187 BeforeChgIPNotifierList.erase(ipBIter);
190 ipAIter = find_if(AfterChgIPNotifierList.begin(),
191 AfterChgIPNotifierList.end(),
192 bind2nd(IsContainsUserIPA, u));
194 if (ipAIter != AfterChgIPNotifierList.end())
196 ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
197 AfterChgIPNotifierList.erase(ipAIter);
201 //-----------------------------------------------------------------------------
202 void AUTH_AO::GetUsers()
205 int h = users->OpenSearch();
206 assert(h && "USERS::OpenSearch is always correct");
208 while (!users->SearchNext(h, &u))
210 usersList.push_back(u);
214 users->CloseSearch(h);
216 //-----------------------------------------------------------------------------
217 void AUTH_AO::UpdateUserAuthorization(CONST_USER_PTR u) const
219 if (u->GetProperty().alwaysOnline)
221 USER_IPS ips = u->GetProperty().ips;
224 users->Authorize(u->GetLogin(), ips[0].ip, 0xFFffFFff, this);
228 //-----------------------------------------------------------------------------
229 void AUTH_AO::AddUser(USER_PTR u)
232 usersList.push_back(u);
233 UpdateUserAuthorization(u);
235 //-----------------------------------------------------------------------------
236 void AUTH_AO::DelUser(USER_PTR u)
238 if (u->IsAuthorizedBy(this))
239 users->Unauthorize(u->GetLogin(), this);
240 UnSetUserNotifiers(u);
243 //-----------------------------------------------------------------------------
244 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
246 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
249 //-----------------------------------------------------------------------------
250 template <typename varParamType>
251 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
253 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
254 if (user->IsAuthorizedBy(&auth))
255 auth.users->Unauthorize(user->GetLogin(), &auth);
257 //-----------------------------------------------------------------------------
258 template <typename varParamType>
259 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
261 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
262 auth.UpdateUserAuthorization(user);
264 //-----------------------------------------------------------------------------