Automatically build configuration page menu
[cascardo/ipsilon.git] / ipsilon / login / common.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.page import Page
21 from ipsilon.util.user import UserSession
22 from ipsilon.util.plugin import PluginLoader, PluginObject
23 from ipsilon.util.plugin import PluginInstaller
24 import cherrypy
25
26
27 class LoginManagerBase(PluginObject):
28
29     def __init__(self):
30         super(LoginManagerBase, self).__init__()
31         self.path = '/'
32         self.next_login = None
33
34     def redirect_to_path(self, path):
35         base = cherrypy.config.get('base.mount', "")
36         raise cherrypy.HTTPRedirect('%s/login/%s' % (base, path))
37
38     def auth_successful(self, username, userdata=None):
39         # save ref before calling UserSession login() as it
40         # may regenerate the session
41         session = UserSession()
42         ref = session.get_data('login', 'Return')
43         if not ref:
44             ref = cherrypy.config.get('base.mount', "") + '/'
45
46         session.login(username)
47
48         # Save additional data provided by the login manager
49         if userdata:
50             for key in userdata:
51                 session.save_data('user', key, userdata[key])
52
53         raise cherrypy.HTTPRedirect(ref)
54
55     def auth_failed(self):
56         # try with next module
57         if self.next_login:
58             return self.redirect_to_path(self.next_login.path)
59
60         # return to the caller if any
61         session = UserSession()
62         ref = session.get_data('login', 'Return')
63
64         # otherwise destroy session and return error
65         if not ref:
66             session.logout(None)
67             raise cherrypy.HTTPError(401)
68
69         raise cherrypy.HTTPRedirect(ref)
70
71
72 class LoginPageBase(Page):
73
74     def __init__(self, site, mgr):
75         super(LoginPageBase, self).__init__(site)
76         self.lm = mgr
77
78     def root(self, *args, **kwargs):
79         raise cherrypy.HTTPError(500)
80
81
82 FACILITY = 'login_config'
83
84
85 class Login(Page):
86
87     def __init__(self, *args, **kwargs):
88         super(Login, self).__init__(*args, **kwargs)
89         self.first_login = None
90
91         loader = PluginLoader(Login, FACILITY, 'LoginManager')
92         self._site[FACILITY] = loader.get_plugin_data()
93         plugins = self._site[FACILITY]
94
95         available = plugins['available'].keys()
96         self._debug('Available login managers: %s' % str(available))
97
98         prev_obj = None
99         for item in plugins['whitelist']:
100             self._debug('Login plugin in whitelist: %s' % item)
101             if item not in plugins['available']:
102                 continue
103             self._debug('Login plugin enabled: %s' % item)
104             plugins['enabled'].append(item)
105             obj = plugins['available'][item]
106             if prev_obj:
107                 prev_obj.next_login = obj
108             else:
109                 self.first_login = obj
110             prev_obj = obj
111             if item in plugins['config']:
112                 obj.set_config(plugins['config'][item])
113             self.__dict__[item] = obj.get_tree(self._site)
114
115     def root(self, *args, **kwargs):
116         if self.first_login:
117             raise cherrypy.HTTPRedirect('%s/login/%s' %
118                                         (self.basepath,
119                                          self.first_login.path))
120         return self._template('login/index.html', title='Login')
121
122
123 class Logout(Page):
124
125     def root(self, *args, **kwargs):
126         UserSession().logout(self.user)
127         return self._template('logout.html', title='Logout')
128
129
130 class LoginMgrsInstall(object):
131
132     def __init__(self):
133         pi = PluginInstaller(LoginMgrsInstall)
134         self.plugins = pi.get_plugins()