From 3616c0d253e02962d11ed684f65dda4f575fbce2 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Mat=C3=ADas=20Aguirre?= Date: Thu, 14 Apr 2011 00:49:52 -0300 Subject: [PATCH] Associate users if the share the same email address and if there's only a single user entry with same email. Closes gh-49 --- README.rst | 10 ++++++++++ doc/configuration.rst | 11 +++++++++++ social_auth/backends/__init__.py | 18 +++++++++++++++--- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 3e60716..45dc366 100644 --- a/README.rst +++ b/README.rst @@ -240,6 +240,16 @@ Configuration Check example application for implementation details, but first, please take a look to `User Profiles`_, it might be what you were looking for. + It's possible to disable user creations by django-social-auth with:: + + SOCIAL_AUTH_CREATE_USERS = False + + Also, it's possible to associate user accounts that share the same email + address if the user entry is unique (that means that if the email is not used + by more than one account). This behavior is disabled by default unless:: + + SOCIAL_AUTH_ASSOCIATE_BY_MAIL = True + ------- Signals diff --git a/doc/configuration.rst b/doc/configuration.rst index ba78d51..91bb7bc 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -151,6 +151,17 @@ Configuration Check example application for implementation details, but first, please take a look to `User Profiles`_, it might be what you were looking for. + It's possible to disable user creations by django-social-auth with:: + + SOCIAL_AUTH_CREATE_USERS = False + + Also, it's possible to associate user accounts that share the same email + address if the user entry is unique (that means that if the email is not used + by more than one account). This behavior is disabled by default unless:: + + SOCIAL_AUTH_ASSOCIATE_BY_MAIL = True + + .. _Model Manager: http://docs.djangoproject.com/en/dev/topics/db/managers/#managers .. _Login URL: http://docs.djangoproject.com/en/dev/ref/settings/?from=olddocs#login-url .. _Login redirect URL: http://docs.djangoproject.com/en/dev/ref/settings/?from=olddocs#login-redirect-url diff --git a/social_auth/backends/__init__.py b/social_auth/backends/__init__.py index 7b5be9f..8e867c8 100644 --- a/social_auth/backends/__init__.py +++ b/social_auth/backends/__init__.py @@ -24,6 +24,7 @@ from oauth2 import Consumer as OAuthConsumer, Token, Request as OAuthRequest, \ SignatureMethod_HMAC_SHA1 from django.conf import settings +from django.core.exceptions import MultipleObjectsReturned from django.contrib.auth import authenticate from django.contrib.auth.backends import ModelBackend from django.utils import simplejson @@ -72,6 +73,7 @@ def _setting(name, default=None): return getattr(settings, name, default) CREATE_USERS = _setting('SOCIAL_AUTH_CREATE_USERS', True) +ASSOCIATE_BY_MAIL = _setting('SOCIAL_AUTH_ASSOCIATE_BY_MAIL', False) LOAD_EXTRA_DATA = _setting('SOCIAL_AUTH_EXTRA_DATA', True) FORCE_RANDOM_USERNAME = _setting('SOCIAL_AUTH_FORCE_RANDOM_USERNAME', False) USERNAME_FIXER = _setting('SOCIAL_AUTH_USERNAME_FIXER', lambda u: u) @@ -111,10 +113,20 @@ class SocialAuthBackend(ModelBackend): if user is None: # new user if not CREATE_USERS: return None - username = self.username(details) + email = details.get('email') - user = User.objects.create_user(username=username, email=email) - is_new = True + if ASSOCIATE_BY_MAIL: + try: + user = User.objects.get(email=email) + except MultipleObjectsReturned: + raise ValueError('Not unique email address supplied') + except User.DoesNotExist: + user = None + if not user: + username = self.username(details) + user = User.objects.create_user(username=username, + email=email) + is_new = True social_user = self.associate_auth(user, uid, response, details) else: # This account was registered to another user, so we raise an -- 2.39.5