From 179e8c7e97aec23786cc2a4b013b7c4fe1707dae Mon Sep 17 00:00:00 2001 From: Binnette Date: Sun, 1 Mar 2020 00:12:10 +0100 Subject: [PATCH] Update username with value from OAuth provider --- umap/settings/base.py | 2 +- umap/utils.py | 30 ++++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/umap/settings/base.py b/umap/settings/base.py index 233653d4..388fa1a9 100644 --- a/umap/settings/base.py +++ b/umap/settings/base.py @@ -213,5 +213,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.user_details' ) diff --git a/umap/utils.py b/umap/utils.py index 57d4ce30..1b1f9e0e 100644 --- a/umap/utils.py +++ b/umap/utils.py @@ -109,3 +109,33 @@ 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 user_details(strategy, details, user=None, *args, **kwargs): + """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)