actions: Record acting_user for subscriptions RealmAuditLog entries.

In most cases, we do have the data for which other user was
responsible for subscribing the target user to new streams.

The main case where we don't is when the user is created and gets the
default streams.
This commit is contained in:
Tim Abbott 2017-07-16 15:40:15 -07:00
parent 6ac9bae5f5
commit 1a51bcd2df
2 changed files with 15 additions and 8 deletions

View File

@ -308,6 +308,7 @@ def process_new_human_user(user_profile, prereg_user=None, newsletter_data=None)
try:
if prereg_user is not None:
streams = prereg_user.streams.all()
acting_user = prereg_user.referred_by # type: Optional[UserProfile]
else:
streams = []
except AttributeError:
@ -318,7 +319,9 @@ def process_new_human_user(user_profile, prereg_user=None, newsletter_data=None)
# add the default streams
if len(streams) == 0:
streams = get_default_subs(user_profile)
bulk_add_subscriptions(streams, [user_profile])
acting_user = None
bulk_add_subscriptions(streams, [user_profile], acting_user=acting_user)
add_new_user_history(user_profile, streams)
@ -1627,8 +1630,8 @@ def query_all_subs_by_stream(streams):
all_subs_by_stream[sub.recipient.type_id].append(sub.user_profile)
return all_subs_by_stream
def bulk_add_subscriptions(streams, users, from_stream_creation=False):
# type: (Iterable[Stream], Iterable[UserProfile], bool) -> Tuple[List[Tuple[UserProfile, Stream]], List[Tuple[UserProfile, Stream]]]
def bulk_add_subscriptions(streams, users, from_stream_creation=False, acting_user=None):
# type: (Iterable[Stream], Iterable[UserProfile], bool, Optional[UserProfile]) -> Tuple[List[Tuple[UserProfile, Stream]], List[Tuple[UserProfile, Stream]]]
recipients_map = bulk_get_recipients(Recipient.STREAM, [stream.id for stream in streams]) # type: Mapping[int, Recipient]
recipients = [recipient.id for recipient in recipients_map.values()] # type: List[int]
@ -1685,6 +1688,7 @@ def bulk_add_subscriptions(streams, users, from_stream_creation=False):
all_subscription_logs = [] # type: (List[RealmAuditLog])
for (sub, stream) in subs_to_add:
all_subscription_logs.append(RealmAuditLog(realm=sub.user_profile.realm,
acting_user=acting_user,
modified_user=sub.user_profile,
modified_stream=stream,
event_last_message_id=0,
@ -1692,6 +1696,7 @@ def bulk_add_subscriptions(streams, users, from_stream_creation=False):
event_time=event_time))
for (sub, stream) in subs_to_activate:
all_subscription_logs.append(RealmAuditLog(realm=sub.user_profile.realm,
acting_user=acting_user,
modified_user=sub.user_profile,
modified_stream=stream,
event_last_message_id=event_last_message_id,
@ -1790,8 +1795,8 @@ def notify_subscriptions_removed(user_profile, streams, no_log=False):
subscriptions=payload)
send_event(event, [user_profile.id])
def bulk_remove_subscriptions(users, streams):
# type: (Iterable[UserProfile], Iterable[Stream]) -> Tuple[List[Tuple[UserProfile, Stream]], List[Tuple[UserProfile, Stream]]]
def bulk_remove_subscriptions(users, streams, acting_user=None):
# type: (Iterable[UserProfile], Iterable[Stream], Optional[UserProfile]) -> Tuple[List[Tuple[UserProfile, Stream]], List[Tuple[UserProfile, Stream]]]
recipients_map = bulk_get_recipients(Recipient.STREAM,
[stream.id for stream in streams]) # type: Mapping[int, Recipient]

View File

@ -177,7 +177,8 @@ def remove_subscriptions_backend(request, user_profile,
people_to_unsub = set([user_profile])
result = dict(removed=[], not_subscribed=[]) # type: Dict[str, List[Text]]
(removed, not_subscribed) = bulk_remove_subscriptions(people_to_unsub, streams)
(removed, not_subscribed) = bulk_remove_subscriptions(people_to_unsub, streams,
acting_user=user_profile)
for (subscriber, stream) in removed:
result["removed"].append(stream.name)
@ -255,7 +256,8 @@ def add_subscriptions_backend(request, user_profile,
else:
subscribers = set([user_profile])
(subscribed, already_subscribed) = bulk_add_subscriptions(streams, subscribers)
(subscribed, already_subscribed) = bulk_add_subscriptions(streams, subscribers,
acting_user=user_profile)
result = dict(subscribed=defaultdict(list), already_subscribed=defaultdict(list)) # type: Dict[str, Any]
for (subscriber, stream) in subscribed:
@ -398,7 +400,7 @@ def json_stream_exists(request, user_profile, stream_name=REQ("stream"),
# So if we're not yet subscribed and autosubscribe is enabled, we
# should join.
if sub is None and autosubscribe:
bulk_add_subscriptions([stream], [user_profile])
bulk_add_subscriptions([stream], [user_profile], acting_user=user_profile)
result["subscribed"] = True
return json_success(result) # results are ignored for HEAD requests