diff --git a/zerver/lib/users.py b/zerver/lib/users.py index 34062c80ec..75f955dae1 100644 --- a/zerver/lib/users.py +++ b/zerver/lib/users.py @@ -510,20 +510,10 @@ def can_access_delivery_email( if target_user_id == user_profile.id: return True - # Bots always have email_address_visibility as EMAIL_ADDRESS_VISIBILITY_EVERYONE. - if email_address_visibility == UserProfile.EMAIL_ADDRESS_VISIBILITY_EVERYONE: - return True - - if email_address_visibility == UserProfile.EMAIL_ADDRESS_VISIBILITY_ADMINS: - return user_profile.is_realm_admin - - if email_address_visibility == UserProfile.EMAIL_ADDRESS_VISIBILITY_MODERATORS: - return user_profile.is_realm_admin or user_profile.is_moderator - - if email_address_visibility == UserProfile.EMAIL_ADDRESS_VISIBILITY_MEMBERS: - return not user_profile.is_guest - - return False + return ( + email_address_visibility + in UserProfile.ROLE_TO_ACCESSIBLE_EMAIL_ADDRESS_VISIBILITY_IDS[user_profile.role] + ) class APIUserDict(TypedDict): diff --git a/zerver/models/users.py b/zerver/models/users.py index 61b92e588b..46b30187a0 100644 --- a/zerver/models/users.py +++ b/zerver/models/users.py @@ -523,6 +523,35 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings): ROLE_GUEST, ] + # Maps: user_profile.role -> which email_address_visibility values + # allow user_profile to see their email address. + ROLE_TO_ACCESSIBLE_EMAIL_ADDRESS_VISIBILITY_IDS = { + ROLE_REALM_OWNER: [ + UserBaseSettings.EMAIL_ADDRESS_VISIBILITY_ADMINS, + UserBaseSettings.EMAIL_ADDRESS_VISIBILITY_MODERATORS, + UserBaseSettings.EMAIL_ADDRESS_VISIBILITY_MEMBERS, + UserBaseSettings.EMAIL_ADDRESS_VISIBILITY_EVERYONE, + ], + ROLE_REALM_ADMINISTRATOR: [ + UserBaseSettings.EMAIL_ADDRESS_VISIBILITY_ADMINS, + UserBaseSettings.EMAIL_ADDRESS_VISIBILITY_MODERATORS, + UserBaseSettings.EMAIL_ADDRESS_VISIBILITY_MEMBERS, + UserBaseSettings.EMAIL_ADDRESS_VISIBILITY_EVERYONE, + ], + ROLE_MODERATOR: [ + UserBaseSettings.EMAIL_ADDRESS_VISIBILITY_MODERATORS, + UserBaseSettings.EMAIL_ADDRESS_VISIBILITY_MEMBERS, + UserBaseSettings.EMAIL_ADDRESS_VISIBILITY_EVERYONE, + ], + ROLE_MEMBER: [ + UserBaseSettings.EMAIL_ADDRESS_VISIBILITY_MEMBERS, + UserBaseSettings.EMAIL_ADDRESS_VISIBILITY_EVERYONE, + ], + ROLE_GUEST: [ + UserBaseSettings.EMAIL_ADDRESS_VISIBILITY_EVERYONE, + ], + } + # Whether the user has been "soft-deactivated" due to weeks of inactivity. # For these users we avoid doing UserMessage table work, as an optimization # for large Zulip organizations with lots of single-visit users.