From f493c688cc5fb72b2a870a25c68f11cb12bc1c2e Mon Sep 17 00:00:00 2001 From: Alex Dehnert Date: Mon, 28 Mar 2022 01:30:07 -0400 Subject: [PATCH] Allow per-backend user pipeline settings According to https://python-social-auth.readthedocs.io/en/latest/configuration/settings.html, "All settings can be defined per-backend by adding the backend name to the setting name, like SOCIAL_AUTH_TWITTER_LOGIN_URL". This changes user.py to actually use per-backend settings for options like USERNAME_IS_FULL_EMAIL. Note that get_username is always called with a backend, but user_details isn't, so a different version of `setting' is needed for the two. --- social_core/pipeline/user.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/social_core/pipeline/user.py b/social_core/pipeline/user.py index e7f15b72..79665b10 100644 --- a/social_core/pipeline/user.py +++ b/social_core/pipeline/user.py @@ -11,17 +11,17 @@ def get_username(strategy, details, backend, user=None, *args, **kwargs): storage = strategy.storage if not user: - email_as_username = strategy.setting('USERNAME_IS_FULL_EMAIL', False) - uuid_length = strategy.setting('UUID_LENGTH', 16) + email_as_username = backend.setting('USERNAME_IS_FULL_EMAIL', False) + uuid_length = backend.setting('UUID_LENGTH', 16) max_length = storage.user.username_max_length() - do_slugify = strategy.setting('SLUGIFY_USERNAMES', False) - do_clean = strategy.setting('CLEAN_USERNAMES', True) + do_slugify = backend.setting('SLUGIFY_USERNAMES', False) + do_clean = backend.setting('CLEAN_USERNAMES', True) def identity_func(val): return val if do_clean: - override_clean = strategy.setting('CLEAN_USERNAME_FUNCTION') + override_clean = backend.setting('CLEAN_USERNAME_FUNCTION') if override_clean: clean_func = module_member(override_clean) else: @@ -30,7 +30,7 @@ def identity_func(val): clean_func = identity_func if do_slugify: - override_slug = strategy.setting('SLUGIFY_FUNCTION') + override_slug = backend.setting('SLUGIFY_FUNCTION') if override_slug: slug_func = module_member(override_slug) else: @@ -88,19 +88,19 @@ def user_details(strategy, details, backend, user=None, *args, **kwargs): # Default protected user fields (username, id, pk and email) can be ignored # by setting the SOCIAL_AUTH_NO_DEFAULT_PROTECTED_USER_FIELDS to True - if strategy.setting('NO_DEFAULT_PROTECTED_USER_FIELDS') is True: + if strategy.setting('NO_DEFAULT_PROTECTED_USER_FIELDS', backend=backend) is True: protected = () else: protected = ('username', 'id', 'pk', 'email', 'password', 'is_active', 'is_staff', 'is_superuser',) - protected = protected + tuple(strategy.setting('PROTECTED_USER_FIELDS', [])) + protected = protected + tuple(strategy.setting('PROTECTED_USER_FIELDS', [], backend=backend)) # 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. - field_mapping = strategy.setting('USER_FIELD_MAPPING', {}, backend) + field_mapping = strategy.setting('USER_FIELD_MAPPING', {}, backend=backend) for name, value in details.items(): # Convert to existing user field if mapping exists name = field_mapping.get(name, name) @@ -111,7 +111,7 @@ def user_details(strategy, details, backend, user=None, *args, **kwargs): if current_value == value: continue - immutable_fields = tuple(strategy.setting('IMMUTABLE_USER_FIELDS', [])) + immutable_fields = tuple(strategy.setting('IMMUTABLE_USER_FIELDS', [], backend=backend)) if name in immutable_fields and current_value: continue