From 095f1cda0cc7b42ee4d6841af34ae2845d70f1a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 5 May 2017 12:56:19 +0200 Subject: [PATCH] Added "Switch user" functionality. The user isn't logged out until the new user logs in. This allows you to click on "Log in as different user", hit the back button, and still be logged in. --- pillar/api/blender_id.py | 9 +++++++++ pillar/web/users/routes.py | 17 +++++++++++++++++ src/templates/layout.jade | 5 +++++ 3 files changed, 31 insertions(+) diff --git a/pillar/api/blender_id.py b/pillar/api/blender_id.py index 4d849820..3fcc0ebb 100644 --- a/pillar/api/blender_id.py +++ b/pillar/api/blender_id.py @@ -284,3 +284,12 @@ def fetch_blenderid_user() -> dict: def setup_app(app, url_prefix): app.register_api_blueprint(blender_id, url_prefix=url_prefix) + + +def switch_user_url(next_url: str) -> str: + from urllib.parse import quote + + base_url = '%s/switch' % blender_id_endpoint() + if next_url: + return '%s?next=%s' % (base_url, quote(next_url)) + return base_url diff --git a/pillar/web/users/routes.py b/pillar/web/users/routes.py index 86fdc410..1b23492c 100644 --- a/pillar/web/users/routes.py +++ b/pillar/web/users/routes.py @@ -107,6 +107,23 @@ def logout(): return redirect('/') +@blueprint.route('/switch') +def switch(): + from pillar.api import blender_id + + # Without this URL, the Cloud will redirect to the HTTP Referrer, which is the Blender ID + # 'switch user' page. We need to explicitly send the user to the homepage to prevent this. + next_url_after_cloud_login = url_for('main.homepage') + + # Without this URL, the user will remain on the Blender ID site. We want them to come + # back to the Cloud after switching users. + next_url_after_bid_login = url_for('users.login', + next=next_url_after_cloud_login, + _external=True) + + return redirect(blender_id.switch_user_url(next_url=next_url_after_bid_login)) + + @blueprint.route('/settings/profile', methods=['GET', 'POST']) @login_required def settings_profile(): diff --git a/src/templates/layout.jade b/src/templates/layout.jade index e0ff3393..2324d889 100644 --- a/src/templates/layout.jade +++ b/src/templates/layout.jade @@ -299,6 +299,11 @@ html(lang="en") | Subscription li.divider(role="separator") | {% endif %} + li + a.navbar-item( + href="{{ url_for('users.switch') }}") + i.pi-log-out(title="Log Out") + | Not {{ current_user.full_name }}? Log in as another user li a.navbar-item( href="{{ url_for('users.logout') }}")