From 371a7768ad611417c6e94a31878481e7c656607a Mon Sep 17 00:00:00 2001 From: Mateusz Mandera Date: Sat, 21 Sep 2019 01:32:59 +0200 Subject: [PATCH] backends: Extract useful is_user_active function. This logic can be useful elsewhere, for checking whether user_profile is active. --- zproject/backends.py | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/zproject/backends.py b/zproject/backends.py index 9e6b0dd6cd..ee5d76e1e5 100644 --- a/zproject/backends.py +++ b/zproject/backends.py @@ -109,6 +109,21 @@ def require_email_format_usernames(realm: Optional[Realm]=None) -> bool: return False return True +def is_user_active(user_profile: UserProfile, return_data: Optional[Dict[str, Any]]=None) -> bool: + if not user_profile.is_active: + if return_data is not None: + if user_profile.is_mirror_dummy: + # Record whether it's a mirror dummy account + return_data['is_mirror_dummy'] = True + return_data['inactive_user'] = True + return False + if user_profile.realm.deactivated: + if return_data is not None: + return_data['inactive_realm'] = True + return False + + return True + def common_get_active_user(email: str, realm: Realm, return_data: Optional[Dict[str, Any]]=None) -> Optional[UserProfile]: """This is the core common function used by essentially all @@ -128,17 +143,9 @@ def common_get_active_user(email: str, realm: Realm, if return_data is not None: return_data['invalid_subdomain'] = True return None - if not user_profile.is_active: - if return_data is not None: - if user_profile.is_mirror_dummy: - # Record whether it's a mirror dummy account - return_data['is_mirror_dummy'] = True - return_data['inactive_user'] = True - return None - if user_profile.realm.deactivated: - if return_data is not None: - return_data['inactive_realm'] = True + if not is_user_active(user_profile, return_data): return None + return user_profile class ZulipAuthMixin: