Refactor send_message_backend to do checking in new function.

This is preparatory for using this new arguments checking function in
internal_send_message as well.

(imported from commit 578e09c50b8a700c019c7dd235b2d9527af34e39)
This commit is contained in:
Tim Abbott 2013-03-18 13:21:22 -04:00
parent 14efaea0bb
commit fb77af8f10
1 changed files with 21 additions and 9 deletions

View File

@ -830,16 +830,10 @@ def send_message_backend(request, user_profile, client,
forged = POST(default=False), forged = POST(default=False),
subject_name = POST('subject', lambda x: x.strip(), None), subject_name = POST('subject', lambda x: x.strip(), None),
message_content = POST('content')): message_content = POST('content')):
stream = None
is_super_user = is_super_user_api(request) is_super_user = is_super_user_api(request)
if forged and not is_super_user: if forged and not is_super_user:
return json_error("User not authorized for this query") return json_error("User not authorized for this query")
if len(message_to) == 0:
return json_error("Message must have recipients.")
if len(message_content) > MAX_MESSAGE_LENGTH:
return json_error("Message too long.")
if client.name == "zephyr_mirror": if client.name == "zephyr_mirror":
# Here's how security works for non-superuser mirroring: # Here's how security works for non-superuser mirroring:
# #
@ -865,6 +859,23 @@ def send_message_backend(request, user_profile, client,
else: else:
sender = user_profile sender = user_profile
return check_send_message(sender, client, message_type_name, message_to,
subject_name, message_content, forged=forged,
forged_timestamp = request.POST.get('time'),
forwarder_user_profile=user_profile)
def check_send_message(sender, client, message_type_name, message_to,
subject_name, message_content, realm=None, forged=False,
forged_timestamp=None, forwarder_user_profile=None):
stream = None
if len(message_to) == 0:
return json_error("Message must have recipients.")
if len(message_content) > MAX_MESSAGE_LENGTH:
return json_error("Message too long.")
if realm is None:
realm = sender.realm
if message_type_name == 'stream': if message_type_name == 'stream':
if len(message_to) > 1: if len(message_to) > 1:
return json_error("Cannot send to multiple streams") return json_error("Cannot send to multiple streams")
@ -888,14 +899,15 @@ def send_message_backend(request, user_profile, client,
# if not valid_stream_name(subject): # if not valid_stream_name(subject):
# return json_error("Invalid subject name") # return json_error("Invalid subject name")
stream = get_stream(stream_name, user_profile.realm) stream = get_stream(stream_name, realm)
if stream is None: if stream is None:
return json_error("Stream does not exist") return json_error("Stream does not exist")
recipient = get_recipient(Recipient.STREAM, stream.id) recipient = get_recipient(Recipient.STREAM, stream.id)
elif message_type_name == 'private': elif message_type_name == 'private':
not_forged_zephyr_mirror = client and client.name == "zephyr_mirror" and not forged not_forged_zephyr_mirror = client and client.name == "zephyr_mirror" and not forged
try: try:
recipient = recipient_for_emails(message_to, not_forged_zephyr_mirror, user_profile, sender) recipient = recipient_for_emails(message_to, not_forged_zephyr_mirror,
forwarder_user_profile, sender)
except ValidationError, e: except ValidationError, e:
return json_error(e.messages[0]) return json_error(e.messages[0])
else: else:
@ -913,7 +925,7 @@ def send_message_backend(request, user_profile, client,
message.subject = subject message.subject = subject
if forged: if forged:
# Forged messages come with a timestamp # Forged messages come with a timestamp
message.pub_date = timestamp_to_datetime(request.POST['time']) message.pub_date = timestamp_to_datetime(forged_timestamp)
else: else:
message.pub_date = now() message.pub_date = now()
message.sending_client = client message.sending_client = client