annotate src/pages/login/page_meta.py @ 979:1d558dfb32ca

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