X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/bc6cac0e474dfe2feb4983aef98f99e23a98ffc4..refs/heads/master:/projects/stargazer/plugins/authorization/ao/ao.cpp diff --git a/projects/stargazer/plugins/authorization/ao/ao.cpp b/projects/stargazer/plugins/authorization/ao/ao.cpp index 78e1c955..cb9a804d 100644 --- a/projects/stargazer/plugins/authorization/ao/ao.cpp +++ b/projects/stargazer/plugins/authorization/ao/ao.cpp @@ -32,6 +32,8 @@ #include +using STG::AUTH_AO; + extern "C" STG::Plugin* GetPlugin() { static AUTH_AO plugin; @@ -48,9 +50,7 @@ return "Always Online authorizator v.1.0"; AUTH_AO::AUTH_AO() : users(NULL), isRunning(false), - onAddUserNotifier(*this), - onDelUserNotifier(*this), - logger(STG::PluginLogger::get("auth_ao")) + logger(PluginLogger::get("auth_ao")) { } //----------------------------------------------------------------------------- @@ -59,8 +59,8 @@ int AUTH_AO::Start() printfd(__FILE__, "AUTH_AO::Start()\n"); GetUsers(); -users->AddNotifierUserAdd(&onAddUserNotifier); -users->AddNotifierUserDel(&onDelUserNotifier); +m_onAddUserConn = users->onAdd([this](auto user){ AddUser(user); }); +m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); }); std::for_each(userList.begin(), userList.end(), [this](auto user){ UpdateUserAuthorization(user); }); @@ -75,91 +75,31 @@ printfd(__FILE__, "AUTH_AO::Stop()\n"); if (!isRunning) return 0; -users->DelNotifierUserAdd(&onAddUserNotifier); -users->DelNotifierUserDel(&onDelUserNotifier); +m_onAddUserConn.disconnect(); +m_onDelUserConn.disconnect(); + +m_conns.clear(); -auto it = userList.begin(); -while (it != userList.end()) - { - if ((*it)->IsAuthorizedBy(this)) - users->Unauthorize((*it)->GetLogin(), this); - UnSetUserNotifiers(*it); - ++it; - } isRunning = false; return 0; } //----------------------------------------------------------------------------- void AUTH_AO::SetUserNotifiers(UserPtr u) { -// ---------- AlwaysOnline ------------------- -CHG_BEFORE_NOTIFIER BeforeChgAONotifier(*this, u); -CHG_AFTER_NOTIFIER AfterChgAONotifier(*this, u); - -BeforeChgAONotifierList.push_front(BeforeChgAONotifier); -AfterChgAONotifierList.push_front(AfterChgAONotifier); - -u->GetProperties().alwaysOnline.AddBeforeNotifier(&BeforeChgAONotifierList.front()); -u->GetProperties().alwaysOnline.AddAfterNotifier(&AfterChgAONotifierList.front()); -// ---------- AlwaysOnline end --------------- - -// ---------- IP ------------------- -CHG_BEFORE_NOTIFIER BeforeChgIPNotifier(*this, u); -CHG_AFTER_NOTIFIER AfterChgIPNotifier(*this, u); - -BeforeChgIPNotifierList.push_front(BeforeChgIPNotifier); -AfterChgIPNotifierList.push_front(AfterChgIPNotifier); - -u->GetProperties().ips.AddBeforeNotifier(&BeforeChgIPNotifierList.front()); -u->GetProperties().ips.AddAfterNotifier(&AfterChgIPNotifierList.front()); -// ---------- IP end --------------- + m_conns.emplace_back( + u->GetID(), + u->GetProperties().alwaysOnline.beforeChange([this, u](auto, auto){ Unauthorize(u); }), + u->GetProperties().alwaysOnline.afterChange([this, u](auto, auto){ UpdateUserAuthorization(u); }), + u->GetProperties().ips.beforeChange([this, u](const auto&, const auto&){ Unauthorize(u); }), + u->GetProperties().ips.afterChange([this, u](const auto&, const auto&){ UpdateUserAuthorization(u); }) + ); } //----------------------------------------------------------------------------- void AUTH_AO::UnSetUserNotifiers(UserPtr u) { -// --- AlwaysOnline --- -auto aoBIter = find_if(BeforeChgAONotifierList.begin(), - BeforeChgAONotifierList.end(), - [u](auto notifier){ return notifier.GetUser() == u; }); - -if (aoBIter != BeforeChgAONotifierList.end()) - { - aoBIter->GetUser()->GetProperties().alwaysOnline.DelBeforeNotifier(&(*aoBIter)); - BeforeChgAONotifierList.erase(aoBIter); - } - -auto aoAIter = find_if(AfterChgAONotifierList.begin(), - AfterChgAONotifierList.end(), - [u](auto notifier){ return notifier.GetUser() == u; }); - -if (aoAIter != AfterChgAONotifierList.end()) - { - aoAIter->GetUser()->GetProperties().alwaysOnline.DelAfterNotifier(&(*aoAIter)); - AfterChgAONotifierList.erase(aoAIter); - } -// --- AlwaysOnline end --- - -// --- IP --- -auto ipBIter = std::find_if(BeforeChgIPNotifierList.begin(), - BeforeChgIPNotifierList.end(), - [u](auto notifier){ return notifier.GetUser() == u; }); - -if (ipBIter != BeforeChgIPNotifierList.end()) - { - ipBIter->GetUser()->GetProperties().ips.DelBeforeNotifier(&(*ipBIter)); - BeforeChgIPNotifierList.erase(ipBIter); - } - -auto ipAIter = find_if(AfterChgIPNotifierList.begin(), - AfterChgIPNotifierList.end(), - [u](auto notifier){ return notifier.GetUser() == u; }); - -if (ipAIter != AfterChgIPNotifierList.end()) - { - ipAIter->GetUser()->GetProperties().ips.DelAfterNotifier(&(*ipAIter)); - AfterChgIPNotifierList.erase(ipAIter); - } -// --- IP end --- + m_conns.erase(std::remove_if(m_conns.begin(), m_conns.end(), + [u](const auto& c){ return std::get<0>(c) == u->GetID(); }), + m_conns.end()); } //----------------------------------------------------------------------------- void AUTH_AO::GetUsers() @@ -204,24 +144,14 @@ UnSetUserNotifiers(u); userList.erase(std::remove(userList.begin(), userList.end(), u), userList.end()); } //----------------------------------------------------------------------------- -int AUTH_AO::SendMessage(const STG::Message &, uint32_t) const +int AUTH_AO::SendMessage(const Message &, uint32_t) const { errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages"; return -1; } //----------------------------------------------------------------------------- -template -void CHG_BEFORE_NOTIFIER::notify(const varParamType &, const varParamType &) +void AUTH_AO::Unauthorize(UserPtr user) { -//EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::Unauthorize, user); -if (user->IsAuthorizedBy(&auth)) - auth.users->Unauthorize(user->GetLogin(), &auth); + if (user->IsAuthorizedBy(this)) + users->Unauthorize(user->GetLogin(), this); } -//----------------------------------------------------------------------------- -template -void CHG_AFTER_NOTIFIER::notify(const varParamType &, const varParamType &) -{ -//EVENT_LOOP_SINGLETON::GetInstance().Enqueue(auth, &AUTH_AO::UpdateUserAuthorization, user); -auth.UpdateUserAuthorization(user); -} -//-----------------------------------------------------------------------------