users: Give moderators same permissions as that of full members.

This commit updates the stream creation, subscribing others to
stream, wildcard mention settings and stream post policy to allow
realm moderators even if they are new and the respective setting
is set to allow full members only.
This commit is contained in:
sahil839 2020-12-28 21:44:26 +05:30 committed by Tim Abbott
parent b4fd15d516
commit 4ca21a6982
3 changed files with 38 additions and 1 deletions

View File

@ -1337,6 +1337,8 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):
@property @property
def is_provisional_member(self) -> bool: def is_provisional_member(self) -> bool:
if self.is_moderator:
return False
diff = (timezone_now() - self.date_joined).days diff = (timezone_now() - self.date_joined).days
if diff < self.realm.waiting_period_threshold: if diff < self.realm.waiting_period_threshold:
return True return True
@ -1379,6 +1381,10 @@ class UserProfile(AbstractBaseUser, PermissionsMixin):
# ROLE_REALM_ADMINISTRATOR to ROLE_MEMBER here. # ROLE_REALM_ADMINISTRATOR to ROLE_MEMBER here.
self.role = UserProfile.ROLE_MEMBER self.role = UserProfile.ROLE_MEMBER
@property
def is_moderator(self) -> bool:
return self.role == UserProfile.ROLE_MODERATOR
@property @property
def is_incoming_webhook(self) -> bool: def is_incoming_webhook(self) -> bool:
return self.bot_type == UserProfile.INCOMING_WEBHOOK_BOT return self.bot_type == UserProfile.INCOMING_WEBHOOK_BOT

View File

@ -304,6 +304,24 @@ class MessagePOSTTest(ZulipTestCase):
bot_without_owner, stream_name, "New members cannot send to this stream." bot_without_owner, stream_name, "New members cannot send to this stream."
) )
moderator_profile = self.example_user("shiva")
moderator_profile.date_joined = timezone_now() - datetime.timedelta(days=9)
moderator_profile.save()
self.assertTrue(moderator_profile.is_moderator)
self.assertFalse(moderator_profile.is_provisional_member)
# Moderators and their owned bots can send to STREAM_POST_POLICY_RESTRICT_NEW_MEMBERS
# streams, even if the admin is a new user
self._send_and_verify_message(moderator_profile, stream_name)
moderator_owned_bot = self.create_test_bot(
short_name="whatever3",
full_name="whatever3",
user_profile=moderator_profile,
)
moderator_owned_bot.date_joined = timezone_now() - datetime.timedelta(days=11)
moderator_owned_bot.save()
self._send_and_verify_message(moderator_owned_bot, stream_name)
# Cross realm bots should be allowed # Cross realm bots should be allowed
notification_bot = get_system_bot("notification-bot@zulip.com") notification_bot = get_system_bot("notification-bot@zulip.com")
internal_send_stream_message( internal_send_stream_message(
@ -1600,12 +1618,14 @@ class StreamMessagesTest(ZulipTestCase):
cordelia = self.example_user("cordelia") cordelia = self.example_user("cordelia")
iago = self.example_user("iago") iago = self.example_user("iago")
polonius = self.example_user("polonius") polonius = self.example_user("polonius")
shiva = self.example_user("shiva")
realm = cordelia.realm realm = cordelia.realm
stream_name = "test_stream" stream_name = "test_stream"
self.subscribe(cordelia, stream_name) self.subscribe(cordelia, stream_name)
self.subscribe(iago, stream_name) self.subscribe(iago, stream_name)
self.subscribe(polonius, stream_name) self.subscribe(polonius, stream_name)
self.subscribe(shiva, stream_name)
do_set_realm_property( do_set_realm_property(
realm, "wildcard_mention_policy", Realm.WILDCARD_MENTION_POLICY_EVERYONE realm, "wildcard_mention_policy", Realm.WILDCARD_MENTION_POLICY_EVERYONE
@ -1626,12 +1646,15 @@ class StreamMessagesTest(ZulipTestCase):
do_set_realm_property(realm, "waiting_period_threshold", 10) do_set_realm_property(realm, "waiting_period_threshold", 10)
iago.date_joined = timezone_now() iago.date_joined = timezone_now()
iago.save() iago.save()
shiva.date_joined = timezone_now()
shiva.save()
cordelia.date_joined = timezone_now() cordelia.date_joined = timezone_now()
cordelia.save() cordelia.save()
self.send_and_verify_wildcard_mention_message("cordelia", test_fails=True) self.send_and_verify_wildcard_mention_message("cordelia", test_fails=True)
self.send_and_verify_wildcard_mention_message("cordelia", sub_count=10) self.send_and_verify_wildcard_mention_message("cordelia", sub_count=10)
# Administrators can use wildcard mentions even if they are new. # Administrators and moderators can use wildcard mentions even if they are new.
self.send_and_verify_wildcard_mention_message("iago") self.send_and_verify_wildcard_mention_message("iago")
self.send_and_verify_wildcard_mention_message("shiva")
cordelia.date_joined = timezone_now() - datetime.timedelta(days=11) cordelia.date_joined = timezone_now() - datetime.timedelta(days=11)
cordelia.save() cordelia.save()

View File

@ -3223,6 +3223,10 @@ class SubscriptionAPITest(ZulipTestCase):
) )
self.assertFalse(othello.can_create_streams()) self.assertFalse(othello.can_create_streams())
othello.role = UserProfile.ROLE_MODERATOR
self.assertTrue(othello.can_create_streams())
othello.role = UserProfile.ROLE_MEMBER
othello.date_joined = timezone_now() - timedelta( othello.date_joined = timezone_now() - timedelta(
days=(othello.realm.waiting_period_threshold + 1) days=(othello.realm.waiting_period_threshold + 1)
) )
@ -3295,6 +3299,10 @@ class SubscriptionAPITest(ZulipTestCase):
) )
self.assertFalse(othello.can_subscribe_other_users()) self.assertFalse(othello.can_subscribe_other_users())
do_change_user_role(othello, UserProfile.ROLE_MODERATOR)
self.assertTrue(othello.can_subscribe_other_users())
do_change_user_role(othello, UserProfile.ROLE_MEMBER)
othello.date_joined = timezone_now() - timedelta( othello.date_joined = timezone_now() - timedelta(
days=(othello.realm.waiting_period_threshold + 1) days=(othello.realm.waiting_period_threshold + 1)
) )