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 std::string AUTH_AO::GetVersion() const
57 return "Always Online authorizator v.1.0";
59 //-----------------------------------------------------------------------------
63 onAddUserNotifier(*this),
64 onDelUserNotifier(*this),
65 logger(GetPluginLogger(GetStgLogger(), "auth_ao"))
68 //-----------------------------------------------------------------------------
71 printfd(__FILE__, "AUTH_AO::Start()\n");
74 users->AddNotifierUserAdd(&onAddUserNotifier);
75 users->AddNotifierUserDel(&onDelUserNotifier);
77 std::for_each(userList.begin(), userList.end(), [this](auto user){ UpdateUserAuthorization(user); });
83 //-----------------------------------------------------------------------------
86 printfd(__FILE__, "AUTH_AO::Stop()\n");
90 users->DelNotifierUserAdd(&onAddUserNotifier);
91 users->DelNotifierUserDel(&onDelUserNotifier);
93 auto it = userList.begin();
94 while (it != userList.end())
96 if ((*it)->IsAuthorizedBy(this))
97 users->Unauthorize((*it)->GetLogin(), this);
98 UnSetUserNotifiers(*it);
104 //-----------------------------------------------------------------------------
105 void AUTH_AO::SetUserNotifiers(USER_PTR u)
107 // ---------- AlwaysOnline -------------------
108 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
109 CHG_AFTER_NOTIFIER<int> AfterChgAONotifier(*this, u);
111 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
112 AfterChgAONotifierList.push_front(AfterChgAONotifier);
114 u->GetProperty().alwaysOnline.AddBeforeNotifier(&BeforeChgAONotifierList.front());
115 u->GetProperty().alwaysOnline.AddAfterNotifier(&AfterChgAONotifierList.front());
116 // ---------- AlwaysOnline end ---------------
118 // ---------- IP -------------------
119 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier(*this, u);
120 CHG_AFTER_NOTIFIER<USER_IPS> AfterChgIPNotifier(*this, u);
122 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
123 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
125 u->GetProperty().ips.AddBeforeNotifier(&BeforeChgIPNotifierList.front());
126 u->GetProperty().ips.AddAfterNotifier(&AfterChgIPNotifierList.front());
127 // ---------- IP end ---------------
129 //-----------------------------------------------------------------------------
130 void AUTH_AO::UnSetUserNotifiers(USER_PTR u)
132 // --- AlwaysOnline ---
133 auto aoBIter = find_if(BeforeChgAONotifierList.begin(),
134 BeforeChgAONotifierList.end(),
135 [u](auto notifier){ return notifier.GetUser() == u; });
137 if (aoBIter != BeforeChgAONotifierList.end())
139 aoBIter->GetUser()->GetProperty().alwaysOnline.DelBeforeNotifier(&(*aoBIter));
140 BeforeChgAONotifierList.erase(aoBIter);
143 auto aoAIter = find_if(AfterChgAONotifierList.begin(),
144 AfterChgAONotifierList.end(),
145 [u](auto notifier){ return notifier.GetUser() == u; });
147 if (aoAIter != AfterChgAONotifierList.end())
149 aoAIter->GetUser()->GetProperty().alwaysOnline.DelAfterNotifier(&(*aoAIter));
150 AfterChgAONotifierList.erase(aoAIter);
152 // --- AlwaysOnline end ---
155 auto ipBIter = std::find_if(BeforeChgIPNotifierList.begin(),
156 BeforeChgIPNotifierList.end(),
157 [u](auto notifier){ return notifier.GetUser() == u; });
159 if (ipBIter != BeforeChgIPNotifierList.end())
161 ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
162 BeforeChgIPNotifierList.erase(ipBIter);
165 auto ipAIter = find_if(AfterChgIPNotifierList.begin(),
166 AfterChgIPNotifierList.end(),
167 [u](auto notifier){ return notifier.GetUser() == u; });
169 if (ipAIter != AfterChgIPNotifierList.end())
171 ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
172 AfterChgIPNotifierList.erase(ipAIter);
176 //-----------------------------------------------------------------------------
177 void AUTH_AO::GetUsers()
180 int h = users->OpenSearch();
181 assert(h && "USERS::OpenSearch is always correct");
183 while (!users->SearchNext(h, &u))
185 userList.push_back(u);
189 users->CloseSearch(h);
191 //-----------------------------------------------------------------------------
192 void AUTH_AO::UpdateUserAuthorization(CONST_USER_PTR u) const
194 if (u->GetProperty().alwaysOnline)
196 USER_IPS ips = u->GetProperty().ips;
199 users->Authorize(u->GetLogin(), ips[0].ip, 0xFFffFFff, this);
203 //-----------------------------------------------------------------------------
204 void AUTH_AO::AddUser(USER_PTR u)
207 userList.push_back(u);
208 UpdateUserAuthorization(u);
210 //-----------------------------------------------------------------------------
211 void AUTH_AO::DelUser(USER_PTR u)
213 if (u->IsAuthorizedBy(this))
214 users->Unauthorize(u->GetLogin(), this);
215 UnSetUserNotifiers(u);
216 userList.erase(std::remove(userList.begin(), userList.end(), u), userList.end());
218 //-----------------------------------------------------------------------------
219 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
221 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
224 //-----------------------------------------------------------------------------
225 template <typename varParamType>
226 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
228 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
229 if (user->IsAuthorizedBy(&auth))
230 auth.users->Unauthorize(user->GetLogin(), &auth);
232 //-----------------------------------------------------------------------------
233 template <typename varParamType>
234 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
236 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
237 auth.UpdateUserAuthorization(user);
239 //-----------------------------------------------------------------------------