]> git.stg.codes - stg.git/blob - projects/stargazer/plugins/authorization/ao/ao.h
9ed7ffcf2226b3a6cd76cc1aa773b1602eb16770
[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 /*
22  $Revision: 1.21 $
23  $Date: 2010/09/10 06:38:26 $
24  $Author: faust $
25 */
26
27 #ifndef AO_H
28 #define AO_H
29
30 #include <pthread.h>
31
32 #include <string>
33 #include <list>
34
35 #include "stg/auth.h"
36 #include "stg/store.h"
37 #include "stg/notifer.h"
38 #include "stg/user_ips.h"
39 #include "stg/user.h"
40 #include "stg/logger.h"
41
42 extern "C" PLUGIN * GetPlugin();
43
44 class AUTH_AO;
45 class USERS;
46 //-----------------------------------------------------------------------------
47 template <typename T>
48 class CHG_BEFORE_NOTIFIER : public PROPERTY_NOTIFIER_BASE<T> {
49 public:
50                 CHG_BEFORE_NOTIFIER(AUTH_AO & a, USER_PTR u)
51                     : PROPERTY_NOTIFIER_BASE<T>(), user(u), auth(a) {}
52                 CHG_BEFORE_NOTIFIER(const CHG_BEFORE_NOTIFIER<T> & rvalue)
53                     : PROPERTY_NOTIFIER_BASE<T>(),
54                       user(rvalue.user), auth(rvalue.auth) {}
55     void        Notify(const T & oldValue, const T & newValue);
56     USER_PTR    GetUser() const { return user; }
57
58 private:
59     CHG_BEFORE_NOTIFIER<T> & operator=(const CHG_BEFORE_NOTIFIER<T> & rvalue);
60
61     USER_PTR        user;
62     const AUTH_AO & auth;
63 };
64 //-----------------------------------------------------------------------------
65 template <typename T>
66 class CHG_AFTER_NOTIFIER : public PROPERTY_NOTIFIER_BASE<T> {
67 public:
68                 CHG_AFTER_NOTIFIER(AUTH_AO & a, USER_PTR u)
69                     : PROPERTY_NOTIFIER_BASE<T>(), user(u), auth(a) {}
70                 CHG_AFTER_NOTIFIER(const CHG_AFTER_NOTIFIER<T> & rvalue)
71                     : PROPERTY_NOTIFIER_BASE<T>(),
72                       user(rvalue.user), auth(rvalue.auth) {}
73     void        Notify(const T & oldValue, const T & newValue);
74     USER_PTR    GetUser() const { return user; }
75
76 private:
77     CHG_AFTER_NOTIFIER<T> & operator=(const CHG_AFTER_NOTIFIER<T> & rvalue);
78
79     USER_PTR        user;
80     const AUTH_AO & auth;
81 };
82 //-----------------------------------------------------------------------------
83 class AUTH_AO : public AUTH {
84 public:
85     AUTH_AO();
86     virtual ~AUTH_AO(){}
87
88     void                SetUsers(USERS * u) { users = u; }
89
90     int                 Start();
91     int                 Stop();
92     int                 Reload(const MODULE_SETTINGS & /*ms*/) { return 0; }
93     bool                IsRunning() { return isRunning; }
94     void                SetSettings(const MODULE_SETTINGS &) {}
95     int                 ParseSettings() { return 0; }
96     const std::string & GetStrError() const { return errorStr; }
97     std::string         GetVersion() const;
98     uint16_t            GetStartPosition() const { return 30; }
99     uint16_t            GetStopPosition() const { return 30; }
100
101     void                AddUser(USER_PTR u);
102     void                DelUser(USER_PTR u);
103
104     int                 SendMessage(const STG_MSG & msg, uint32_t ip) const;
105
106 private:
107     AUTH_AO(const AUTH_AO & rvalue);
108     AUTH_AO & operator=(const AUTH_AO & rvalue);
109
110     void                GetUsers();
111     void                SetUserNotifiers(USER_PTR u);
112     void                UnSetUserNotifiers(USER_PTR u);
113     void                UpdateUserAuthorization(CONST_USER_PTR u) const;
114
115     mutable std::string errorStr;
116     USERS *             users;
117     std::list<USER_PTR> usersList;
118     bool                isRunning;
119     MODULE_SETTINGS     settings;
120
121     std::list<CHG_BEFORE_NOTIFIER<int> >      BeforeChgAONotifierList;
122     std::list<CHG_AFTER_NOTIFIER<int> >       AfterChgAONotifierList;
123
124     std::list<CHG_BEFORE_NOTIFIER<USER_IPS> > BeforeChgIPNotifierList;
125     std::list<CHG_AFTER_NOTIFIER<USER_IPS> >  AfterChgIPNotifierList;
126
127     class ADD_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
128     public:
129         ADD_USER_NONIFIER(AUTH_AO & a) : auth(a) {}
130         virtual ~ADD_USER_NONIFIER() {}
131         void Notify(const USER_PTR & user) { auth.AddUser(user); }
132
133     private:
134         ADD_USER_NONIFIER(const ADD_USER_NONIFIER & rvalue);
135         ADD_USER_NONIFIER & operator=(const ADD_USER_NONIFIER & rvalue);
136
137         AUTH_AO & auth;
138     } onAddUserNotifier;
139
140     class DEL_USER_NONIFIER: public NOTIFIER_BASE<USER_PTR> {
141     public:
142         DEL_USER_NONIFIER(AUTH_AO & a) : auth(a) {}
143         virtual ~DEL_USER_NONIFIER() {}
144         void Notify(const USER_PTR & user) { auth.DelUser(user); }
145
146     private:
147         DEL_USER_NONIFIER(const DEL_USER_NONIFIER & rvalue);
148         DEL_USER_NONIFIER & operator=(const DEL_USER_NONIFIER & rvalue);
149
150         AUTH_AO & auth;
151     } onDelUserNotifier;
152     PLUGIN_LOGGER logger;
153
154     friend class CHG_BEFORE_NOTIFIER<int>;
155     friend class CHG_AFTER_NOTIFIER<int>;
156     friend class CHG_BEFORE_NOTIFIER<USER_IPS>;
157     friend class CHG_AFTER_NOTIFIER<USER_IPS>;
158
159 };
160 //-----------------------------------------------------------------------------
161
162 #endif