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