]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/ao/ao.h
Start replacing notifiers with subscriptions.
[stg.git] / projects / stargazer / plugins / authorization / ao / ao.h
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 #pragma once
22
23 #include "stg/auth.h"
24 #include "stg/module_settings.h"
25 #include "stg/store.h"
26 #include "stg/notifer.h"
27 #include "stg/subscriptions.h"
28 #include "stg/user_ips.h"
29 #include "stg/user.h"
30 #include "stg/logger.h"
31
32 #include <string>
33 #include <vector>
34 #include <list>
35
36 #include <pthread.h>
37
38 namespace STG
39 {
40 struct Users;
41 }
42
43 class AUTH_AO;
44
45 using UserPtr = STG::User*;
46 using ConstUserPtr = const STG::User*;
47
48 template <typename T>
49 class CHG_BEFORE_NOTIFIER : public STG::PropertyNotifierBase<T> {
50 public:
51                 CHG_BEFORE_NOTIFIER(AUTH_AO & a, UserPtr u)
52                     : user(u), auth(a) {}
53                 CHG_BEFORE_NOTIFIER(const CHG_BEFORE_NOTIFIER<T> & rvalue)
54                     : user(rvalue.user), auth(rvalue.auth) {}
55     void        notify(const T & oldValue, const T & newValue) override;
56     UserPtr    GetUser() const { return user; }
57
58 private:
59     CHG_BEFORE_NOTIFIER<T> & operator=(const CHG_BEFORE_NOTIFIER<T> & rvalue);
60
61     UserPtr        user;
62     const AUTH_AO & auth;
63 };
64 //-----------------------------------------------------------------------------
65 template <typename T>
66 class CHG_AFTER_NOTIFIER : public STG::PropertyNotifierBase<T> {
67 public:
68                 CHG_AFTER_NOTIFIER(AUTH_AO & a, UserPtr u)
69                     : user(u), auth(a) {}
70                 CHG_AFTER_NOTIFIER(const CHG_AFTER_NOTIFIER<T> & rvalue)
71                     : user(rvalue.user), auth(rvalue.auth) {}
72     void        notify(const T & oldValue, const T & newValue) override;
73     UserPtr    GetUser() const { return user; }
74
75 private:
76     CHG_AFTER_NOTIFIER<T> & operator=(const CHG_AFTER_NOTIFIER<T> & rvalue);
77
78     UserPtr        user;
79     const AUTH_AO & auth;
80 };
81 //-----------------------------------------------------------------------------
82 class AUTH_AO : public STG::Auth {
83 public:
84     AUTH_AO();
85
86     void                SetUsers(STG::Users * u) override { users = u; }
87
88     int                 Start() override;
89     int                 Stop() override;
90     int                 Reload(const STG::ModuleSettings & /*ms*/) override { return 0; }
91     bool                IsRunning() override { return isRunning; }
92     void                SetSettings(const STG::ModuleSettings &) override {}
93     int                 ParseSettings() override { return 0; }
94     const std::string & GetStrError() const override { return errorStr; }
95     std::string         GetVersion() const override;
96     uint16_t            GetStartPosition() const override { return 30; }
97     uint16_t            GetStopPosition() const override { return 30; }
98
99     int                 SendMessage(const STG::Message & msg, uint32_t ip) const override;
100
101 private:
102     AUTH_AO(const AUTH_AO & rvalue);
103     AUTH_AO & operator=(const AUTH_AO & rvalue);
104
105     void                AddUser(UserPtr u);
106     void                DelUser(UserPtr u);
107
108     void                GetUsers();
109     void                SetUserNotifiers(UserPtr u);
110     void                UnSetUserNotifiers(UserPtr u);
111     void                UpdateUserAuthorization(ConstUserPtr u) const;
112
113     mutable std::string errorStr;
114     STG::Users *             users;
115     std::vector<UserPtr> userList;
116     bool                isRunning;
117     STG::ModuleSettings     settings;
118
119     std::list<CHG_BEFORE_NOTIFIER<int> >      BeforeChgAONotifierList;
120     std::list<CHG_AFTER_NOTIFIER<int> >       AfterChgAONotifierList;
121
122     std::list<CHG_BEFORE_NOTIFIER<STG::UserIPs> > BeforeChgIPNotifierList;
123     std::list<CHG_AFTER_NOTIFIER<STG::UserIPs> >  AfterChgIPNotifierList;
124
125     STG::ScopedConnection m_onAddUserConn;
126     STG::ScopedConnection m_onDelUserConn;
127
128     STG::PluginLogger logger;
129
130     friend class CHG_BEFORE_NOTIFIER<int>;
131     friend class CHG_AFTER_NOTIFIER<int>;
132     friend class CHG_BEFORE_NOTIFIER<STG::UserIPs>;
133     friend class CHG_AFTER_NOTIFIER<STG::UserIPs>;
134
135 };