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(usersList.begin(), usersList.end(), [this](auto user){ UpdateUserAuthorization(user); });
 
  83 //-----------------------------------------------------------------------------
 
  86 printfd(__FILE__, "AUTH_AO::Stop()\n");
 
  90 users->DelNotifierUserAdd(&onAddUserNotifier);
 
  91 users->DelNotifierUserDel(&onDelUserNotifier);
 
  93 std::list<USER_PTR>::iterator users_iter;
 
  94 users_iter = usersList.begin();
 
  95 while (users_iter != usersList.end())
 
  97     if ((*users_iter)->IsAuthorizedBy(this))
 
  98         users->Unauthorize((*users_iter)->GetLogin(), this);
 
  99     UnSetUserNotifiers(*users_iter);
 
 105 //-----------------------------------------------------------------------------
 
 106 void AUTH_AO::SetUserNotifiers(USER_PTR u)
 
 108 // ---------- AlwaysOnline -------------------
 
 109 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
 
 110 CHG_AFTER_NOTIFIER<int>  AfterChgAONotifier(*this, u);
 
 112 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
 
 113 AfterChgAONotifierList.push_front(AfterChgAONotifier);
 
 115 u->GetProperty().alwaysOnline.AddBeforeNotifier(&BeforeChgAONotifierList.front());
 
 116 u->GetProperty().alwaysOnline.AddAfterNotifier(&AfterChgAONotifierList.front());
 
 117 // ---------- AlwaysOnline end ---------------
 
 119 // ---------- IP -------------------
 
 120 CHG_BEFORE_NOTIFIER<USER_IPS> BeforeChgIPNotifier(*this, u);
 
 121 CHG_AFTER_NOTIFIER<USER_IPS>  AfterChgIPNotifier(*this, u);
 
 123 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
 
 124 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
 
 126 u->GetProperty().ips.AddBeforeNotifier(&BeforeChgIPNotifierList.front());
 
 127 u->GetProperty().ips.AddAfterNotifier(&AfterChgIPNotifierList.front());
 
 128 // ---------- IP end ---------------
 
 130 //-----------------------------------------------------------------------------
 
 131 void AUTH_AO::UnSetUserNotifiers(USER_PTR u)
 
 133 // ---      AlwaysOnline        ---
 
 134 std::list<CHG_BEFORE_NOTIFIER<int> >::iterator aoBIter;
 
 135 std::list<CHG_AFTER_NOTIFIER<int> >::iterator  aoAIter;
 
 137 aoBIter = find_if(BeforeChgAONotifierList.begin(),
 
 138                   BeforeChgAONotifierList.end(),
 
 139                   [u](auto notifier){ return notifier.GetUser() == u; });
 
 141 if (aoBIter != BeforeChgAONotifierList.end())
 
 143     aoBIter->GetUser()->GetProperty().alwaysOnline.DelBeforeNotifier(&(*aoBIter));
 
 144     BeforeChgAONotifierList.erase(aoBIter);
 
 147 aoAIter = find_if(AfterChgAONotifierList.begin(),
 
 148                   AfterChgAONotifierList.end(),
 
 149                   [u](auto notifier){ return notifier.GetUser() == u; });
 
 151 if (aoAIter != AfterChgAONotifierList.end())
 
 153     aoAIter->GetUser()->GetProperty().alwaysOnline.DelAfterNotifier(&(*aoAIter));
 
 154     AfterChgAONotifierList.erase(aoAIter);
 
 156 // ---      AlwaysOnline end    ---
 
 159 std::list<CHG_BEFORE_NOTIFIER<USER_IPS> >::iterator ipBIter;
 
 160 std::list<CHG_AFTER_NOTIFIER<USER_IPS> >::iterator  ipAIter;
 
 162 ipBIter = std::find_if(BeforeChgIPNotifierList.begin(),
 
 163                        BeforeChgIPNotifierList.end(),
 
 164                        [u](auto notifier){ return notifier.GetUser() == u; });
 
 166 if (ipBIter != BeforeChgIPNotifierList.end())
 
 168     ipBIter->GetUser()->GetProperty().ips.DelBeforeNotifier(&(*ipBIter));
 
 169     BeforeChgIPNotifierList.erase(ipBIter);
 
 172 ipAIter = find_if(AfterChgIPNotifierList.begin(),
 
 173                   AfterChgIPNotifierList.end(),
 
 174                   [u](auto notifier){ return notifier.GetUser() == u; });
 
 176 if (ipAIter != AfterChgIPNotifierList.end())
 
 178     ipAIter->GetUser()->GetProperty().ips.DelAfterNotifier(&(*ipAIter));
 
 179     AfterChgIPNotifierList.erase(ipAIter);
 
 183 //-----------------------------------------------------------------------------
 
 184 void AUTH_AO::GetUsers()
 
 187 int h = users->OpenSearch();
 
 188 assert(h && "USERS::OpenSearch is always correct");
 
 190 while (!users->SearchNext(h, &u))
 
 192     usersList.push_back(u);
 
 196 users->CloseSearch(h);
 
 198 //-----------------------------------------------------------------------------
 
 199 void AUTH_AO::UpdateUserAuthorization(CONST_USER_PTR u) const
 
 201 if (u->GetProperty().alwaysOnline)
 
 203     USER_IPS ips = u->GetProperty().ips;
 
 206         users->Authorize(u->GetLogin(), ips[0].ip, 0xFFffFFff, this);
 
 210 //-----------------------------------------------------------------------------
 
 211 void AUTH_AO::AddUser(USER_PTR u)
 
 214 usersList.push_back(u);
 
 215 UpdateUserAuthorization(u);
 
 217 //-----------------------------------------------------------------------------
 
 218 void AUTH_AO::DelUser(USER_PTR u)
 
 220 if (u->IsAuthorizedBy(this))
 
 221     users->Unauthorize(u->GetLogin(), this);
 
 222 UnSetUserNotifiers(u);
 
 225 //-----------------------------------------------------------------------------
 
 226 int AUTH_AO::SendMessage(const STG_MSG &, uint32_t) const
 
 228 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
 
 231 //-----------------------------------------------------------------------------
 
 232 template <typename varParamType>
 
 233 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
 
 235 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
 
 236 if (user->IsAuthorizedBy(&auth))
 
 237     auth.users->Unauthorize(user->GetLogin(), &auth);
 
 239 //-----------------------------------------------------------------------------
 
 240 template <typename varParamType>
 
 241 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
 
 243 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
 
 244 auth.UpdateUserAuthorization(user);
 
 246 //-----------------------------------------------------------------------------