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