comparison src/pages/login/page_meta.py @ 963:2932170bb526

pages: added login/logged and register pages - login page will redirect to logged if a session is already active - if a redirect url is set, use will be redirected to it when login/registration is successful - logged page allow to disconnect profile
author Goffi <goffi@goffi.org>
date Fri, 27 Oct 2017 18:47:07 +0200
parents
children cdd389ef97bc
comparison
equal deleted inserted replaced
962:c7fba7709d05 963:2932170bb526
1 #!/usr/bin/env python2.7
2 # -*- coding: utf-8 -*-
3
4 from sat.core.i18n import _
5 from sat.core import exceptions
6 from libervia.server.constants import Const as C
7 from libervia.server import session_iface
8 from twisted.internet import defer
9 from sat.core.log import getLogger
10 log = getLogger('pages/login')
11
12 """SàT log-in page, with link to create an account"""
13
14 name = u"login"
15 access = C.PAGES_ACCESS_PUBLIC
16 template = u"login/login.html"
17
18
19 def prepare_render(self, request):
20 template_data = request.template_data
21
22 # we redirect to logged page if a session is active
23 profile = self.getProfile(request)
24 if profile is not None:
25 self.pageRedirect('/login/logged', request)
26
27 # login error message
28 session_data = self.host.getSessionData(request, session_iface.ISATSession)
29 login_error = session_data.popPageData(self, 'login_error')
30 if login_error is not None:
31 template_data['S_C'] = C # we need server constants in template
32 template_data['login_error'] = login_error
33 template_data['empty_password_allowed'] = bool(self.host.options['empty_password_allowed_warning_dangerous_list'])
34
35 # register page url
36 template_data['register_url'] = self.getPageRedirectURL(request, 'register')
37
38 # if login is set, we put it in template to prefill field
39 template_data['login'] = session_data.popPageData(self, 'login')
40
41 def login_error(self, request, error_const):
42 """set login_error in page data
43
44 @param error_const(unicode): one of login error constant
45 @return C.POST_NO_CONFIRM: avoid confirm message
46 """
47 session_data = self.host.getSessionData(request, session_iface.ISATSession)
48 session_data.setPageData(self, "login_error", error_const)
49 return C.POST_NO_CONFIRM
50
51 @defer.inlineCallbacks
52 def on_data_post(self, request):
53 profile = self.getProfile(request)
54 type_ = self.getPostedData(request, 'type')
55 if type_ == 'disconnect':
56 if profile is None:
57 log.warning(_(u'Disconnect called when no profile is logged'))
58 self.pageError(request, C.HTTP_BAD_REQUEST)
59 else:
60 self.host.purgeSession(request)
61 defer.returnValue(C.POST_NO_CONFIRM)
62 elif type_ == 'login':
63 login, password = self.getPostedData(request, (u'login', u'password'))
64 try:
65 status = yield self.host.connect(request, login, password)
66 except ValueError as e:
67 if e.message in (C.XMPP_AUTH_ERROR, C.PROFILE_AUTH_ERROR):
68 defer.returnValue(login_error(self, request, e.message))
69 else:
70 # this error was not expected!
71 raise e
72 except exceptions.TimeOutError:
73 defer.returnValue(login_error(self, request, C.NO_REPLY))
74 else:
75 if status in (C.PROFILE_LOGGED, C.PROFILE_LOGGED_EXT_JID, C.SESSION_ACTIVE):
76 # Profile has been logged correctly
77 self.redirectOrContinue(request)
78 else:
79 log.error(_(u'Unhandled status: {status}'.format(status=status)))
80 else:
81 self.pageError(request, C.HTTP_BAD_REQUEST)