From db4cbcd438093546ee96c7ae808c6475eabc0b46 Mon Sep 17 00:00:00 2001 From: Jessica McKellar Date: Thu, 30 Jan 2014 14:29:18 -0500 Subject: [PATCH] Make /json/subscriptions/remove support principals. Admins will use this to unsubscribe other people from streams. (imported from commit 5b42d5852ddcfa2c8776482c45471828c4daf453) --- zerver/views/__init__.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/zerver/views/__init__.py b/zerver/views/__init__.py index 67f735455e..33aafaa115 100644 --- a/zerver/views/__init__.py +++ b/zerver/views/__init__.py @@ -1143,12 +1143,34 @@ def json_remove_subscriptions(request, user_profile): @has_request_variables def remove_subscriptions_backend(request, user_profile, - streams_raw = REQ("subscriptions", validator=check_list(check_string))): + streams_raw = REQ("subscriptions", validator=check_list(check_string)), + principals = REQ(validator=check_list(check_string), default=None)): + + removing_someone_else = principals and \ + set(principals) != set((user_profile.email,)) + if removing_someone_else and not user_profile.is_admin(): + # You can only unsubscribe other people from a stream if you are a realm + # admin. + return json_error("This action requires administrative rights") streams, _ = list_to_streams(streams_raw, user_profile) + for stream in streams: + if removing_someone_else and stream.invite_only and \ + not subscribed_to_stream(user_profile, stream): + # Even as an admin, you can't remove other people from an + # invite-only stream you're not on. + return json_error("Cannot administer invite-only streams this way") + + if principals: + people_to_unsub = set(principal_to_user_profile( + user_profile, principal) for principal in principals) + else: + people_to_unsub = [user_profile] + result = dict(removed=[], not_subscribed=[]) - (removed, not_subscribed) = bulk_remove_subscriptions([user_profile], streams) + (removed, not_subscribed) = bulk_remove_subscriptions(people_to_unsub, streams) + for (subscriber, stream) in removed: result["removed"].append(stream.name) for (subscriber, stream) in not_subscribed: