From 76b0f5fc465f3d966c9691f6fdf3ddc17e3dd8c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Thu, 8 Sep 2016 12:03:51 +0200 Subject: [PATCH] Moved login-code into a separate function. This makes it easier to log in users by their token from unittests. --- pillar/auth/__init__.py | 12 ++++++++++++ pillar/web/users/routes.py | 12 +++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/pillar/auth/__init__.py b/pillar/auth/__init__.py index e1a5bb33..313ac30d 100644 --- a/pillar/auth/__init__.py +++ b/pillar/auth/__init__.py @@ -33,6 +33,11 @@ class UserClass(flask_login.UserMixin): class AnonymousUser(flask_login.AnonymousUserMixin): + @property + def objectid(self): + """Anonymous user has no settable objectid.""" + return None + def has_role(self, *roles): return False @@ -73,6 +78,13 @@ def config_login_manager(app): return login_manager +def login_user(oauth_token): + """Log in the user identified by the given token.""" + + user = UserClass(oauth_token) + flask_login.login_user(user) + + def get_blender_id_oauth_token(): """Returns a tuple (token, ''), for use with flask_oauthlib.""" return session.get('blender_id_oauth_token') diff --git a/pillar/web/users/routes.py b/pillar/web/users/routes.py index 5416de39..cdaac312 100644 --- a/pillar/web/users/routes.py +++ b/pillar/web/users/routes.py @@ -6,11 +6,12 @@ import urlparse from flask import (abort, Blueprint, current_app, flash, redirect, render_template, request, session, url_for) -from flask_login import login_required, login_user, logout_user, current_user +from flask_login import login_required, logout_user, current_user from flask_oauthlib.client import OAuthException from werkzeug import exceptions as wz_exceptions -from pillar.auth import UserClass, subscriptions +import pillar.auth +from pillar.auth import subscriptions from pillar.web import system_util from .forms import UserProfileForm from .forms import UserSettingsEmailsForm @@ -57,9 +58,7 @@ def blender_id_authorized(): session['blender_id_oauth_token'] = (oauth_resp['access_token'], '') - user = UserClass(oauth_resp['access_token']) - login_user(user) - current_app.login_manager.reload_user() # This ensures that flask_login.current_user is set. + pillar.auth.login_user(oauth_resp['access_token']) if current_user is not None: # Check with the store for user roles. If the user has an active @@ -91,8 +90,7 @@ def login_local(): return abort(r.status_code) res = r.json() # If correct, receive token and log in the user - user = UserClass(res['token']) - login_user(user) + pillar.auth.login_user(res['token']) return redirect(url_for('main.homepage')) return render_template('users/login.html', form=form)