From fca2d47ef09da56eee551768f8f59cd0594c0c16 Mon Sep 17 00:00:00 2001 From: Jonas Carson Date: Fri, 6 Oct 2023 08:36:11 -0700 Subject: [PATCH] Update username and email on drupal login --- primed/users/adapters.py | 27 ++++++++++++++++--- primed/users/tests/test_adapters.py | 41 +++++++++++++++++++++++------ 2 files changed, 57 insertions(+), 11 deletions(-) diff --git a/primed/users/adapters.py b/primed/users/adapters.py index ce34b704..b0f6d6a3 100644 --- a/primed/users/adapters.py +++ b/primed/users/adapters.py @@ -23,15 +23,36 @@ class SocialAccountAdapter(DefaultSocialAccountAdapter): def is_open_for_signup(self, request: HttpRequest, sociallogin: Any): return getattr(settings, "ACCOUNT_ALLOW_REGISTRATION", True) - def update_user_name(self, user, extra_data: Dict): + def update_user_info(self, user, extra_data: Dict): + drupal_username = extra_data.get("preferred_username") + drupal_email = extra_data.get("email") first_name = extra_data.get("first_name") last_name = extra_data.get("last_name") full_name = " ".join(part for part in (first_name, last_name) if part) + user_changed = False if user.name != full_name: logger.info( - f"[SocialAccountAdatpter:update_user_name] user {user} name updated from {user.name} to {full_name}" + f"[SocialAccountAdatpter:update_user_name] user {user} " + f"name updated from {user.name} to {full_name}" ) user.name = full_name + user_changed = True + if user.username != drupal_username: + logger.info( + f"[SocialAccountAdatpter:update_user_name] user {user} " + f"username updated from {user.username} to {drupal_username}" + ) + user.username = drupal_username + user_changed = True + if user.email != drupal_email: + logger.info( + f"[SocialAccountAdatpter:update_user_name] user {user}" + f" email updated from {user.email} to {drupal_email}" + ) + user.email = drupal_email + user_changed = True + + if user_changed is True: user.save() def update_user_study_sites(self, user, extra_data: Dict): @@ -116,6 +137,6 @@ def update_user_data(self, sociallogin: Any): extra_data = sociallogin.account.extra_data user = sociallogin.user - self.update_user_name(user, extra_data) + self.update_user_info(user, extra_data) self.update_user_study_sites(user, extra_data) self.update_user_groups(user, extra_data) diff --git a/primed/users/tests/test_adapters.py b/primed/users/tests/test_adapters.py index c65c254f..c5c24a83 100644 --- a/primed/users/tests/test_adapters.py +++ b/primed/users/tests/test_adapters.py @@ -34,36 +34,61 @@ def test_drupal_social_login_adapter(self): User = get_user_model() user = User() old_name = "Old Name" - setattr(user, account_settings.USER_MODEL_USERNAME_FIELD, "test") - setattr(user, "name", "Old Name") - setattr(user, account_settings.USER_MODEL_EMAIL_FIELD, "test@example.com") + old_username = "test" + old_email = "test@example.com" + setattr(user, account_settings.USER_MODEL_USERNAME_FIELD, old_username) + setattr(user, "name", old_name) + setattr(user, account_settings.USER_MODEL_EMAIL_FIELD, old_email) account = SocialAccount( provider="drupal_oauth_provider", uid="123", - extra_data=dict(first_name="Old", last_name="Name"), + extra_data=dict( + first_name="Old", + last_name="Name", + email=old_email, + preferred_username=old_username, + ), ) sociallogin = SocialLogin(user=user, account=account) complete_social_login(request, sociallogin) - user = User.objects.get(**{account_settings.USER_MODEL_USERNAME_FIELD: "test"}) + user = User.objects.get( + **{account_settings.USER_MODEL_USERNAME_FIELD: old_username} + ) assert SocialAccount.objects.filter(user=user, uid=account.uid).exists() is True assert user.name == old_name + assert user.username == old_username + assert user.email == old_email - def test_update_user_name(self): + def test_update_user_info(self): adapter = SocialAccountAdapter() User = get_user_model() user = User() - new_name = "New Name" + new_first_name = "New" + new_last_name = "Name" + new_name = f"{new_first_name} {new_last_name}" + new_email = "newemail@example.com" + new_username = "newusername" setattr(user, account_settings.USER_MODEL_USERNAME_FIELD, "test") setattr(user, "name", "Old Name") setattr(user, account_settings.USER_MODEL_EMAIL_FIELD, "test@example.com") user.save() - adapter.update_user_name(user, dict(first_name="New", last_name="Name")) + adapter.update_user_info( + user, + dict( + first_name=new_first_name, + last_name=new_last_name, + email=new_email, + preferred_username=new_username, + ), + ) assert user.name == new_name + assert user.email == new_email + assert user.username == new_username def test_update_user_study_sites_add(self): adapter = SocialAccountAdapter()