]> git.parisson.com Git - django-social-auth.git/commitdiff
Associate users if the share the same email address and if there's only a single...
authorMatías Aguirre <matiasaguirre@gmail.com>
Thu, 14 Apr 2011 03:49:52 +0000 (00:49 -0300)
committerMatías Aguirre <matiasaguirre@gmail.com>
Thu, 14 Apr 2011 03:49:52 +0000 (00:49 -0300)
README.rst
doc/configuration.rst
social_auth/backends/__init__.py

index 3e6071629430fbc62e2640b0dc02fc60804bd370..45dc3663f354c56eefcb40ea7ec3791ed11df95d 100644 (file)
@@ -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
index ba78d5136ae95a5b46dd5ff534a819522c2f679a..91bb7bc73788346b669c6467f480ca0e139af8fa 100644 (file)
@@ -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
index 7b5be9f7925bd60051e38965a1ea04f77d3672c6..8e867c83a8e4ec7c68388cff1649a4f47bb86cb8 100644 (file)
@@ -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