]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/ao/ao.cpp
Make explicit calls on notifications in AUTH_OA
[stg.git] / projects / stargazer / plugins / authorization / ao / ao.cpp
1 /*
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.
6  *
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.
11  *
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
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 /*
22 $Revision: 1.30 $
23 $Date: 2010/03/04 12:29:06 $
24 $Author: faust $
25 */
26
27 #include <unistd.h>
28
29 #include <csignal>
30 #include <algorithm> // for_each
31 #include <functional> // mem_fun_ref
32
33 #include "ao.h"
34 #include "user.h"
35 #include "users.h"
36 #include "user_property.h"
37 #include "common.h"
38 //#include "../../../eventloop.h"
39
40 class AO_CREATOR
41 {
42 private:
43     AUTH_AO * ao;
44
45 public:
46     AO_CREATOR()
47         : ao(new AUTH_AO())
48         {
49         };
50     ~AO_CREATOR()
51         {
52         delete ao;
53         };
54
55     AUTH_AO * GetPlugin()
56         {
57         return ao;
58         };
59 };
60 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
63 AO_CREATOR aoc;
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
67 template <typename varType>
68 class IS_CONTAINS_USER: public binary_function<varType, USER_PTR, bool>
69 {
70 public:
71     bool operator()(varType notifier, USER_PTR user) const
72         {
73         return notifier.GetUser() == user;
74         };
75 };
76 //-----------------------------------------------------------------------------
77 //-----------------------------------------------------------------------------
78 //-----------------------------------------------------------------------------
79 PLUGIN * GetPlugin()
80 {
81 return aoc.GetPlugin();
82 }
83 //-----------------------------------------------------------------------------
84 //-----------------------------------------------------------------------------
85 //-----------------------------------------------------------------------------
86 const string AUTH_AO::GetVersion() const
87 {
88 return "Always Online authorizator v.1.0";
89 }
90 //-----------------------------------------------------------------------------
91 AUTH_AO::AUTH_AO()
92     : users(NULL),
93       isRunning(false),
94       onAddUserNotifier(*this),
95       onDelUserNotifier(*this)
96 {
97 }
98 //-----------------------------------------------------------------------------
99 int AUTH_AO::Start()
100 {
101 printfd(__FILE__, "AUTH_AO::Start()\n");
102 GetUsers();
103
104 users->AddNotifierUserAdd(&onAddUserNotifier);
105 users->AddNotifierUserDel(&onDelUserNotifier);
106
107 std::for_each(usersList.begin(), usersList.end(), std::bind1st(std::mem_fun(&AUTH_AO::UpdateUserAuthorization), this));
108
109 isRunning = true;
110
111 return 0;
112 }
113 //-----------------------------------------------------------------------------
114 int AUTH_AO::Stop()
115 {
116 printfd(__FILE__, "AUTH_AO::Stop()\n");
117 if (!isRunning)
118     return 0;
119
120 users->DelNotifierUserAdd(&onAddUserNotifier);
121 users->DelNotifierUserDel(&onDelUserNotifier);
122
123 list<USER_PTR>::iterator users_iter;
124 users_iter = usersList.begin();
125 while (users_iter != usersList.end())
126     {
127     Unauthorize(*users_iter);
128     UnSetUserNotifiers(*users_iter);
129     ++users_iter;
130     }
131 isRunning = false;
132 return 0;
133 }
134 //-----------------------------------------------------------------------------
135 void AUTH_AO::SetUserNotifiers(USER_PTR u)
136 {
137 // ---------- AlwaysOnline -------------------
138 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
139 CHG_AFTER_NOTIFIER<int>  AfterChgAONotifier(*this, u);
140
141 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
142 AfterChgAONotifierList.push_front(AfterChgAONotifier);
143
144 u->GetProperty().alwaysOnline.AddBeforeNotifier(&BeforeChgAONotifierList.front());
145 u->GetProperty().alwaysOnline.AddAfterNotifier(&AfterChgAONotifierList.front());
146 // ---------- AlwaysOnline end ---------------
147
148 // ---------- IP -------------------
149 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier(*this, u);
150 CHG_AFTER_NOTIFIER<USER_IPS>  AfterChgIPNotifier(*this, u);
151
152 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
153 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
154
155 u->GetProperty().ips.AddBeforeNotifier(&BeforeChgIPNotifierList.front());
156 u->GetProperty().ips.AddAfterNotifier(&AfterChgIPNotifierList.front());
157 // ---------- IP end ---------------
158 }
159 //-----------------------------------------------------------------------------
160 void AUTH_AO::UnSetUserNotifiers(USER_PTR u)
161 {
162 // ---      AlwaysOnline        ---
163 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<int> > IsContainsUserAOB;
164 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<int> > IsContainsUserAOA;
165
166 list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
167 list<CHG_AFTER_NOTIFIER<int> >::iterator  aoAIter;
168
169 aoBIter = find_if(BeforeChgAONotifierList.begin(),
170                   BeforeChgAONotifierList.end(),
171                   bind2nd(IsContainsUserAOB, u));
172
173 if (aoBIter != BeforeChgAONotifierList.end())
174     {
175     aoBIter->GetUser()->GetProperty().alwaysOnline.DelBeforeNotifier(&(*aoBIter));
176     BeforeChgAONotifierList.erase(aoBIter);
177     }
178
179 aoAIter = find_if(AfterChgAONotifierList.begin(),
180                   AfterChgAONotifierList.end(),
181                   bind2nd(IsContainsUserAOA, u));
182
183 if (aoAIter != AfterChgAONotifierList.end())
184     {
185     aoAIter->GetUser()->GetProperty().alwaysOnline.DelAfterNotifier(&(*aoAIter));
186     AfterChgAONotifierList.erase(aoAIter);
187     }
188 // ---      AlwaysOnline end    ---
189
190 // ---          IP              ---
191 IS_CONTAINS_USER<CHG_BEFORE_NOTIFIER<USER_IPS> > IsContainsUserIPB;
192 IS_CONTAINS_USER<CHG_AFTER_NOTIFIER<USER_IPS> >  IsContainsUserIPA;
193
194 list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
195 list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator  ipAIter;
196
197 ipBIter = find_if(BeforeChgIPNotifierList.begin(),
198                   BeforeChgIPNotifierList.end(),
199                   bind2nd(IsContainsUserIPB, u));
200
201 if (ipBIter != BeforeChgIPNotifierList.end())
202     {
203     ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
204     BeforeChgIPNotifierList.erase(ipBIter);
205     }
206
207 ipAIter = find_if(AfterChgIPNotifierList.begin(),
208                   AfterChgIPNotifierList.end(),
209                   bind2nd(IsContainsUserIPA, u));
210
211 if (ipAIter != AfterChgIPNotifierList.end())
212     {
213     ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
214     AfterChgIPNotifierList.erase(ipAIter);
215     }
216 // ---          IP end          ---
217 }
218 //-----------------------------------------------------------------------------
219 void AUTH_AO::GetUsers()
220 {
221 USER_PTR u;
222 int h = users->OpenSearch();
223 if (!h)
224     {
225     printfd(__FILE__, "users->OpenSearch() error\n");
226     return;
227     }
228
229 while (!users->SearchNext(h, &u))
230     {
231     usersList.push_back(u);
232     SetUserNotifiers(u);
233     }
234
235 users->CloseSearch(h);
236 }
237 //-----------------------------------------------------------------------------
238 void AUTH_AO::Unauthorize(USER_PTR u) const
239 {
240 printfd(__FILE__, "AUTH_AO::Unauthorize - login: '%s'\n", u->GetLogin().c_str());
241 u->Unauthorize(this);
242 }
243 //-----------------------------------------------------------------------------
244 void AUTH_AO::UpdateUserAuthorization(USER_PTR u) const
245 {
246 printfd(__FILE__, "AUTH_AO::UpdateUserAuthorization - login: '%s'\n", u->GetLogin().c_str());
247 if (u->GetProperty().alwaysOnline)
248     {
249     USER_IPS ips = u->GetProperty().ips;
250     if (ips.OnlyOneIP())
251         {
252         if (u->Authorize(ips[0].ip, 0xFFffFFff, this) == 0)
253             {
254             }
255         }
256     }
257 }
258 //-----------------------------------------------------------------------------
259 void AUTH_AO::AddUser(USER_PTR u)
260 {
261 printfd(__FILE__, "AUTH_AO::AddUser - login: '%s'\n", u->GetLogin().c_str());
262 SetUserNotifiers(u);
263 usersList.push_back(u);
264 UpdateUserAuthorization(u);
265 }
266 //-----------------------------------------------------------------------------
267 void AUTH_AO::DelUser(USER_PTR u)
268 {
269 printfd(__FILE__, "AUTH_AO::DelUser - login: '%s'\n", u->GetLogin().c_str());
270 Unauthorize(u);
271 UnSetUserNotifiers(u);
272 usersList.remove(u);
273 }
274 //-----------------------------------------------------------------------------
275 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
276 {
277 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
278 return -1;
279 }
280 //-----------------------------------------------------------------------------
281 template <typename varParamType>
282 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
283 {
284 printfd(__FILE__, "CHG_BEFORE_NOTIFIER::Notify\n");
285 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
286 auth.Unauthorize(user);
287 }
288 //-----------------------------------------------------------------------------
289 template <typename varParamType>
290 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
291 {
292 printfd(__FILE__, "CHG_AFTER_NOTIFIER::Notify\n");
293 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
294 auth.UpdateUserAuthorization(user);
295 }
296 //-----------------------------------------------------------------------------