2020-06-11 00:54:34 +02:00
|
|
|
from typing import Any, Dict, List
|
|
|
|
|
2017-09-25 09:47:15 +02:00
|
|
|
from django.db import transaction
|
2021-04-16 00:57:30 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2017-11-01 10:04:16 +01:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2020-06-11 00:54:34 +02:00
|
|
|
from zerver.models import Realm, UserGroup, UserGroupMembership, UserProfile
|
|
|
|
|
2017-09-25 09:47:15 +02:00
|
|
|
|
2021-08-06 15:22:08 +02:00
|
|
|
def access_user_group_by_id(
|
|
|
|
user_group_id: int, user_profile: UserProfile, for_mention: bool = False
|
|
|
|
) -> UserGroup:
|
2017-11-01 10:04:16 +01:00
|
|
|
try:
|
2018-02-19 13:38:18 +01:00
|
|
|
user_group = UserGroup.objects.get(id=user_group_id, realm=user_profile.realm)
|
2021-08-06 15:22:08 +02:00
|
|
|
if not for_mention and user_group.is_system_group:
|
|
|
|
raise JsonableError(_("Insufficient permission"))
|
2018-02-19 13:38:18 +01:00
|
|
|
group_member_ids = get_user_group_members(user_group)
|
2021-05-21 07:02:43 +02:00
|
|
|
if (
|
|
|
|
not user_profile.is_realm_admin
|
|
|
|
and not user_profile.is_moderator
|
|
|
|
and user_profile.id not in group_member_ids
|
|
|
|
):
|
2021-06-30 09:13:19 +02:00
|
|
|
raise JsonableError(_("Insufficient permission"))
|
2017-11-01 10:04:16 +01:00
|
|
|
except UserGroup.DoesNotExist:
|
|
|
|
raise JsonableError(_("Invalid user group"))
|
|
|
|
return user_group
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-05-10 19:13:36 +02:00
|
|
|
def user_groups_in_realm_serialized(realm: Realm) -> List[Dict[str, Any]]:
|
2017-11-30 01:09:23 +01:00
|
|
|
"""This function is used in do_events_register code path so this code
|
|
|
|
should be performant. We need to do 2 database queries because
|
|
|
|
Django's ORM doesn't properly support the left join between
|
|
|
|
UserGroup and UserGroupMembership that we need.
|
2017-11-07 07:56:26 +01:00
|
|
|
"""
|
2017-11-30 01:09:23 +01:00
|
|
|
realm_groups = UserGroup.objects.filter(realm=realm)
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
group_dicts: Dict[str, Any] = {}
|
2017-11-30 01:09:23 +01:00
|
|
|
for user_group in realm_groups:
|
|
|
|
group_dicts[user_group.id] = dict(
|
|
|
|
id=user_group.id,
|
|
|
|
name=user_group.name,
|
|
|
|
description=user_group.description,
|
|
|
|
members=[],
|
2021-08-06 19:31:00 +02:00
|
|
|
is_system_group=user_group.is_system_group,
|
2017-11-30 01:09:23 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
membership = UserGroupMembership.objects.filter(user_group__realm=realm).values_list(
|
2021-02-12 08:20:45 +01:00
|
|
|
"user_group_id", "user_profile_id"
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2017-11-30 01:09:23 +01:00
|
|
|
for (user_group_id, user_profile_id) in membership:
|
2021-02-12 08:20:45 +01:00
|
|
|
group_dicts[user_group_id]["members"].append(user_profile_id)
|
2017-11-30 01:09:23 +01:00
|
|
|
for group_dict in group_dicts.values():
|
2021-02-12 08:20:45 +01:00
|
|
|
group_dict["members"] = sorted(group_dict["members"])
|
2017-11-30 01:09:23 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
return sorted(group_dicts.values(), key=lambda group_dict: group_dict["id"])
|
2017-11-07 07:56:26 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def get_user_groups(user_profile: UserProfile) -> List[UserGroup]:
|
2017-09-25 09:47:15 +02:00
|
|
|
return list(user_profile.usergroup_set.all())
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-05 11:15:10 +01:00
|
|
|
def remove_user_from_user_group(user_profile: UserProfile, user_group: UserGroup) -> int:
|
2017-09-25 09:47:15 +02:00
|
|
|
num_deleted, _ = UserGroupMembership.objects.filter(
|
2021-02-12 08:19:30 +01:00
|
|
|
user_profile=user_profile, user_group=user_group
|
|
|
|
).delete()
|
2017-09-25 09:47:15 +02:00
|
|
|
return num_deleted
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
|
|
|
def create_user_group(
|
2021-08-06 15:22:08 +02:00
|
|
|
name: str,
|
|
|
|
members: List[UserProfile],
|
|
|
|
realm: Realm,
|
|
|
|
*,
|
|
|
|
description: str = "",
|
|
|
|
is_system_group: bool = False,
|
2021-02-12 08:19:30 +01:00
|
|
|
) -> UserGroup:
|
2017-09-25 09:47:15 +02:00
|
|
|
with transaction.atomic():
|
2021-08-06 15:22:08 +02:00
|
|
|
user_group = UserGroup.objects.create(
|
|
|
|
name=name, realm=realm, description=description, is_system_group=is_system_group
|
|
|
|
)
|
2020-09-02 06:20:26 +02:00
|
|
|
UserGroupMembership.objects.bulk_create(
|
2021-02-12 08:19:30 +01:00
|
|
|
UserGroupMembership(user_profile=member, user_group=user_group) for member in members
|
2020-09-02 06:20:26 +02:00
|
|
|
)
|
2017-09-25 09:47:15 +02:00
|
|
|
return user_group
|
2017-11-02 08:53:30 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2018-02-19 13:38:18 +01:00
|
|
|
def get_user_group_members(user_group: UserGroup) -> List[UserProfile]:
|
|
|
|
members = UserGroupMembership.objects.filter(user_group=user_group)
|
|
|
|
return [member.user_profile.id for member in members]
|
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-11-27 05:27:04 +01:00
|
|
|
def get_memberships_of_users(user_group: UserGroup, members: List[UserProfile]) -> List[int]:
|
2021-02-12 08:19:30 +01:00
|
|
|
return list(
|
|
|
|
UserGroupMembership.objects.filter(
|
|
|
|
user_group=user_group, user_profile__in=members
|
2021-02-12 08:20:45 +01:00
|
|
|
).values_list("user_profile_id", flat=True)
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|