From fec95b2472c5aa08da817819c14fb5c159aec2d6 Mon Sep 17 00:00:00 2001 From: lijianguo Date: Fri, 11 Mar 2022 17:20:56 +0800 Subject: [PATCH] chore(oauth2): update user info pipline --- rootfs/api/authentication.py | 3 ++- rootfs/api/backend.py | 2 +- rootfs/api/pipeline.py | 10 +++++++++- rootfs/api/serializers.py | 2 +- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/rootfs/api/authentication.py b/rootfs/api/authentication.py index 130442d7..90bf5352 100644 --- a/rootfs/api/authentication.py +++ b/rootfs/api/authentication.py @@ -53,7 +53,8 @@ def _get_user(key): user_info = OAuthManager().get_user_by_token(key) if not user_info.get('email'): user_info['email'] = OAuthManager().get_email_by_token(key) - return serializers.UserSerializer.update_or_create(user_info) + user, _ = serializers.UserSerializer.update_or_create(user_info) + return user except Exception as e: logger.info(e) raise exceptions.AuthenticationFailed(_('Verify token fail.')) diff --git a/rootfs/api/backend.py b/rootfs/api/backend.py index 8de031f9..d5c181fa 100644 --- a/rootfs/api/backend.py +++ b/rootfs/api/backend.py @@ -25,7 +25,7 @@ def authenticate(self, request, username=None, password=None, **kwargs): user_info['password'] = password if not user_info.get('email'): user_info['email'] = client.get_email() - user = serializers.UserSerializer.update_or_create(user_info) + user, _ = serializers.UserSerializer.update_or_create(user_info) return user def get_user(self, user_id): diff --git a/rootfs/api/pipeline.py b/rootfs/api/pipeline.py index 3c39d1fc..0426ccb7 100644 --- a/rootfs/api/pipeline.py +++ b/rootfs/api/pipeline.py @@ -2,7 +2,15 @@ def update_or_create(backend, user, response, *args, **kwargs): - UserSerializer.update_or_create(response) + user, created = UserSerializer.update_or_create(response) + + if not created: + return {'is_new': False} + + return { + 'is_new': True, + 'user': user + } def load_extra_data(backend, details, response, uid, user, *args, **kwargs): diff --git a/rootfs/api/serializers.py b/rootfs/api/serializers.py index cf279e0f..701f5ae6 100644 --- a/rootfs/api/serializers.py +++ b/rootfs/api/serializers.py @@ -164,7 +164,7 @@ def update_or_create(data): user.date_joined = now user.set_unusable_password() user.save() - return user + return user, created class AdminUserSerializer(serializers.ModelSerializer):