2017-01-30 00:48:45 +01:00
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
from typing import Any, Iterable, List, Mapping, Set, Tuple, Optional
|
2017-01-30 00:48:45 +01:00
|
|
|
|
|
|
|
from django.utils.translation import ugettext as _
|
|
|
|
|
2017-01-30 06:42:09 +01:00
|
|
|
from zerver.lib.actions import check_stream_name, create_streams_if_needed
|
2017-01-30 00:48:45 +01:00
|
|
|
from zerver.lib.request import JsonableError
|
|
|
|
from zerver.models import UserProfile, Stream, Subscription, \
|
2017-10-28 20:26:11 +02:00
|
|
|
Realm, Recipient, bulk_get_recipients, get_stream_recipient, get_stream, \
|
2017-11-14 20:33:09 +01:00
|
|
|
bulk_get_streams, get_realm_stream, DefaultStreamGroup
|
2017-01-30 00:48:45 +01:00
|
|
|
|
2018-12-24 17:04:27 +01:00
|
|
|
def check_for_exactly_one_stream_arg(stream_id: Optional[int], stream: Optional[str]) -> None:
|
|
|
|
if stream_id is None and stream is None:
|
|
|
|
raise JsonableError(_("Please supply 'stream'."))
|
|
|
|
|
|
|
|
if stream_id is not None and stream is not None:
|
|
|
|
raise JsonableError(_("Please choose one: 'stream' or 'stream_id'."))
|
|
|
|
|
2018-02-12 16:02:19 +01:00
|
|
|
def access_stream_for_delete_or_update(user_profile: UserProfile, stream_id: int) -> Stream:
|
2017-08-22 21:41:08 +02:00
|
|
|
|
|
|
|
# We should only ever use this for realm admins, who are allowed
|
2018-02-12 16:02:19 +01:00
|
|
|
# to delete or update all streams on their realm, even private streams
|
|
|
|
# to which they are not subscribed. We do an assert here, because
|
2017-08-22 21:41:08 +02:00
|
|
|
# all callers should have the require_realm_admin decorator.
|
|
|
|
assert(user_profile.is_realm_admin)
|
|
|
|
|
|
|
|
error = _("Invalid stream id")
|
|
|
|
try:
|
|
|
|
stream = Stream.objects.get(id=stream_id)
|
|
|
|
except Stream.DoesNotExist:
|
|
|
|
raise JsonableError(error)
|
|
|
|
|
|
|
|
if stream.realm_id != user_profile.realm_id:
|
|
|
|
raise JsonableError(error)
|
|
|
|
|
|
|
|
return stream
|
|
|
|
|
2018-02-14 17:59:01 +01:00
|
|
|
# Only set allow_realm_admin flag to True when you want to allow realm admin to
|
|
|
|
# access unsubscribed private stream content.
|
2017-11-05 11:15:10 +01:00
|
|
|
def access_stream_common(user_profile: UserProfile, stream: Stream,
|
2018-05-11 01:40:23 +02:00
|
|
|
error: str,
|
2018-02-14 17:59:01 +01:00
|
|
|
require_active: bool=True,
|
2018-03-22 18:52:40 +01:00
|
|
|
allow_realm_admin: bool=False) -> Tuple[Recipient, Optional[Subscription]]:
|
2017-01-30 00:48:45 +01:00
|
|
|
"""Common function for backend code where the target use attempts to
|
|
|
|
access the target stream, returning all the data fetched along the
|
|
|
|
way. If that user does not have permission to access that stream,
|
|
|
|
we throw an exception. A design goal is that the error message is
|
|
|
|
the same for streams you can't access and streams that don't exist."""
|
|
|
|
|
|
|
|
# First, we don't allow any access to streams in other realms.
|
|
|
|
if stream.realm_id != user_profile.realm_id:
|
|
|
|
raise JsonableError(error)
|
|
|
|
|
2017-10-28 20:26:11 +02:00
|
|
|
recipient = get_stream_recipient(stream.id)
|
2017-01-30 00:48:45 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
sub = Subscription.objects.get(user_profile=user_profile,
|
|
|
|
recipient=recipient,
|
2017-11-29 23:35:33 +01:00
|
|
|
active=require_active)
|
2017-01-30 00:48:45 +01:00
|
|
|
except Subscription.DoesNotExist:
|
|
|
|
sub = None
|
|
|
|
|
|
|
|
# If the stream is in your realm and public, you can access it.
|
2018-05-02 17:00:06 +02:00
|
|
|
if stream.is_public() and not user_profile.is_guest:
|
2017-01-30 00:48:45 +01:00
|
|
|
return (recipient, sub)
|
|
|
|
|
|
|
|
# Or if you are subscribed to the stream, you can access it.
|
|
|
|
if sub is not None:
|
|
|
|
return (recipient, sub)
|
|
|
|
|
2018-02-14 17:59:01 +01:00
|
|
|
# For some specific callers (e.g. getting list of subscribers,
|
|
|
|
# removing other users from a stream, and updating stream name and
|
|
|
|
# description), we allow realm admins to access stream even if
|
|
|
|
# they are not subscribed to a private stream.
|
|
|
|
if user_profile.is_realm_admin and allow_realm_admin:
|
|
|
|
return (recipient, sub)
|
|
|
|
|
2017-01-30 00:48:45 +01:00
|
|
|
# Otherwise it is a private stream and you're not on it, so throw
|
|
|
|
# an error.
|
|
|
|
raise JsonableError(error)
|
|
|
|
|
2017-11-29 23:35:33 +01:00
|
|
|
def access_stream_by_id(user_profile: UserProfile,
|
|
|
|
stream_id: int,
|
2018-02-14 17:59:01 +01:00
|
|
|
require_active: bool=True,
|
2018-03-22 18:52:40 +01:00
|
|
|
allow_realm_admin: bool=False) -> Tuple[Stream, Recipient, Optional[Subscription]]:
|
2018-04-27 15:50:57 +02:00
|
|
|
stream = get_stream_by_id(stream_id)
|
2017-01-30 00:48:45 +01:00
|
|
|
|
2018-04-27 15:50:57 +02:00
|
|
|
error = _("Invalid stream id")
|
2017-11-29 23:35:33 +01:00
|
|
|
(recipient, sub) = access_stream_common(user_profile, stream, error,
|
2018-02-14 17:59:01 +01:00
|
|
|
require_active=require_active,
|
|
|
|
allow_realm_admin=allow_realm_admin)
|
2017-01-30 00:48:45 +01:00
|
|
|
return (stream, recipient, sub)
|
|
|
|
|
2018-04-27 15:50:57 +02:00
|
|
|
def get_stream_by_id(stream_id: int) -> Stream:
|
|
|
|
error = _("Invalid stream id")
|
|
|
|
try:
|
|
|
|
stream = Stream.objects.get(id=stream_id)
|
|
|
|
except Stream.DoesNotExist:
|
|
|
|
raise JsonableError(error)
|
|
|
|
return stream
|
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def check_stream_name_available(realm: Realm, name: str) -> None:
|
2017-01-30 06:42:09 +01:00
|
|
|
check_stream_name(name)
|
2017-03-23 07:22:28 +01:00
|
|
|
try:
|
|
|
|
get_stream(name, realm)
|
2018-01-08 19:54:19 +01:00
|
|
|
raise JsonableError(_("Stream name '%s' is already taken.") % (name,))
|
2017-03-23 07:22:28 +01:00
|
|
|
except Stream.DoesNotExist:
|
|
|
|
pass
|
2017-01-30 06:42:09 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def access_stream_by_name(user_profile: UserProfile,
|
2018-08-07 14:13:58 +02:00
|
|
|
stream_name: str,
|
|
|
|
allow_realm_admin: bool=False) -> Tuple[Stream, Recipient, Optional[Subscription]]:
|
2019-04-20 03:49:03 +02:00
|
|
|
error = _("Invalid stream name '%s'") % (stream_name,)
|
2017-03-23 07:22:28 +01:00
|
|
|
try:
|
2017-10-22 02:02:27 +02:00
|
|
|
stream = get_realm_stream(stream_name, user_profile.realm_id)
|
2017-03-23 07:22:28 +01:00
|
|
|
except Stream.DoesNotExist:
|
2017-01-30 00:48:45 +01:00
|
|
|
raise JsonableError(error)
|
|
|
|
|
2018-08-07 14:13:58 +02:00
|
|
|
(recipient, sub) = access_stream_common(user_profile, stream, error,
|
|
|
|
allow_realm_admin=allow_realm_admin)
|
2017-01-30 00:48:45 +01:00
|
|
|
return (stream, recipient, sub)
|
2017-01-30 02:57:24 +01:00
|
|
|
|
2018-12-24 17:04:27 +01:00
|
|
|
def access_stream_for_unmute_topic_by_name(user_profile: UserProfile,
|
|
|
|
stream_name: str,
|
|
|
|
error: str) -> Stream:
|
2017-08-30 02:19:34 +02:00
|
|
|
"""
|
|
|
|
It may seem a little silly to have this helper function for unmuting
|
|
|
|
topics, but it gets around a linter warning, and it helps to be able
|
|
|
|
to review all security-related stuff in one place.
|
|
|
|
|
|
|
|
Our policy for accessing streams when you unmute a topic is that you
|
|
|
|
don't necessarily need to have an active subscription or even "legal"
|
|
|
|
access to the stream. Instead, we just verify the stream_id has been
|
|
|
|
muted in the past (not here, but in the caller).
|
|
|
|
|
|
|
|
Long term, we'll probably have folks just pass us in the id of the
|
|
|
|
MutedTopic row to unmute topics.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
stream = get_stream(stream_name, user_profile.realm)
|
|
|
|
except Stream.DoesNotExist:
|
|
|
|
raise JsonableError(error)
|
|
|
|
return stream
|
|
|
|
|
2018-12-24 17:04:27 +01:00
|
|
|
def access_stream_for_unmute_topic_by_id(user_profile: UserProfile,
|
|
|
|
stream_id: int,
|
|
|
|
error: str) -> Stream:
|
|
|
|
try:
|
|
|
|
stream = Stream.objects.get(id=stream_id, realm_id=user_profile.realm_id)
|
|
|
|
except Stream.DoesNotExist:
|
|
|
|
raise JsonableError(error)
|
|
|
|
return stream
|
|
|
|
|
2018-05-11 01:40:23 +02:00
|
|
|
def can_access_stream_history_by_name(user_profile: UserProfile, stream_name: str) -> bool:
|
2018-04-05 00:10:01 +02:00
|
|
|
"""Determine whether the provided user is allowed to access the
|
|
|
|
history of the target stream. The stream is specified by name.
|
|
|
|
|
|
|
|
This is used by the caller to determine whether this user can get
|
|
|
|
historical messages before they joined for a narrowing search.
|
2017-08-15 18:58:29 +02:00
|
|
|
|
|
|
|
Because of the way our search is currently structured,
|
|
|
|
we may be passed an invalid stream here. We return
|
|
|
|
False in that situation, and subsequent code will do
|
|
|
|
validation and raise the appropriate JsonableError.
|
|
|
|
|
|
|
|
Note that this function should only be used in contexts where
|
|
|
|
access_stream is being called elsewhere to confirm that the user
|
|
|
|
can actually see this stream.
|
|
|
|
"""
|
|
|
|
try:
|
2018-04-05 00:10:01 +02:00
|
|
|
stream = get_stream(stream_name, user_profile.realm)
|
2017-08-15 18:58:29 +02:00
|
|
|
except Stream.DoesNotExist:
|
|
|
|
return False
|
2018-04-05 00:28:14 +02:00
|
|
|
|
2018-05-02 17:00:06 +02:00
|
|
|
if stream.is_history_realm_public() and not user_profile.is_guest:
|
2018-04-05 00:28:14 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
if stream.is_history_public_to_subscribers():
|
|
|
|
# In this case, we check if the user is subscribed.
|
2019-04-20 03:49:03 +02:00
|
|
|
error = _("Invalid stream name '%s'") % (stream_name,)
|
2018-04-05 00:28:14 +02:00
|
|
|
try:
|
|
|
|
(recipient, sub) = access_stream_common(user_profile, stream, error)
|
|
|
|
except JsonableError:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
return False
|
2017-08-15 18:58:29 +02:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def filter_stream_authorization(user_profile: UserProfile,
|
|
|
|
streams: Iterable[Stream]) -> Tuple[List[Stream], List[Stream]]:
|
2017-05-07 17:12:29 +02:00
|
|
|
streams_subscribed = set() # type: Set[int]
|
2017-01-30 02:57:24 +01:00
|
|
|
recipients_map = bulk_get_recipients(Recipient.STREAM, [stream.id for stream in streams])
|
|
|
|
subs = Subscription.objects.filter(user_profile=user_profile,
|
|
|
|
recipient__in=list(recipients_map.values()),
|
|
|
|
active=True)
|
|
|
|
|
|
|
|
for sub in subs:
|
|
|
|
streams_subscribed.add(sub.recipient.type_id)
|
|
|
|
|
2017-05-07 17:12:29 +02:00
|
|
|
unauthorized_streams = [] # type: List[Stream]
|
2017-01-30 02:57:24 +01:00
|
|
|
for stream in streams:
|
2017-07-05 11:50:47 +02:00
|
|
|
# The user is authorized for their own streams
|
2017-01-30 02:57:24 +01:00
|
|
|
if stream.id in streams_subscribed:
|
|
|
|
continue
|
|
|
|
|
2018-05-02 17:00:06 +02:00
|
|
|
# Users are not authorized for invite_only streams, and guest
|
|
|
|
# users are not authorized for any streams
|
|
|
|
if stream.invite_only or user_profile.is_guest:
|
2017-01-30 02:57:24 +01:00
|
|
|
unauthorized_streams.append(stream)
|
|
|
|
|
|
|
|
authorized_streams = [stream for stream in streams if
|
|
|
|
stream.id not in set(stream.id for stream in unauthorized_streams)]
|
|
|
|
return authorized_streams, unauthorized_streams
|
2017-01-30 03:02:40 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def list_to_streams(streams_raw: Iterable[Mapping[str, Any]],
|
|
|
|
user_profile: UserProfile,
|
|
|
|
autocreate: bool=False) -> Tuple[List[Stream], List[Stream]]:
|
2017-01-30 03:02:40 +01:00
|
|
|
"""Converts list of dicts to a list of Streams, validating input in the process
|
|
|
|
|
|
|
|
For each stream name, we validate it to ensure it meets our
|
2017-01-30 07:01:19 +01:00
|
|
|
requirements for a proper stream name using check_stream_name.
|
2017-01-30 03:02:40 +01:00
|
|
|
|
|
|
|
This function in autocreate mode should be atomic: either an exception will be raised
|
|
|
|
during a precheck, or all the streams specified will have been created if applicable.
|
|
|
|
|
|
|
|
@param streams_raw The list of stream dictionaries to process;
|
|
|
|
names should already be stripped of whitespace by the caller.
|
|
|
|
@param user_profile The user for whom we are retreiving the streams
|
|
|
|
@param autocreate Whether we should create streams if they don't already exist
|
|
|
|
"""
|
|
|
|
# Validate all streams, getting extant ones, then get-or-creating the rest.
|
|
|
|
|
|
|
|
stream_set = set(stream_dict["name"] for stream_dict in streams_raw)
|
|
|
|
|
|
|
|
for stream_name in stream_set:
|
|
|
|
# Stream names should already have been stripped by the
|
|
|
|
# caller, but it makes sense to verify anyway.
|
|
|
|
assert stream_name == stream_name.strip()
|
2017-01-30 07:01:19 +01:00
|
|
|
check_stream_name(stream_name)
|
2017-01-30 03:02:40 +01:00
|
|
|
|
2017-05-07 17:12:29 +02:00
|
|
|
existing_streams = [] # type: List[Stream]
|
|
|
|
missing_stream_dicts = [] # type: List[Mapping[str, Any]]
|
2017-01-30 03:02:40 +01:00
|
|
|
existing_stream_map = bulk_get_streams(user_profile.realm, stream_set)
|
|
|
|
|
2019-01-24 09:16:35 +01:00
|
|
|
member_creating_announcement_only_stream = False
|
|
|
|
|
2017-01-30 03:02:40 +01:00
|
|
|
for stream_dict in streams_raw:
|
|
|
|
stream_name = stream_dict["name"]
|
|
|
|
stream = existing_stream_map.get(stream_name.lower())
|
|
|
|
if stream is None:
|
2019-01-24 09:16:35 +01:00
|
|
|
if stream_dict.get("is_announcement_only", False) and not user_profile.is_realm_admin:
|
|
|
|
member_creating_announcement_only_stream = True
|
2017-01-30 03:02:40 +01:00
|
|
|
missing_stream_dicts.append(stream_dict)
|
|
|
|
else:
|
|
|
|
existing_streams.append(stream)
|
|
|
|
|
|
|
|
if len(missing_stream_dicts) == 0:
|
|
|
|
# This is the happy path for callers who expected all of these
|
|
|
|
# streams to exist already.
|
2017-05-07 17:12:29 +02:00
|
|
|
created_streams = [] # type: List[Stream]
|
2017-01-30 03:02:40 +01:00
|
|
|
else:
|
|
|
|
# autocreate=True path starts here
|
|
|
|
if not user_profile.can_create_streams():
|
|
|
|
raise JsonableError(_('User cannot create streams.'))
|
|
|
|
elif not autocreate:
|
|
|
|
raise JsonableError(_("Stream(s) (%s) do not exist") % ", ".join(
|
|
|
|
stream_dict["name"] for stream_dict in missing_stream_dicts))
|
2019-01-24 09:16:35 +01:00
|
|
|
elif member_creating_announcement_only_stream:
|
|
|
|
raise JsonableError(_('User cannot create a stream with these settings.'))
|
2017-01-30 03:02:40 +01:00
|
|
|
|
|
|
|
# We already filtered out existing streams, so dup_streams
|
|
|
|
# will normally be an empty list below, but we protect against somebody
|
|
|
|
# else racing to create the same stream. (This is not an entirely
|
|
|
|
# paranoid approach, since often on Zulip two people will discuss
|
|
|
|
# creating a new stream, and both people eagerly do it.)
|
|
|
|
created_streams, dup_streams = create_streams_if_needed(realm=user_profile.realm,
|
|
|
|
stream_dicts=missing_stream_dicts)
|
|
|
|
existing_streams += dup_streams
|
|
|
|
|
|
|
|
return existing_streams, created_streams
|
2017-11-14 20:33:09 +01:00
|
|
|
|
|
|
|
def access_default_stream_group_by_id(realm: Realm, group_id: int) -> DefaultStreamGroup:
|
|
|
|
try:
|
|
|
|
return DefaultStreamGroup.objects.get(realm=realm, id=group_id)
|
|
|
|
except DefaultStreamGroup.DoesNotExist:
|
2019-04-20 03:49:03 +02:00
|
|
|
raise JsonableError(_("Default stream group with id '%s' does not exist.") % (group_id,))
|