]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/ao/ao.cpp
Complete replacement notifiers with subscriptions.
[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 #include "ao.h"
22
23 #include "stg/user.h"
24 #include "stg/users.h"
25 #include "stg/user_property.h"
26 #include "stg/common.h"
27
28 #include <algorithm> // for_each
29 #include <functional> // mem_fun_ref
30 #include <csignal>
31 #include <cassert>
32
33 #include <unistd.h>
34
35 using STG::AUTH_AO;
36
37 extern "C" STG::Plugin* GetPlugin()
38 {
39     static AUTH_AO plugin;
40     return &plugin;
41 }
42 //-----------------------------------------------------------------------------
43 //-----------------------------------------------------------------------------
44 //-----------------------------------------------------------------------------
45 std::string AUTH_AO::GetVersion() const
46 {
47 return "Always Online authorizator v.1.0";
48 }
49 //-----------------------------------------------------------------------------
50 AUTH_AO::AUTH_AO()
51     : users(NULL),
52       isRunning(false),
53       logger(PluginLogger::get("auth_ao"))
54 {
55 }
56 //-----------------------------------------------------------------------------
57 int AUTH_AO::Start()
58 {
59 printfd(__FILE__, "AUTH_AO::Start()\n");
60 GetUsers();
61
62 m_onAddUserConn = users->onAdd([this](auto user){ AddUser(user); });
63 m_onDelUserConn = users->onDel([this](auto user){ DelUser(user); });
64
65 std::for_each(userList.begin(), userList.end(), [this](auto user){ UpdateUserAuthorization(user); });
66
67 isRunning = true;
68
69 return 0;
70 }
71 //-----------------------------------------------------------------------------
72 int AUTH_AO::Stop()
73 {
74 printfd(__FILE__, "AUTH_AO::Stop()\n");
75 if (!isRunning)
76     return 0;
77
78 m_onAddUserConn.disconnect();
79 m_onDelUserConn.disconnect();
80
81 m_conns.clear();
82
83 isRunning = false;
84 return 0;
85 }
86 //-----------------------------------------------------------------------------
87 void AUTH_AO::SetUserNotifiers(UserPtr u)
88 {
89     m_conns.emplace_back(
90         u->GetID(),
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); })
95     );
96 }
97 //-----------------------------------------------------------------------------
98 void AUTH_AO::UnSetUserNotifiers(UserPtr u)
99 {
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(); }),
102                   m_conns.end());
103 }
104 //-----------------------------------------------------------------------------
105 void AUTH_AO::GetUsers()
106 {
107 UserPtr u;
108 int h = users->OpenSearch();
109 assert(h && "USERS::OpenSearch is always correct");
110
111 while (!users->SearchNext(h, &u))
112     {
113     userList.push_back(u);
114     SetUserNotifiers(u);
115     }
116
117 users->CloseSearch(h);
118 }
119 //-----------------------------------------------------------------------------
120 void AUTH_AO::UpdateUserAuthorization(ConstUserPtr u) const
121 {
122 if (u->GetProperties().alwaysOnline)
123     {
124     auto ips = u->GetProperties().ips.get();
125     if (ips.onlyOneIP())
126         {
127         users->Authorize(u->GetLogin(), ips[0].ip, 0xFFffFFff, this);
128         }
129     }
130 }
131 //-----------------------------------------------------------------------------
132 void AUTH_AO::AddUser(UserPtr u)
133 {
134 SetUserNotifiers(u);
135 userList.push_back(u);
136 UpdateUserAuthorization(u);
137 }
138 //-----------------------------------------------------------------------------
139 void AUTH_AO::DelUser(UserPtr u)
140 {
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());
145 }
146 //-----------------------------------------------------------------------------
147 int AUTH_AO::SendMessage(const Message &, uint32_t) const
148 {
149 errorStr = "Authorization modele \'AlwaysOnline\' does not support sending messages";
150 return -1;
151 }
152 //-----------------------------------------------------------------------------
153 void AUTH_AO::Unauthorize(UserPtr user)
154 {
155     if (user->IsAuthorizedBy(this))
156         users->Unauthorize(user->GetLogin(), this);
157 }