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
37 extern "C" STG::Plugin* GetPlugin()
39 static AUTH_AO plugin;
42 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 std::string AUTH_AO::GetVersion() const
47 return "Always Online authorizator v.1.0";
49 //-----------------------------------------------------------------------------
53 logger(PluginLogger::get("auth_ao"))
56 //-----------------------------------------------------------------------------
59 printfd(__FILE__, "AUTH_AO::Start()\n");
62 m_onAddUserConn = users->onAdd([this](auto user){ AddUser(user); });
63 m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); });
65 std::for_each(userList.begin(), userList.end(), [this](auto user){ UpdateUserAuthorization(user); });
71 //-----------------------------------------------------------------------------
74 printfd(__FILE__, "AUTH_AO::Stop()\n");
78 m_onAddUserConn.disconnect();
79 m_onDelUserConn.disconnect();
86 //-----------------------------------------------------------------------------
87 void AUTH_AO::SetUserNotifiers(UserPtr u)
91 u->GetProperties().alwaysOnline.beforeChange([this, u](auto, auto){ Unauthorize(u); }),
92 u->GetProperties().alwaysOnline.afterChange([this, u](auto, auto){ UpdateUserAuthorization(u); }),
93 u->GetProperties().ips.beforeChange([this, u](const auto&, const auto&){ Unauthorize(u); }),
94 u->GetProperties().ips.afterChange([this, u](const auto&, const auto&){ UpdateUserAuthorization(u); })
97 //-----------------------------------------------------------------------------
98 void AUTH_AO::UnSetUserNotifiers(UserPtr u)
100 m_conns.erase(std::remove_if(m_conns.begin(), m_conns.end(),
101 [u](const auto& c){ return std::get<0>(c) == u->GetID(); }),
104 //-----------------------------------------------------------------------------
105 void AUTH_AO::GetUsers()
108 int h = users->OpenSearch();
109 assert(h && "USERS::OpenSearch is always correct");
111 while (!users->SearchNext(h, &u))
113 userList.push_back(u);
117 users->CloseSearch(h);
119 //-----------------------------------------------------------------------------
120 void AUTH_AO::UpdateUserAuthorization(ConstUserPtr u) const
122 if (u->GetProperties().alwaysOnline)
124 auto ips = u->GetProperties().ips.get();
127 users->Authorize(u->GetLogin(), ips[0].ip, 0xFFffFFff, this);
131 //-----------------------------------------------------------------------------
132 void AUTH_AO::AddUser(UserPtr u)
135 userList.push_back(u);
136 UpdateUserAuthorization(u);
138 //-----------------------------------------------------------------------------
139 void AUTH_AO::DelUser(UserPtr u)
141 if (u->IsAuthorizedBy(this))
142 users->Unauthorize(u->GetLogin(), this);
143 UnSetUserNotifiers(u);
144 userList.erase(std::remove(userList.begin(), userList.end(), u), userList.end());
146 //-----------------------------------------------------------------------------
147 int AUTH_AO::SendMessage(const Message &, uint32_t) const
149 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
152 //-----------------------------------------------------------------------------
153 void AUTH_AO::Unauthorize(UserPtr user)
155 if (user->IsAuthorizedBy(this))
156 users->Unauthorize(user->GetLogin(), this);