Skip to content

Commit

Permalink
users: Add read-only mode for access_user_by_id.
Browse files Browse the repository at this point in the history
We've be using this in the upcoming GET /users/{id} method.
  • Loading branch information
timabbott committed Feb 7, 2020
1 parent aa9286a commit e39840c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 5 additions & 1 deletion zerver/lib/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ def access_bot_by_id(user_profile: UserProfile, user_id: int) -> UserProfile:
return target

def access_user_by_id(user_profile: UserProfile, user_id: int,
allow_deactivated: bool=False, allow_bots: bool=False) -> UserProfile:
allow_deactivated: bool=False, allow_bots: bool=False,
read_only: bool=False) -> UserProfile:
try:
target = get_user_profile_by_id_in_realm(user_id, user_profile.realm)
except UserProfile.DoesNotExist:
Expand All @@ -207,6 +208,9 @@ def access_user_by_id(user_profile: UserProfile, user_id: int,
raise JsonableError(_("No such user"))
if not target.is_active and not allow_deactivated:
raise JsonableError(_("User is deactivated"))
if read_only:
# Administrative access is not required just to read a user.
return target
if not user_profile.can_admin_user(target):
raise JsonableError(_("Insufficient permission"))
return target
Expand Down
3 changes: 3 additions & 0 deletions zerver/tests/test_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ def test_access_user_by_id(self) -> None:
# Non-admin user can't admin another user
with self.assertRaises(JsonableError):
access_user_by_id(self.example_user("cordelia"), self.example_user("aaron").id)
# But does have read-only access to it.
access_user_by_id(self.example_user("cordelia"), self.example_user("aaron").id,
read_only=True)

def test_change_regular_member_to_guest(self) -> None:
iago = self.example_user("iago")
Expand Down

0 comments on commit e39840c

Please sign in to comment.