Merge pull request #754 from Binnette/sync-username-on-connect

Update username with value from OAuth provider
This commit is contained in:
Yohan Boniface 2020-03-09 17:06:00 +01:00 committed by GitHub
commit 4000da1d41
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 1 deletions

View file

@ -223,5 +223,5 @@ SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.user.create_user',
'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data',
'social_core.pipeline.user.user_details'
'umap.utils.oauth_user_details'
)

View file

@ -109,3 +109,37 @@ def gzip_file(from_path, to_path):
with open(from_path, 'rb') as f_in:
with gzip.open(to_path, 'wb') as f_out:
f_out.writelines(f_in)
def oauth_user_details(strategy, details, user=None, *args, **kwargs):
"""
This method is a pipeline stage only to be used with social_auth
Note this is a workaround that can break if social_auth is updated
Update user details using data from provider.
"""
if not user:
return
changed = False # flag to track changes
# uMap fix: remove 'username' from protected fields
protected = ('id', 'pk', 'email') + \
tuple(strategy.setting('PROTECTED_USER_FIELDS', []))
# Update user model attributes with the new data sent by the current
# provider. Update on some attributes is disabled by default, for
# example username and id fields. It's also possible to disable update
# on fields defined in SOCIAL_AUTH_PROTECTED_USER_FIELDS.
for name, value in details.items():
if value is None or not hasattr(user, name) or name in protected:
continue
# Check https://github.com/omab/python-social-auth/issues/671
current_value = getattr(user, name, None)
# uMap fix: update field when value has changed
if current_value == value:
continue
changed = True
setattr(user, name, value)
if changed:
strategy.storage.user.changed(user)