From 41fbb16cdfdb737ed3f57428ce2720d332ef68a1 Mon Sep 17 00:00:00 2001 From: Yashashvi Dave Date: Wed, 17 Oct 2018 18:34:58 +0530 Subject: [PATCH] org settings: Fix error when admin update realm users full names. Currently, if there is only one admin in realm and admin tries to updates any non-adminuser's full name it throws error, "Cannot remove only realm admin". Because in `/json/users/` api check_if_last_admin_is_changed is checked even if property is_admin is not changed. This commit fix this issue and add tests for it. --- zerver/tests/test_users.py | 3 +++ zerver/views/users.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/zerver/tests/test_users.py b/zerver/tests/test_users.py index fa61ab4b3c..9b2a62a5a2 100644 --- a/zerver/tests/test_users.py +++ b/zerver/tests/test_users.py @@ -173,6 +173,9 @@ class PermissionTest(ZulipTestCase): self.assert_json_success(result) hamlet = self.example_user('hamlet') self.assertEqual(hamlet.full_name, new_name) + req['is_admin'] = ujson.dumps(False) + result = self.client_patch('/json/users/{}'.format(hamlet.id), req) + self.assert_json_success(result) def test_non_admin_cannot_change_full_name(self) -> None: self.login(self.example_email("hamlet")) diff --git a/zerver/views/users.py b/zerver/views/users.py index ea7d0199f8..26ccbabd7e 100644 --- a/zerver/views/users.py +++ b/zerver/views/users.py @@ -81,7 +81,7 @@ def update_user_backend(request: HttpRequest, user_profile: UserProfile, user_id is_admin: Optional[bool]=REQ(default=None, validator=check_bool)) -> HttpResponse: target = access_user_by_id(user_profile, user_id, allow_deactivated=True, allow_bots=True) - if is_admin is not None: + if is_admin is not None and target.is_realm_admin != is_admin: if not is_admin and check_last_admin(user_profile): return json_error(_('Cannot remove the only organization administrator')) do_change_is_admin(target, is_admin)