Skip to content

Commit

Permalink
Allow per-backend user pipeline settings
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dehnert committed Jul 13, 2024
1 parent 4a7d470 commit 3fb334e
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions social_core/pipeline/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")
slug_func = module_member(override_slug) if override_slug else slugify
else:
slug_func = identity_func
Expand Down Expand Up @@ -82,7 +82,7 @@ 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 = (
Expand All @@ -96,13 +96,13 @@ def user_details(strategy, details, backend, user=None, *args, **kwargs):
"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)
Expand All @@ -113,7 +113,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

Expand Down

0 comments on commit 3fb334e

Please sign in to comment.