Add auto-auth requirement to all admin pages
[cascardo/ipsilon.git] / ipsilon / admin / common.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2014  Simo Sorce <simo@redhat.com>
4 #
5 # see file 'COPYING' for use and warranty information
6 #
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 import cherrypy
21 from ipsilon.util.page import Page
22 from ipsilon.util.page import admin_protect
23
24
25 class AdminPage(Page):
26
27     def __init__(self, *args, **kwargs):
28         super(AdminPage, self).__init__(*args, **kwargs)
29         self.default_headers.update({
30             'Cache-Control': 'no-cache, must-revalidate',
31             'Pragma': 'no-cache',
32             'Expires': 'Thu, 01 Dec 1994 16:00:00 GMT',
33         })
34         self.auth_protect = True
35
36
37 class AdminPluginPage(AdminPage):
38
39     def __init__(self, obj, site, parent):
40         super(AdminPluginPage, self).__init__(site, form=True)
41         self._obj = obj
42         self.title = '%s plugin' % obj.name
43         self.url = '%s/%s' % (parent.url, obj.name)
44         self.facility = parent.facility
45         self.menu = [parent]
46         self.back = parent.url
47
48         # Get the defaults
49         self.plugin_config = obj.get_config_desc()
50         if not self.plugin_config:
51             self.plugin_config = dict()
52
53         # Now overlay the actual config
54         for option in self.plugin_config:
55             self.plugin_config[option][2] = obj.get_config_value(option)
56
57         self.options_order = []
58         if hasattr(obj, 'conf_opt_order'):
59             self.options_order = obj.conf_opt_order
60
61         # append any undefined options
62         add = []
63         for k in self.plugin_config.keys():
64             if k not in self.options_order:
65                 add.append(k)
66         if len(add):
67             add.sort()
68             for k in add:
69                 self.options_order.append(k)
70
71     @admin_protect
72     def GET(self, *args, **kwargs):
73         return self._template('admin/plugin_config.html', title=self.title,
74                               name='admin_%s_%s_form' % (self.facility,
75                                                          self._obj.name),
76                               menu=self.menu, action=self.url, back=self.back,
77                               options_order=self.options_order,
78                               options=self.plugin_config)
79
80     @admin_protect
81     def POST(self, *args, **kwargs):
82
83         message = "Nothing was modified."
84         message_type = "info"
85         new_values = dict()
86
87         for key, value in kwargs.iteritems():
88             if key in self.plugin_config:
89                 if value != self.plugin_config[key][2]:
90                     cherrypy.log.error("Storing [%s]: %s = %s" %
91                                        (self._obj.name, key, value))
92                     new_values[key] = value
93
94         if len(new_values) != 0:
95             # First we try to save in the database
96             try:
97                 self._obj.save_plugin_config(self.facility, new_values)
98                 message = "New configuration saved."
99                 message_type = "success"
100             except Exception:  # pylint: disable=broad-except
101                 message = "Failed to save data!"
102                 message_type = "error"
103
104             # And only if it succeeds we change the live object
105             for name, value in new_values.items():
106                 self._obj.set_config_value(name, value)
107                 self.plugin_config[name][2] = value
108
109         return self._template('admin/plugin_config.html', title=self.title,
110                               message=message,
111                               message_type=message_type,
112                               name='admin_%s_%s_form' % (self.facility,
113                                                          self._obj.name),
114                               menu=self.menu, action=self.url,
115                               options=self.plugin_config)
116
117
118 class Admin(AdminPage):
119
120     def __init__(self, site, mount):
121         super(Admin, self).__init__(site)
122         self.url = '%s/%s' % (self.basepath, mount)
123         self.menu = []
124
125     def root(self, *args, **kwargs):
126         return self._template('admin/index.html',
127                               title='Configuration',
128                               menu=self.menu)
129
130     def add_subtree(self, name, page):
131         self.__dict__[name] = page
132         self.menu.append(page)
133
134     def del_subtree(self, name):
135         self.menu.remove(self.__dict__[name])
136         del self.__dict__[name]