5

I've successfully managed to use django-socialauth to associate an account (in this case, an instagram account) with an existing user account. I've also set up my pipeline to collect additional user details:

def update_social_auth(backend, details, response, social_user, uid, user,
                   *args, **kwargs):

    if getattr(backend, 'name', None) in ('instagram', 'tumblr'):
        social_user.extra_data['username'] = details.get('username')

    social_user.save()

This works great when an account is associated for the first time. However, if the account has already been associated, the username field will not be present in extra_data.

How can I update a user's extra_data after the association has already been made? Is there a way using django-socialauth to do this without disconnecting and reconnecting, or using the account's (e.g Instagram's) API?

If it helps, this is my pipeline at the moment:

SOCIAL_AUTH_PIPELINE = (
    'social_auth.backends.pipeline.social.social_auth_user',
    'social_auth.backends.pipeline.social.associate_user',
    'social_auth.backends.pipeline.social.load_extra_data',
    'social_auth.backends.pipeline.user.update_user_details',
    'apps.utils.social.utils.update_social_auth'
)
NT3RP
  • 15,262
  • 9
  • 61
  • 97

1 Answers1

0

Here is a snippet of code I use to add 'admin' and 'staff' options to an existing Django user; I don't know about django-socialauth or the extra_data field, but I'm guessing something like this might be applicable:

 :
userqueryset = User.objects.filter(username=user_name)
if not userqueryset:
    print("User %s does not exist"%user_name, file=sys.stderr)
    return am_errors.AM_USERNOTEXISTS
# Have all the details - now update the user in the Django user database
# see:
#   https://docs.djangoproject.com/en/1.7/ref/contrib/auth/#django.contrib.auth.models.User
#   https://docs.djangoproject.c om/en/1.7/ref/contrib/auth/#manager-methods
user = userqueryset[0]
user.is_staff     = True
user.is_superuser = True
user.save()
 :

FWIW, my app is using 3rd party authentication (specifically atm OpenId Connect via Google+), so I think there's some common goal here. In my case I want to be able to add Django admin privileges to a user that has already been created.

The full module containing the above code is at github.com/gklyne/annalist/blob/develop/src/annalist_root/annalist_manager/am_createuser.py#L231

Graham Klyne
  • 816
  • 10
  • 16