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>
 
  24 #include "stg/users.h"
 
  25 #include "stg/user_property.h"
 
  26 #include "stg/common.h"
 
  28 #include <algorithm> // for_each
 
  29 #include <functional> // mem_fun_ref
 
  35 extern "C" STG::Plugin* GetPlugin()
 
  37     static AUTH_AO plugin;
 
  40 //-----------------------------------------------------------------------------
 
  41 //-----------------------------------------------------------------------------
 
  42 //-----------------------------------------------------------------------------
 
  43 std::string AUTH_AO::GetVersion() const
 
  45 return "Always Online authorizator v.1.0";
 
  47 //-----------------------------------------------------------------------------
 
  51       onAddUserNotifier(*this),
 
  52       onDelUserNotifier(*this),
 
  53       logger(STG::PluginLogger::get("auth_ao"))
 
  56 //-----------------------------------------------------------------------------
 
  59 printfd(__FILE__, "AUTH_AO::Start()\n");
 
  62 users->AddNotifierUserAdd(&onAddUserNotifier);
 
  63 users->AddNotifierUserDel(&onDelUserNotifier);
 
  65 std::for_each(userList.begin(), userList.end(), [this](auto user){ UpdateUserAuthorization(user); });
 
  71 //-----------------------------------------------------------------------------
 
  74 printfd(__FILE__, "AUTH_AO::Stop()\n");
 
  78 users->DelNotifierUserAdd(&onAddUserNotifier);
 
  79 users->DelNotifierUserDel(&onDelUserNotifier);
 
  81 auto it = userList.begin();
 
  82 while (it != userList.end())
 
  84     if ((*it)->IsAuthorizedBy(this))
 
  85         users->Unauthorize((*it)->GetLogin(), this);
 
  86     UnSetUserNotifiers(*it);
 
  92 //-----------------------------------------------------------------------------
 
  93 void AUTH_AO::SetUserNotifiers(UserPtr u)
 
  95 // ---------- AlwaysOnline -------------------
 
  96 CHG_BEFORE_NOTIFIER<int> BeforeChgAONotifier(*this, u);
 
  97 CHG_AFTER_NOTIFIER<int>  AfterChgAONotifier(*this, u);
 
  99 BeforeChgAONotifierList.push_front(BeforeChgAONotifier);
 
 100 AfterChgAONotifierList.push_front(AfterChgAONotifier);
 
 102 u->GetProperties().alwaysOnline.AddBeforeNotifier(&BeforeChgAONotifierList.front());
 
 103 u->GetProperties().alwaysOnline.AddAfterNotifier(&AfterChgAONotifierList.front());
 
 104 // ---------- AlwaysOnline end ---------------
 
 106 // ---------- IP -------------------
 
 107 CHG_BEFORE_NOTIFIER<STG::UserIPs> BeforeChgIPNotifier(*this, u);
 
 108 CHG_AFTER_NOTIFIER<STG::UserIPs>  AfterChgIPNotifier(*this, u);
 
 110 BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier);
 
 111 AfterChgIPNotifierList.push_front(AfterChgIPNotifier);
 
 113 u->GetProperties().ips.AddBeforeNotifier(&BeforeChgIPNotifierList.front());
 
 114 u->GetProperties().ips.AddAfterNotifier(&AfterChgIPNotifierList.front());
 
 115 // ---------- IP end ---------------
 
 117 //-----------------------------------------------------------------------------
 
 118 void AUTH_AO::UnSetUserNotifiers(UserPtr u)
 
 120 // ---      AlwaysOnline        ---
 
 121 auto aoBIter = find_if(BeforeChgAONotifierList.begin(),
 
 122                        BeforeChgAONotifierList.end(),
 
 123                        [u](auto notifier){ return notifier.GetUser() == u; });
 
 125 if (aoBIter != BeforeChgAONotifierList.end())
 
 127     aoBIter->GetUser()->GetProperties().alwaysOnline.DelBeforeNotifier(&(*aoBIter));
 
 128     BeforeChgAONotifierList.erase(aoBIter);
 
 131 auto aoAIter = find_if(AfterChgAONotifierList.begin(),
 
 132                        AfterChgAONotifierList.end(),
 
 133                        [u](auto notifier){ return notifier.GetUser() == u; });
 
 135 if (aoAIter != AfterChgAONotifierList.end())
 
 137     aoAIter->GetUser()->GetProperties().alwaysOnline.DelAfterNotifier(&(*aoAIter));
 
 138     AfterChgAONotifierList.erase(aoAIter);
 
 140 // ---      AlwaysOnline end    ---
 
 143 auto ipBIter = std::find_if(BeforeChgIPNotifierList.begin(),
 
 144                             BeforeChgIPNotifierList.end(),
 
 145                             [u](auto notifier){ return notifier.GetUser() == u; });
 
 147 if (ipBIter != BeforeChgIPNotifierList.end())
 
 149     ipBIter->GetUser()->GetProperties().ips.DelBeforeNotifier(&(*ipBIter));
 
 150     BeforeChgIPNotifierList.erase(ipBIter);
 
 153 auto ipAIter = find_if(AfterChgIPNotifierList.begin(),
 
 154                        AfterChgIPNotifierList.end(),
 
 155                        [u](auto notifier){ return notifier.GetUser() == u; });
 
 157 if (ipAIter != AfterChgIPNotifierList.end())
 
 159     ipAIter->GetUser()->GetProperties().ips.DelAfterNotifier(&(*ipAIter));
 
 160     AfterChgIPNotifierList.erase(ipAIter);
 
 164 //-----------------------------------------------------------------------------
 
 165 void AUTH_AO::GetUsers()
 
 168 int h = users->OpenSearch();
 
 169 assert(h && "USERS::OpenSearch is always correct");
 
 171 while (!users->SearchNext(h, &u))
 
 173     userList.push_back(u);
 
 177 users->CloseSearch(h);
 
 179 //-----------------------------------------------------------------------------
 
 180 void AUTH_AO::UpdateUserAuthorization(ConstUserPtr u) const
 
 182 if (u->GetProperties().alwaysOnline)
 
 184     auto ips = u->GetProperties().ips.get();
 
 187         users->Authorize(u->GetLogin(), ips[0].ip, 0xFFffFFff, this);
 
 191 //-----------------------------------------------------------------------------
 
 192 void AUTH_AO::AddUser(UserPtr u)
 
 195 userList.push_back(u);
 
 196 UpdateUserAuthorization(u);
 
 198 //-----------------------------------------------------------------------------
 
 199 void AUTH_AO::DelUser(UserPtr u)
 
 201 if (u->IsAuthorizedBy(this))
 
 202     users->Unauthorize(u->GetLogin(), this);
 
 203 UnSetUserNotifiers(u);
 
 204 userList.erase(std::remove(userList.begin(), userList.end(), u), userList.end());
 
 206 //-----------------------------------------------------------------------------
 
 207 int AUTH_AO::SendMessage(const STG::Message &, uint32_t) const
 
 209 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
 
 212 //-----------------------------------------------------------------------------
 
 213 template <typename varParamType>
 
 214 void CHG_BEFORE_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
 
 216 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user);
 
 217 if (user->IsAuthorizedBy(&auth))
 
 218     auth.users->Unauthorize(user->GetLogin(), &auth);
 
 220 //-----------------------------------------------------------------------------
 
 221 template <typename varParamType>
 
 222 void CHG_AFTER_NOTIFIER<varParamType>::Notify(const varParamType &, const varParamType &)
 
 224 //EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user);
 
 225 auth.UpdateUserAuthorization(user);
 
 227 //-----------------------------------------------------------------------------