backends: Early return in sync_avatar_from_ldap.

This commit is contained in:
Alex Vandiver 2024-06-13 13:04:16 +00:00 committed by Alex Vandiver
parent 0153d6dbcd
commit b36ad31f0e
1 changed files with 29 additions and 27 deletions

View File

@ -807,38 +807,40 @@ class ZulipLDAPAuthBackendBase(ZulipAuthMixin, LDAPBackend):
return username
def sync_avatar_from_ldap(self, user: UserProfile, ldap_user: _LDAPUser) -> None:
if "avatar" in settings.AUTH_LDAP_USER_ATTR_MAP:
# We do local imports here to avoid import loops
from io import BytesIO
if "avatar" not in settings.AUTH_LDAP_USER_ATTR_MAP:
return
from zerver.actions.user_settings import do_change_avatar_fields
from zerver.lib.upload import upload_avatar_image
# We do local imports here to avoid import loops
from io import BytesIO
avatar_attr_name = settings.AUTH_LDAP_USER_ATTR_MAP["avatar"]
if avatar_attr_name not in ldap_user.attrs: # nocoverage
# If this specific user doesn't have e.g. a
# thumbnailPhoto set in LDAP, just skip that user.
return
from zerver.actions.user_settings import do_change_avatar_fields
from zerver.lib.upload import upload_avatar_image
ldap_avatar = ldap_user.attrs[avatar_attr_name][0]
avatar_attr_name = settings.AUTH_LDAP_USER_ATTR_MAP["avatar"]
if avatar_attr_name not in ldap_user.attrs: # nocoverage
# If this specific user doesn't have e.g. a
# thumbnailPhoto set in LDAP, just skip that user.
return
avatar_changed = is_avatar_new(ldap_avatar, user)
if not avatar_changed:
# Don't do work to replace the avatar with itself.
return
ldap_avatar = ldap_user.attrs[avatar_attr_name][0]
# Structurally, to make the S3 backend happy, we need to
# provide a Content-Type; since that isn't specified in
# any metadata, we auto-detect it.
content_type = magic.from_buffer(ldap_avatar[:1024], mime=True)
if content_type.startswith("image/"):
upload_avatar_image(BytesIO(ldap_avatar), user, user, content_type=content_type)
do_change_avatar_fields(user, UserProfile.AVATAR_FROM_USER, acting_user=None)
# Update avatar hash.
user.avatar_hash = user_avatar_content_hash(ldap_avatar)
user.save(update_fields=["avatar_hash"])
else:
logging.warning("Could not parse %s field for user %s", avatar_attr_name, user.id)
avatar_changed = is_avatar_new(ldap_avatar, user)
if not avatar_changed:
# Don't do work to replace the avatar with itself.
return
# Structurally, to make the S3 backend happy, we need to
# provide a Content-Type; since that isn't specified in
# any metadata, we auto-detect it.
content_type = magic.from_buffer(ldap_avatar[:1024], mime=True)
if content_type.startswith("image/"):
upload_avatar_image(BytesIO(ldap_avatar), user, user, content_type=content_type)
do_change_avatar_fields(user, UserProfile.AVATAR_FROM_USER, acting_user=None)
# Update avatar hash.
user.avatar_hash = user_avatar_content_hash(ldap_avatar)
user.save(update_fields=["avatar_hash"])
else:
logging.warning("Could not parse %s field for user %s", avatar_attr_name, user.id)
def is_user_disabled_in_ldap(self, ldap_user: _LDAPUser) -> bool:
"""Implements checks for whether a user has been