Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
access_user: Make allow_bots parameter a mandatory kwarg.
Tim Abbott58dc337970c8
diff --git a/zerver/lib/users.py b/zerver/lib/users.pyindex 61da3115aa4..297f1bac861 100644--- a/zerver/lib/users.py+++ b/zerver/lib/users.py@@ -324,7 +324,7 @@ def access_user_by_id(target_user_id: int,*,allow_deactivated: bool = False,- allow_bots: bool = False,+ allow_bots: bool,for_admin: bool,) -> UserProfile:"""Master function for accessing another user by ID in API code;@@ -345,7 +345,7 @@ def access_user_by_id_including_cross_realm(target_user_id: int,*,allow_deactivated: bool = False,- allow_bots: bool = False,+ allow_bots: bool,for_admin: bool,) -> UserProfile:"""Variant of access_user_by_id allowing cross-realm bots to be accessed."""diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.pyindex 5f6232aa0ab..4a52843a6a8 100644--- a/zerver/tests/test_users.py+++ b/zerver/tests/test_users.py@@ -545,14 +545,14 @@ class PermissionTest(ZulipTestCase):# Must be a valid user ID in the realmwith self.assertRaises(JsonableError):- access_user_by_id(iago, 1234, for_admin=False)+ access_user_by_id(iago, 1234, allow_bots=False, for_admin=False)with self.assertRaises(JsonableError):- access_user_by_id_including_cross_realm(iago, 1234, for_admin=False)+ access_user_by_id_including_cross_realm(iago, 1234, allow_bots=False, for_admin=False)with self.assertRaises(JsonableError):- access_user_by_id(iago, self.mit_user("sipbtest").id, for_admin=False)+ access_user_by_id(iago, self.mit_user("sipbtest").id, allow_bots=False, for_admin=False)with self.assertRaises(JsonableError):access_user_by_id_including_cross_realm(- iago, self.mit_user("sipbtest").id, for_admin=False+ iago, self.mit_user("sipbtest").id, allow_bots=False, for_admin=False)# Can only access bot users if allow_bots is passed@@ -560,9 +560,9 @@ class PermissionTest(ZulipTestCase):access_user_by_id(iago, bot.id, allow_bots=True, for_admin=True)access_user_by_id_including_cross_realm(iago, bot.id, allow_bots=True, for_admin=True)with self.assertRaises(JsonableError):- access_user_by_id(iago, bot.id, for_admin=True)+ access_user_by_id(iago, bot.id, allow_bots=False, for_admin=True)with self.assertRaises(JsonableError):- access_user_by_id_including_cross_realm(iago, bot.id, for_admin=True)+ access_user_by_id_including_cross_realm(iago, bot.id, allow_bots=False, for_admin=True)# Only the including_cross_realm variant works for system bots.system_bot = get_system_bot(settings.WELCOME_BOT, internal_realm.id)@@ -573,43 +573,61 @@ class PermissionTest(ZulipTestCase):)# And even then, only if `allow_bots` was passed.with self.assertRaises(JsonableError):- access_user_by_id(iago, system_bot.id, for_admin=False)+ access_user_by_id(iago, system_bot.id, allow_bots=False, for_admin=False)with self.assertRaises(JsonableError):- access_user_by_id_including_cross_realm(iago, system_bot.id, for_admin=False)+ access_user_by_id_including_cross_realm(+ iago, system_bot.id, allow_bots=False, for_admin=False+ )# Can only access deactivated users if allow_deactivated is passedhamlet = self.example_user("hamlet")do_deactivate_user(hamlet, acting_user=None)with self.assertRaises(JsonableError):- access_user_by_id(iago, hamlet.id, for_admin=False)+ access_user_by_id(iago, hamlet.id, allow_bots=False, for_admin=False)with self.assertRaises(JsonableError):- access_user_by_id_including_cross_realm(iago, hamlet.id, for_admin=False)+ access_user_by_id_including_cross_realm(+ iago, hamlet.id, allow_bots=False, for_admin=False+ )with self.assertRaises(JsonableError):- access_user_by_id(iago, hamlet.id, for_admin=True)+ access_user_by_id(iago, hamlet.id, allow_bots=False, for_admin=True)with self.assertRaises(JsonableError):- access_user_by_id_including_cross_realm(iago, hamlet.id, for_admin=True)- access_user_by_id(iago, hamlet.id, allow_deactivated=True, for_admin=True)+ access_user_by_id_including_cross_realm(+ iago, hamlet.id, allow_bots=False, for_admin=True+ )+ access_user_by_id(iago, hamlet.id, allow_bots=False, allow_deactivated=True, for_admin=True)access_user_by_id_including_cross_realm(- iago, hamlet.id, allow_deactivated=True, for_admin=True+ iago, hamlet.id, allow_bots=False, allow_deactivated=True, for_admin=True)# Non-admin user can't admin another userwith self.assertRaises(JsonableError):access_user_by_id(- self.example_user("cordelia"), self.example_user("aaron").id, for_admin=True+ self.example_user("cordelia"),+ self.example_user("aaron").id,+ allow_bots=False,+ for_admin=True,)with self.assertRaises(JsonableError):access_user_by_id_including_cross_realm(- self.example_user("cordelia"), self.example_user("aaron").id, for_admin=True+ self.example_user("cordelia"),+ self.example_user("aaron").id,+ allow_bots=False,+ for_admin=True,)# But does have read-only access to it.access_user_by_id(- self.example_user("cordelia"), self.example_user("aaron").id, for_admin=False+ self.example_user("cordelia"),+ self.example_user("aaron").id,+ allow_bots=False,+ for_admin=False,)access_user_by_id_including_cross_realm(- self.example_user("cordelia"), self.example_user("aaron").id, for_admin=False+ self.example_user("cordelia"),+ self.example_user("aaron").id,+ allow_bots=False,+ for_admin=False,)def test_access_user_by_id_when_personal_recipient_is_none(self) -> None:@@ -628,12 +646,12 @@ class PermissionTest(ZulipTestCase):)aaron = self.example_user("aaron")- target_user = access_user_by_id(polonius, aaron.id, for_admin=False)+ target_user = access_user_by_id(polonius, aaron.id, allow_bots=False, for_admin=False)self.assertEqual(target_user, aaron)othello = self.example_user("othello")with self.assertRaises(JsonableError):- access_user_by_id(polonius, othello.id, for_admin=False)+ access_user_by_id(polonius, othello.id, allow_bots=False, for_admin=False)def check_property_for_role(self, user_profile: UserProfile, role: int) -> bool:if role == UserProfile.ROLE_REALM_ADMINISTRATOR:diff --git a/zerver/views/presence.py b/zerver/views/presence.pyindex 4d76de0f7f1..cf8f1422f87 100644--- a/zerver/views/presence.py+++ b/zerver/views/presence.py@@ -72,7 +72,7 @@ def get_presence_backend(def get_status_backend(request: HttpRequest, user_profile: UserProfile, user_id: int) -> HttpResponse:- target_user = access_user_by_id(user_profile, user_id, for_admin=False)+ target_user = access_user_by_id(user_profile, user_id, allow_bots=False, for_admin=False)return json_success(request, data={"status": get_user_status(target_user)})@@ -160,7 +160,7 @@ def update_user_status_admin(emoji_code: str | None = None,emoji_type: Annotated[str | None, ApiParamConfig("reaction_type")] = None,) -> HttpResponse:- target_user = access_user_by_id(user_profile, user_id, for_admin=True)+ target_user = access_user_by_id(user_profile, user_id, allow_bots=False, for_admin=True)return update_user_status_backend(request,user_profile=target_user,diff --git a/zerver/views/users.py b/zerver/views/users.pyindex 5620b1ab202..85ea9fbae6f 100644--- a/zerver/views/users.py+++ b/zerver/views/users.py@@ -132,7 +132,7 @@ def deactivate_user_backend(| None = None,user_id: PathOnly[int],) -> HttpResponse:- target = access_user_by_id(user_profile, user_id, for_admin=True)+ target = access_user_by_id(user_profile, user_id, allow_bots=False, for_admin=True)if target.is_realm_owner and not user_profile.is_realm_owner:raise OrganizationOwnerRequiredErrorif check_last_owner(target):