in kitsune/users/auth.py [0:0]
def update_user(self, user, claims):
"""Update existing user with new claims, if necessary save, and return user"""
profile = user.profile
fxa_uid = claims.get("uid")
email = claims.get("email")
user_attr_changed = False
# Check if the user has active subscriptions
subscriptions = claims.get("subscriptions", [])
if (request := getattr(self, "request", None)) and not profile.is_fxa_migrated:
# Check if there is already a Mozilla account with this ID
if Profile.objects.filter(fxa_uid=fxa_uid).exists():
msg = _("This Mozilla account is already used in another profile.")
messages.error(request, msg)
return None
# If it's not migrated, we can assume that there isn't an FxA id too
profile.is_fxa_migrated = True
profile.fxa_uid = fxa_uid
# This is the first time an existing user is using FxA. Redirect to profile edit
# in case the user wants to update any settings.
request.session["oidc_login_next"] = reverse("users.edit_my_profile")
messages.info(request, "fxa_notification_updated")
# There is a change in the email in Mozilla accounts. Let's update user's email
# unless we have a superuser
if email and (email != user.email) and not user.profile.in_staff_group:
if User.objects.exclude(id=user.id).filter(email=email).exists():
if request:
msg = _(
"The e-mail address used with this Mozilla account is already "
"linked in another profile."
)
messages.error(request, msg)
return None
user.email = email
user_attr_changed = True
# Follow avatars from FxA profiles
profile.fxa_avatar = claims.get("avatar", "")
# User subscription information
products = Product.active.filter(codename__in=subscriptions)
profile.products.clear()
profile.products.add(*products)
# update contributor status
self.update_contributor_status(profile)
# Users can select their own display name.
if not profile.name:
profile.name = claims.get("displayName", "")
# If there is a refresh token, store it
if self.refresh_token:
profile.fxa_refresh_token = self.refresh_token
with transaction.atomic():
if user_attr_changed:
user.save()
profile.save()
# If we have an updated email, let's update Zendesk too
# the check is repeated for now but it will save a few
# API calls if we trigger the task only when we know that we have new emails
if user_attr_changed:
update_zendesk_identity.delay(user.id, email)
return user