VC: Fix error on clone page for legacy-ID events
[cds-indico.git] / indico / modules / auth / util.py
blob9ed1876e3d74068556f6b196b2d610e7c8faaeb5
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.web.flask.util import url_for
25 def save_identity_info(identity_info, user):
26 """Saves information from IdentityInfo in the session"""
27 trusted_email = identity_info.provider.settings.get('trusted_email', False)
28 session['login_identity_info'] = {
29 'provider': identity_info.provider.name,
30 'provider_title': identity_info.provider.title,
31 'identifier': identity_info.identifier,
32 'multipass_data': identity_info.multipass_data,
33 'data': dict(identity_info.data.lists()),
34 'indico_user_id': user.id if user else None,
35 'email_verified': bool(identity_info.data.get('email') and trusted_email)
39 def load_identity_info():
40 """Retrieves identity information from the session"""
41 try:
42 info = session['login_identity_info'].copy()
43 except KeyError:
44 return None
45 # Restoring a multidict is quite ugly...
46 data = info.pop('data')
47 info['data'] = MultiDict()
48 info['data'].update(data)
49 return info
52 def redirect_to_login(next_url=None, reason=None):
53 """Redirects to the login page.
55 :param next_url: URL to be redirected upon successful login. If not
56 specified, it will be set to ``request.relative_url``.
57 :param reason: Why the user is redirected to a login page.
58 """
59 if not next_url:
60 next_url = request.relative_url
61 if reason:
62 session['login_reason'] = reason
63 return redirect(url_for_login(next_url))
66 def url_for_login(next_url=None):
67 return url_for('auth.login', next=next_url, _external=True, _secure=True)
70 def url_for_logout(next_url=None):
71 return url_for('auth.logout', next=next_url)