Use new Log class everywhere
[cascardo/ipsilon.git] / ipsilon / util / page.py
1 #!/usr/bin/python
2 #
3 # Copyright (C) 2013  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 from ipsilon.util.log import Log
21 from ipsilon.util.user import UserSession
22 from urllib import unquote
23 import cherrypy
24
25
26 def admin_protect(fn):
27
28     def check(*args, **kwargs):
29         if UserSession().get_user().is_admin:
30             return fn(*args, **kwargs)
31
32         raise cherrypy.HTTPError(403)
33
34     return check
35
36
37 class Page(Log):
38     def __init__(self, site, form=False):
39         if 'template_env' not in site:
40             raise ValueError('Missing template environment')
41         self._site = site
42         self.basepath = cherrypy.config.get('base.mount', "")
43         self.user = None
44         self._is_form_page = form
45
46     def _compare_urls(self, url1, url2):
47         u1 = unquote(url1)
48         u2 = unquote(url2)
49         if u1 == u2:
50             return True
51         return False
52
53     def __call__(self, *args, **kwargs):
54         # pylint: disable=star-args
55         self.user = UserSession().get_user()
56
57         if len(args) > 0:
58             op = getattr(self, args[0], None)
59             if callable(op) and getattr(self, args[0]+'.exposed', None):
60                 return op(*args[1:], **kwargs)
61         else:
62             if self._is_form_page:
63                 self._debug("method: %s" % cherrypy.request.method)
64                 op = getattr(self, cherrypy.request.method, None)
65                 if callable(op):
66                     # Basic CSRF protection
67                     if cherrypy.request.method != 'GET':
68                         url = cherrypy.url(relative=False)
69                         if 'referer' not in cherrypy.request.headers:
70                             self._debug("Missing referer in %s request to %s"
71                                         % (cherrypy.request.method, url))
72                             raise cherrypy.HTTPError(403)
73                         referer = cherrypy.request.headers['referer']
74                         if not self._compare_urls(referer, url):
75                             self._debug("Wrong referer %s in request to %s"
76                                         % (referer, url))
77                             raise cherrypy.HTTPError(403)
78                     return op(*args, **kwargs)
79             else:
80                 op = getattr(self, 'root', None)
81                 if callable(op):
82                     return op(*args, **kwargs)
83
84         return self.default(*args, **kwargs)
85
86     def _template_model(self):
87         model = dict()
88         model['basepath'] = self.basepath
89         model['title'] = 'IPSILON'
90         model['user'] = self.user
91         return model
92
93     def _template(self, *args, **kwargs):
94         # pylint: disable=star-args
95         t = self._site['template_env'].get_template(args[0])
96         m = self._template_model()
97         m.update(kwargs)
98         return t.render(**m)
99
100     def default(self, *args, **kwargs):
101         raise cherrypy.HTTPError(404)
102
103     def add_subtree(self, name, page):
104         self.__dict__[name] = page
105
106     def del_subtree(self, name):
107         del self.__dict__[name]
108
109     exposed = True