Use proper url in emails sent to pending participants
[cds-indico.git] / indico / modules / auth / util.py
blobca4273c35788d29b36e21804891addc5d9449c78
1 # This file is part of Indico.
2 # Copyright (C) 2002 - 2015 European Organization for Nuclear Research (CERN).
4 # Indico is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3 of the
7 # License, or (at your option) any later version.
9 # Indico is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with Indico; if not, see <http://www.gnu.org/licenses/>.
17 from __future__ import unicode_literals
19 from flask import session, redirect, request
20 from werkzeug.datastructures import MultiDict
22 from indico.core.config import Config
23 from indico.web.flask.util import url_for
26 def save_identity_info(identity_info, user):
27 """Saves information from IdentityInfo in the session"""
28 trusted_email = identity_info.provider.settings.get('trusted_email', False)
29 session['login_identity_info'] = {
30 'provider': identity_info.provider.name,
31 'provider_title': identity_info.provider.title,
32 'identifier': identity_info.identifier,
33 'multipass_data': identity_info.multipass_data,
34 'data': dict(identity_info.data.lists()),
35 'indico_user_id': user.id if user else None,
36 'email_verified': bool(identity_info.data.get('email') and trusted_email)
40 def load_identity_info():
41 """Retrieves identity information from the session"""
42 try:
43 info = session['login_identity_info'].copy()
44 except KeyError:
45 return None
46 # Restoring a multidict is quite ugly...
47 data = info.pop('data')
48 info['data'] = MultiDict()
49 info['data'].update(data)
50 return info
53 def redirect_to_login(next_url=None, reason=None):
54 """Redirects to the login page.
56 :param next_url: URL to be redirected upon successful login. If not
57 specified, it will be set to ``request.relative_url``.
58 :param reason: Why the user is redirected to a login page.
59 """
60 if not next_url:
61 next_url = request.relative_url
62 if reason:
63 session['login_reason'] = reason
64 return redirect(url_for_login(next_url))
67 def url_for_login(next_url=None):
68 return url_for('auth.login', next=next_url, _external=True, _secure=True)
71 def url_for_logout(next_url=None):
72 return url_for('auth.logout', next=next_url)
75 def url_for_register(next_url=None):
76 if Config.getInstance().getLocalIdentities():
77 return url_for('auth.register', next=next_url)
79 external_url = Config.getInstance().getExternalRegistrationURL()
80 return external_url or url_for_login()