How to update user `extra_data` after they have been associated with an account?

I was able to use django-socialauthto associate an account (in this case, an instagram account) with an existing user account. I also created my pipeline to collect additional user information:

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 the account is connected for the first time. However, if the account has already been linked, the field usernamewill not be present in extra_data.

How can I update a user extra_dataafter an association has already been made? Is there a way to use django-socialauthto do this without disconnecting and reconnecting or using an account (e.g. Instagram) API?

If this helps, this is my pipeline for now:

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'
)
+5
source share
1 answer

Here is the code snippet that I use to add the "admin" and "staff" parameters to an existing Django user; I do not know about django-socialauthor field extra_data, but I assume that this may 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 application uses third-party authentication (in particular, OpenID Connect via Google+), so I think that there is a common goal. In my case, I want to be able to add Django admin privileges to an already created user.

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

0

All Articles