diff --git a/README.rst b/README.rst index cb3e37dcb..e547a6a8a 100644 --- a/README.rst +++ b/README.rst @@ -199,7 +199,10 @@ Configuration in case your user layout needs to purify username on some weird way. - Final user name will have an integer suffix in case it's already taken. + Final user name will have a random UUID-generated suffix in case it's already + taken. The UUID token max length can be changed with the setting:: + + SOCIAL_AUTH_UUID_LENGTH = 16 - Backends will store extra values from response by default, set this to False to avoid such behavior:: @@ -249,9 +252,10 @@ Configuration 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:: + It is also possible to associate multiple user accounts with a single email + address as long as the rest of the user data is unique. Set value as True + to enable, otherwise set as False to disable. + This behavior is disabled by default (false) unless specifically set:: SOCIAL_AUTH_ASSOCIATE_BY_MAIL = True @@ -496,6 +500,20 @@ credentials in the following way:: TEST_GOOGLE_USER = 'testing_account@gmail.com' TEST_GOOGLE_PASSWORD = 'password_for_testing_account' + +------------- +Miscellaneous +------------- + +South_ users should add this rule to enable migrations:: + try: + import south + from south.modelsinspector import add_introspection_rules + add_introspection_rules([], ["^social_auth\.fields\.JSONField"]) + except: + pass + + ---- Bugs ---- @@ -597,3 +615,4 @@ Base work is copyrighted by: .. _mattucf: https://github.com/mattucf .. _Quard: https://github.com/Quard .. _micrypt: https://github.com/micrypt +.. _South: http://south.aeracode.org/ diff --git a/doc/configuration.rst b/doc/configuration.rst index 5330fe5a9..624897224 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -110,7 +110,10 @@ Configuration in case your user layout needs to purify username on some weird way. - Final user name will have an integer suffix in case it's already taken. + Final user name will have a random UUID-generated suffix in case it's already + taken. The UUID token max length can be changed with the setting:: + + SOCIAL_AUTH_UUID_LENGTH = 16 - Backends will store extra values from response by default, set this to False to avoid such behavior:: diff --git a/doc/index.rst b/doc/index.rst index e1686b3ad..926317cd0 100644 --- a/doc/index.rst +++ b/doc/index.rst @@ -19,6 +19,7 @@ Contents: signals contributions testing + miscellaneous bugs Indices and tables diff --git a/doc/miscellaneous.rst b/doc/miscellaneous.rst new file mode 100644 index 000000000..dcbc1025d --- /dev/null +++ b/doc/miscellaneous.rst @@ -0,0 +1,13 @@ +Miscellaneous +============= + +South_ users should add this rule to enable migrations:: + try: + import south + from south.modelsinspector import add_introspection_rules + add_introspection_rules([], ["^social_auth\.fields\.JSONField"]) + except: + pass + + +.. _South: http://south.aeracode.org/ diff --git a/social_auth/backends/__init__.py b/social_auth/backends/__init__.py index 76d3dc715..4eb1f1595 100644 --- a/social_auth/backends/__init__.py +++ b/social_auth/backends/__init__.py @@ -65,8 +65,6 @@ User = UserSocialAuth._meta.get_field('user').rel.to # username field max length USERNAME_MAX_LENGTH = User._meta.get_field(USERNAME).max_length -# uuid hex characters to keep while generating unique usernames -UUID_MAX_LENGTH = 16 # a few settings values def _setting(name, default=None): @@ -192,9 +190,10 @@ def get_random_username(): # breaking the field max_length value, this reduces the # uniqueness, but it's less likely to happen repetitions than # increasing an index. - if len(username) + UUID_MAX_LENGTH > USERNAME_MAX_LENGTH: - username = username[:USERNAME_MAX_LENGTH - UUID_MAX_LENGTH] - name = username + uuid4().get_hex()[:UUID_MAX_LENGTH] + uuid_length = getattr(settings, 'SOCIAL_AUTH_UUID_LENGTH', 16) + if len(username) + uuid_length > USERNAME_MAX_LENGTH: + username = username[:USERNAME_MAX_LENGTH - uuid_length] + name = username + uuid4().get_hex()[:uuid_length] return final_username diff --git a/social_auth/views.py b/social_auth/views.py index 6c446ff30..23fcb63b9 100644 --- a/social_auth/views.py +++ b/social_auth/views.py @@ -47,9 +47,11 @@ def complete_process(request, backend): login(request, user) if getattr(settings, 'SOCIAL_AUTH_SESSION_EXPIRATION', True): # Set session expiration date if present and not disabled by - # setting + # setting. Use last social-auth instance for current provider, + # users can associate several accounts with a same provider. backend_name = backend.AUTH_BACKEND.name - social_user = user.social_auth.get(provider=backend_name) + social_user = user.social_auth.filter(provider=backend_name) \ + .order_by('-id')[0] if social_user.expiration_delta(): request.session.set_expiry(social_user.expiration_delta()) url = request.session.pop(REDIRECT_FIELD_NAME, '') or DEFAULT_REDIRECT