2024-07-12 02:30:25 +02:00
|
|
|
from collections.abc import Mapping, Sequence
|
2023-11-19 19:45:19 +01:00
|
|
|
from datetime import datetime
|
2024-07-12 02:30:25 +02:00
|
|
|
from typing import TypedDict
|
2022-04-14 23:30:17 +02:00
|
|
|
|
|
|
|
import django.db.utils
|
|
|
|
from django.db import transaction
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
|
|
|
from zerver.lib.exceptions import JsonableError
|
2024-06-04 12:36:52 +02:00
|
|
|
from zerver.lib.timestamp import datetime_to_timestamp
|
2023-06-12 13:27:47 +02:00
|
|
|
from zerver.lib.user_groups import (
|
2024-04-30 15:16:58 +02:00
|
|
|
AnonymousSettingGroupDict,
|
2024-04-29 05:51:48 +02:00
|
|
|
get_group_setting_value_for_api,
|
2024-05-24 11:24:18 +02:00
|
|
|
get_group_setting_value_for_audit_log_data,
|
2023-06-12 13:27:47 +02:00
|
|
|
get_role_based_system_groups_dict,
|
|
|
|
set_defaults_for_group_settings,
|
|
|
|
)
|
2022-03-01 07:52:47 +01:00
|
|
|
from zerver.models import (
|
|
|
|
GroupGroupMembership,
|
2024-04-16 16:05:43 +02:00
|
|
|
NamedUserGroup,
|
2022-03-01 07:52:47 +01:00
|
|
|
Realm,
|
2022-11-21 05:43:03 +01:00
|
|
|
RealmAuditLog,
|
2022-03-01 07:52:47 +01:00
|
|
|
UserGroup,
|
|
|
|
UserGroupMembership,
|
|
|
|
UserProfile,
|
|
|
|
)
|
2023-12-15 01:55:59 +01:00
|
|
|
from zerver.models.groups import SystemGroups
|
2024-09-06 17:06:03 +02:00
|
|
|
from zerver.models.realm_audit_logs import AuditLogEventType
|
2023-12-15 01:16:00 +01:00
|
|
|
from zerver.models.users import active_user_ids
|
2024-08-09 12:30:44 +02:00
|
|
|
from zerver.tornado.django_api import send_event_on_commit
|
2022-04-14 23:30:17 +02:00
|
|
|
|
|
|
|
|
2022-07-15 18:10:00 +02:00
|
|
|
class MemberGroupUserDict(TypedDict):
|
|
|
|
id: int
|
|
|
|
role: int
|
2023-11-19 19:45:19 +01:00
|
|
|
date_joined: datetime
|
2022-07-15 18:10:00 +02:00
|
|
|
|
|
|
|
|
2024-08-09 12:30:44 +02:00
|
|
|
@transaction.atomic(savepoint=False)
|
2022-12-14 06:53:21 +01:00
|
|
|
def create_user_group_in_database(
|
2022-12-12 04:17:49 +01:00
|
|
|
name: str,
|
2024-07-12 02:30:17 +02:00
|
|
|
members: list[UserProfile],
|
2022-12-12 04:17:49 +01:00
|
|
|
realm: Realm,
|
|
|
|
*,
|
2024-07-12 02:30:23 +02:00
|
|
|
acting_user: UserProfile | None,
|
2022-12-12 04:17:49 +01:00
|
|
|
description: str = "",
|
2023-06-14 16:48:58 +02:00
|
|
|
group_settings_map: Mapping[str, UserGroup] = {},
|
2022-12-12 04:17:49 +01:00
|
|
|
is_system_group: bool = False,
|
2024-04-16 16:05:43 +02:00
|
|
|
) -> NamedUserGroup:
|
|
|
|
user_group = NamedUserGroup(
|
|
|
|
name=name,
|
|
|
|
realm=realm,
|
|
|
|
description=description,
|
|
|
|
is_system_group=is_system_group,
|
|
|
|
realm_for_sharding=realm,
|
2024-06-04 12:36:52 +02:00
|
|
|
creator=acting_user,
|
2022-12-12 04:17:49 +01:00
|
|
|
)
|
2023-06-12 13:27:47 +02:00
|
|
|
|
2023-06-14 16:48:58 +02:00
|
|
|
for setting_name, setting_value in group_settings_map.items():
|
|
|
|
setattr(user_group, setting_name, setting_value)
|
|
|
|
|
|
|
|
system_groups_name_dict = get_role_based_system_groups_dict(realm)
|
|
|
|
user_group = set_defaults_for_group_settings(
|
|
|
|
user_group, group_settings_map, system_groups_name_dict
|
|
|
|
)
|
2023-06-12 13:27:47 +02:00
|
|
|
user_group.save()
|
|
|
|
|
2022-12-12 04:17:49 +01:00
|
|
|
UserGroupMembership.objects.bulk_create(
|
|
|
|
UserGroupMembership(user_profile=member, user_group=user_group) for member in members
|
|
|
|
)
|
2022-11-21 05:43:03 +01:00
|
|
|
|
|
|
|
creation_time = timezone_now()
|
|
|
|
audit_log_entries = [
|
|
|
|
RealmAuditLog(
|
|
|
|
realm=realm,
|
|
|
|
acting_user=acting_user,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_CREATED,
|
2022-11-21 05:43:03 +01:00
|
|
|
event_time=creation_time,
|
|
|
|
modified_user_group=user_group,
|
2023-06-05 22:34:52 +02:00
|
|
|
),
|
2022-11-21 05:43:03 +01:00
|
|
|
] + [
|
|
|
|
RealmAuditLog(
|
|
|
|
realm=realm,
|
|
|
|
acting_user=acting_user,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_ADDED,
|
2022-11-21 05:43:03 +01:00
|
|
|
event_time=creation_time,
|
|
|
|
modified_user=member,
|
|
|
|
modified_user_group=user_group,
|
|
|
|
)
|
|
|
|
for member in members
|
|
|
|
]
|
|
|
|
RealmAuditLog.objects.bulk_create(audit_log_entries)
|
2022-12-12 04:17:49 +01:00
|
|
|
return user_group
|
|
|
|
|
|
|
|
|
2022-04-14 23:30:17 +02:00
|
|
|
@transaction.atomic(savepoint=False)
|
|
|
|
def update_users_in_full_members_system_group(
|
2024-07-12 02:30:23 +02:00
|
|
|
realm: Realm, affected_user_ids: Sequence[int] = [], *, acting_user: UserProfile | None
|
2022-04-14 23:30:17 +02:00
|
|
|
) -> None:
|
2024-04-17 16:34:39 +02:00
|
|
|
full_members_system_group = NamedUserGroup.objects.get(
|
2023-09-21 13:06:39 +02:00
|
|
|
realm=realm, name=SystemGroups.FULL_MEMBERS, is_system_group=True
|
2022-04-14 23:30:17 +02:00
|
|
|
)
|
2024-04-17 16:34:39 +02:00
|
|
|
members_system_group = NamedUserGroup.objects.get(
|
2023-09-21 13:06:39 +02:00
|
|
|
realm=realm, name=SystemGroups.MEMBERS, is_system_group=True
|
2022-04-14 23:30:17 +02:00
|
|
|
)
|
|
|
|
|
2024-07-12 02:30:17 +02:00
|
|
|
full_member_group_users: list[MemberGroupUserDict] = list()
|
|
|
|
member_group_users: list[MemberGroupUserDict] = list()
|
2022-04-14 23:30:17 +02:00
|
|
|
|
|
|
|
if affected_user_ids:
|
|
|
|
full_member_group_users = list(
|
|
|
|
full_members_system_group.direct_members.filter(id__in=affected_user_ids).values(
|
|
|
|
"id", "role", "date_joined"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
member_group_users = list(
|
|
|
|
members_system_group.direct_members.filter(id__in=affected_user_ids).values(
|
|
|
|
"id", "role", "date_joined"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else:
|
|
|
|
full_member_group_users = list(
|
|
|
|
full_members_system_group.direct_members.all().values("id", "role", "date_joined")
|
|
|
|
)
|
|
|
|
member_group_users = list(
|
|
|
|
members_system_group.direct_members.all().values("id", "role", "date_joined")
|
|
|
|
)
|
|
|
|
|
2022-07-15 18:10:00 +02:00
|
|
|
def is_provisional_member(user: MemberGroupUserDict) -> bool:
|
2022-04-14 23:30:17 +02:00
|
|
|
diff = (timezone_now() - user["date_joined"]).days
|
|
|
|
if diff < realm.waiting_period_threshold:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
old_full_members = [
|
|
|
|
user
|
|
|
|
for user in full_member_group_users
|
|
|
|
if is_provisional_member(user) or user["role"] != UserProfile.ROLE_MEMBER
|
|
|
|
]
|
|
|
|
|
|
|
|
full_member_group_user_ids = [user["id"] for user in full_member_group_users]
|
|
|
|
members_excluding_full_members = [
|
|
|
|
user for user in member_group_users if user["id"] not in full_member_group_user_ids
|
|
|
|
]
|
|
|
|
|
|
|
|
new_full_members = [
|
|
|
|
user for user in members_excluding_full_members if not is_provisional_member(user)
|
|
|
|
]
|
|
|
|
|
|
|
|
old_full_member_ids = [user["id"] for user in old_full_members]
|
|
|
|
new_full_member_ids = [user["id"] for user in new_full_members]
|
|
|
|
|
|
|
|
if len(old_full_members) > 0:
|
2023-09-29 01:45:53 +02:00
|
|
|
bulk_remove_members_from_user_groups(
|
|
|
|
[full_members_system_group], old_full_member_ids, acting_user=acting_user
|
2022-11-21 04:06:15 +01:00
|
|
|
)
|
2022-04-14 23:30:17 +02:00
|
|
|
|
|
|
|
if len(new_full_members) > 0:
|
2023-09-29 01:45:53 +02:00
|
|
|
bulk_add_members_to_user_groups(
|
|
|
|
[full_members_system_group], new_full_member_ids, acting_user=acting_user
|
2022-11-21 03:48:10 +01:00
|
|
|
)
|
2022-04-14 23:30:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
def promote_new_full_members() -> None:
|
|
|
|
for realm in Realm.objects.filter(deactivated=False).exclude(waiting_period_threshold=0):
|
2022-11-21 03:30:07 +01:00
|
|
|
update_users_in_full_members_system_group(realm, acting_user=None)
|
2022-04-14 23:30:17 +02:00
|
|
|
|
|
|
|
|
2022-02-28 11:50:33 +01:00
|
|
|
def do_send_create_user_group_event(
|
2024-04-16 16:05:43 +02:00
|
|
|
user_group: NamedUserGroup,
|
2024-07-12 02:30:17 +02:00
|
|
|
members: list[UserProfile],
|
2024-04-16 16:05:43 +02:00
|
|
|
direct_subgroups: Sequence[UserGroup] = [],
|
2022-02-28 11:50:33 +01:00
|
|
|
) -> None:
|
2024-06-04 12:36:52 +02:00
|
|
|
creator_id = user_group.creator_id
|
|
|
|
assert user_group.date_created is not None
|
|
|
|
date_created = datetime_to_timestamp(user_group.date_created)
|
|
|
|
|
2022-04-14 23:30:17 +02:00
|
|
|
event = dict(
|
|
|
|
type="user_group",
|
|
|
|
op="add",
|
|
|
|
group=dict(
|
|
|
|
name=user_group.name,
|
2024-06-04 12:36:52 +02:00
|
|
|
creator_id=creator_id,
|
|
|
|
date_created=date_created,
|
2022-04-14 23:30:17 +02:00
|
|
|
members=[member.id for member in members],
|
|
|
|
description=user_group.description,
|
|
|
|
id=user_group.id,
|
|
|
|
is_system_group=user_group.is_system_group,
|
2022-05-17 10:06:02 +02:00
|
|
|
direct_subgroup_ids=[direct_subgroup.id for direct_subgroup in direct_subgroups],
|
2024-09-19 15:39:08 +02:00
|
|
|
can_join_group=get_group_setting_value_for_api(user_group.can_join_group),
|
2023-07-24 17:44:11 +02:00
|
|
|
can_manage_group=get_group_setting_value_for_api(user_group.can_manage_group),
|
2024-04-29 05:51:48 +02:00
|
|
|
can_mention_group=get_group_setting_value_for_api(user_group.can_mention_group),
|
2024-05-15 15:44:18 +02:00
|
|
|
deactivated=False,
|
2022-04-14 23:30:17 +02:00
|
|
|
),
|
|
|
|
)
|
2024-08-09 12:30:44 +02:00
|
|
|
send_event_on_commit(user_group.realm, event, active_user_ids(user_group.realm_id))
|
2022-04-14 23:30:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
def check_add_user_group(
|
2022-11-21 03:37:11 +01:00
|
|
|
realm: Realm,
|
|
|
|
name: str,
|
2024-07-12 02:30:17 +02:00
|
|
|
initial_members: list[UserProfile],
|
2022-12-14 06:45:55 +01:00
|
|
|
description: str = "",
|
2023-06-14 16:48:58 +02:00
|
|
|
group_settings_map: Mapping[str, UserGroup] = {},
|
2022-11-21 03:37:11 +01:00
|
|
|
*,
|
2024-07-12 02:30:23 +02:00
|
|
|
acting_user: UserProfile | None,
|
2024-04-16 16:05:43 +02:00
|
|
|
) -> NamedUserGroup:
|
2022-04-14 23:30:17 +02:00
|
|
|
try:
|
2022-12-14 06:53:21 +01:00
|
|
|
user_group = create_user_group_in_database(
|
2023-06-14 16:48:58 +02:00
|
|
|
name,
|
|
|
|
initial_members,
|
|
|
|
realm,
|
|
|
|
description=description,
|
|
|
|
group_settings_map=group_settings_map,
|
|
|
|
acting_user=acting_user,
|
2022-11-21 03:37:11 +01:00
|
|
|
)
|
2022-04-14 23:30:17 +02:00
|
|
|
do_send_create_user_group_event(user_group, initial_members)
|
2022-12-14 06:45:55 +01:00
|
|
|
return user_group
|
2022-04-14 23:30:17 +02:00
|
|
|
except django.db.utils.IntegrityError:
|
2023-07-17 22:40:33 +02:00
|
|
|
raise JsonableError(_("User group '{group_name}' already exists.").format(group_name=name))
|
2022-04-14 23:30:17 +02:00
|
|
|
|
|
|
|
|
2023-06-15 05:24:23 +02:00
|
|
|
def do_send_user_group_update_event(
|
2024-07-12 02:30:23 +02:00
|
|
|
user_group: NamedUserGroup, data: dict[str, str | int | AnonymousSettingGroupDict]
|
2023-06-15 05:24:23 +02:00
|
|
|
) -> None:
|
2022-04-14 23:30:17 +02:00
|
|
|
event = dict(type="user_group", op="update", group_id=user_group.id, data=data)
|
2024-09-12 11:46:48 +02:00
|
|
|
if "name" in data:
|
|
|
|
# This field will be popped eventually before sending the event
|
|
|
|
# to client, but is needed to make sure we do not send the
|
|
|
|
# name update event for deactivated groups to client with
|
|
|
|
# 'include_deactivated_groups' client capability set to false.
|
|
|
|
event["deactivated"] = user_group.deactivated
|
2024-08-09 10:41:30 +02:00
|
|
|
send_event_on_commit(user_group.realm, event, active_user_ids(user_group.realm_id))
|
2022-04-14 23:30:17 +02:00
|
|
|
|
|
|
|
|
2023-06-05 22:34:52 +02:00
|
|
|
@transaction.atomic(savepoint=False)
|
2022-11-21 03:43:48 +01:00
|
|
|
def do_update_user_group_name(
|
2024-07-12 02:30:23 +02:00
|
|
|
user_group: NamedUserGroup, name: str, *, acting_user: UserProfile | None
|
2022-11-21 03:43:48 +01:00
|
|
|
) -> None:
|
2022-04-14 23:30:17 +02:00
|
|
|
try:
|
2023-06-05 22:34:52 +02:00
|
|
|
old_value = user_group.name
|
2022-04-14 23:30:17 +02:00
|
|
|
user_group.name = name
|
2024-04-18 18:59:50 +02:00
|
|
|
user_group.save(update_fields=["name"])
|
2023-06-05 22:34:52 +02:00
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=user_group.realm,
|
|
|
|
modified_user_group=user_group,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_NAME_CHANGED,
|
2023-06-05 22:34:52 +02:00
|
|
|
event_time=timezone_now(),
|
|
|
|
acting_user=acting_user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: name,
|
|
|
|
},
|
2023-06-05 22:34:52 +02:00
|
|
|
)
|
2022-04-14 23:30:17 +02:00
|
|
|
except django.db.utils.IntegrityError:
|
2023-07-17 22:40:33 +02:00
|
|
|
raise JsonableError(_("User group '{group_name}' already exists.").format(group_name=name))
|
2022-04-14 23:30:17 +02:00
|
|
|
do_send_user_group_update_event(user_group, dict(name=name))
|
|
|
|
|
|
|
|
|
2023-06-05 22:34:52 +02:00
|
|
|
@transaction.atomic(savepoint=False)
|
2022-11-21 03:45:16 +01:00
|
|
|
def do_update_user_group_description(
|
2024-07-12 02:30:23 +02:00
|
|
|
user_group: NamedUserGroup, description: str, *, acting_user: UserProfile | None
|
2022-11-21 03:45:16 +01:00
|
|
|
) -> None:
|
2023-06-05 22:34:52 +02:00
|
|
|
old_value = user_group.description
|
2022-04-14 23:30:17 +02:00
|
|
|
user_group.description = description
|
2024-04-18 18:59:50 +02:00
|
|
|
user_group.save(update_fields=["description"])
|
2023-06-05 22:34:52 +02:00
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=user_group.realm,
|
|
|
|
modified_user_group=user_group,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DESCRIPTION_CHANGED,
|
2023-06-05 22:34:52 +02:00
|
|
|
event_time=timezone_now(),
|
|
|
|
acting_user=acting_user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={
|
|
|
|
RealmAuditLog.OLD_VALUE: old_value,
|
|
|
|
RealmAuditLog.NEW_VALUE: description,
|
|
|
|
},
|
2023-06-05 22:34:52 +02:00
|
|
|
)
|
2022-04-14 23:30:17 +02:00
|
|
|
do_send_user_group_update_event(user_group, dict(description=description))
|
|
|
|
|
|
|
|
|
|
|
|
def do_send_user_group_members_update_event(
|
2024-07-12 02:30:17 +02:00
|
|
|
event_name: str, user_group: NamedUserGroup, user_ids: list[int]
|
2022-04-14 23:30:17 +02:00
|
|
|
) -> None:
|
|
|
|
event = dict(type="user_group", op=event_name, group_id=user_group.id, user_ids=user_ids)
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
send_event_on_commit(user_group.realm, event, active_user_ids(user_group.realm_id))
|
2022-04-14 23:30:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic(savepoint=False)
|
2023-09-29 01:45:53 +02:00
|
|
|
def bulk_add_members_to_user_groups(
|
2024-07-12 02:30:17 +02:00
|
|
|
user_groups: list[NamedUserGroup],
|
|
|
|
user_profile_ids: list[int],
|
2023-09-29 01:45:53 +02:00
|
|
|
*,
|
2024-07-12 02:30:23 +02:00
|
|
|
acting_user: UserProfile | None,
|
2022-11-21 03:48:10 +01:00
|
|
|
) -> None:
|
2023-09-29 01:45:53 +02:00
|
|
|
# All intended callers of this function involve a single user
|
|
|
|
# being added to one or more groups, or many users being added to
|
|
|
|
# a single group; but it's easy enough for the implementation to
|
|
|
|
# support both.
|
|
|
|
|
2022-04-14 23:30:17 +02:00
|
|
|
memberships = [
|
|
|
|
UserGroupMembership(user_group_id=user_group.id, user_profile_id=user_id)
|
|
|
|
for user_id in user_profile_ids
|
2023-09-29 01:45:53 +02:00
|
|
|
for user_group in user_groups
|
2022-04-14 23:30:17 +02:00
|
|
|
]
|
|
|
|
UserGroupMembership.objects.bulk_create(memberships)
|
2022-11-21 07:26:34 +01:00
|
|
|
now = timezone_now()
|
|
|
|
RealmAuditLog.objects.bulk_create(
|
|
|
|
RealmAuditLog(
|
|
|
|
realm=user_group.realm,
|
|
|
|
modified_user_id=user_id,
|
|
|
|
modified_user_group=user_group,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_ADDED,
|
2022-11-21 07:26:34 +01:00
|
|
|
event_time=now,
|
|
|
|
acting_user=acting_user,
|
|
|
|
)
|
|
|
|
for user_id in user_profile_ids
|
2023-09-29 01:45:53 +02:00
|
|
|
for user_group in user_groups
|
2022-11-21 07:26:34 +01:00
|
|
|
)
|
2022-04-14 23:30:17 +02:00
|
|
|
|
2023-09-29 01:45:53 +02:00
|
|
|
for user_group in user_groups:
|
|
|
|
do_send_user_group_members_update_event("add_members", user_group, user_profile_ids)
|
2022-04-14 23:30:17 +02:00
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic(savepoint=False)
|
2023-09-29 01:45:53 +02:00
|
|
|
def bulk_remove_members_from_user_groups(
|
2024-07-12 02:30:17 +02:00
|
|
|
user_groups: list[NamedUserGroup],
|
|
|
|
user_profile_ids: list[int],
|
2023-09-29 01:45:53 +02:00
|
|
|
*,
|
2024-07-12 02:30:23 +02:00
|
|
|
acting_user: UserProfile | None,
|
2022-11-21 04:06:15 +01:00
|
|
|
) -> None:
|
2023-09-29 01:45:53 +02:00
|
|
|
# All intended callers of this function involve a single user
|
|
|
|
# being added to one or more groups, or many users being added to
|
|
|
|
# a single group; but it's easy enough for the implementation to
|
|
|
|
# support both.
|
|
|
|
|
2022-04-14 23:30:17 +02:00
|
|
|
UserGroupMembership.objects.filter(
|
2023-09-29 01:45:53 +02:00
|
|
|
user_group__in=user_groups, user_profile_id__in=user_profile_ids
|
2022-04-14 23:30:17 +02:00
|
|
|
).delete()
|
2022-11-21 07:26:34 +01:00
|
|
|
now = timezone_now()
|
|
|
|
RealmAuditLog.objects.bulk_create(
|
|
|
|
RealmAuditLog(
|
|
|
|
realm=user_group.realm,
|
|
|
|
modified_user_id=user_id,
|
|
|
|
modified_user_group=user_group,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_USER_MEMBERSHIP_REMOVED,
|
2022-11-21 07:26:34 +01:00
|
|
|
event_time=now,
|
|
|
|
acting_user=acting_user,
|
|
|
|
)
|
|
|
|
for user_id in user_profile_ids
|
2023-09-29 01:45:53 +02:00
|
|
|
for user_group in user_groups
|
2022-11-21 07:26:34 +01:00
|
|
|
)
|
2022-04-14 23:30:17 +02:00
|
|
|
|
2023-09-29 01:45:53 +02:00
|
|
|
for user_group in user_groups:
|
|
|
|
do_send_user_group_members_update_event("remove_members", user_group, user_profile_ids)
|
2022-04-14 23:30:17 +02:00
|
|
|
|
|
|
|
|
2022-03-01 07:52:47 +01:00
|
|
|
def do_send_subgroups_update_event(
|
2024-07-12 02:30:17 +02:00
|
|
|
event_name: str, user_group: NamedUserGroup, subgroup_ids: list[int]
|
2022-03-01 07:52:47 +01:00
|
|
|
) -> None:
|
|
|
|
event = dict(
|
2022-05-16 17:02:44 +02:00
|
|
|
type="user_group", op=event_name, group_id=user_group.id, direct_subgroup_ids=subgroup_ids
|
2022-03-01 07:52:47 +01:00
|
|
|
)
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
send_event_on_commit(user_group.realm, event, active_user_ids(user_group.realm_id))
|
2022-03-01 07:52:47 +01:00
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic
|
2022-11-21 04:09:19 +01:00
|
|
|
def add_subgroups_to_user_group(
|
2024-04-17 05:45:32 +02:00
|
|
|
user_group: NamedUserGroup,
|
2024-07-12 02:30:17 +02:00
|
|
|
subgroups: list[NamedUserGroup],
|
2024-04-17 05:45:32 +02:00
|
|
|
*,
|
2024-07-12 02:30:23 +02:00
|
|
|
acting_user: UserProfile | None,
|
2022-11-21 04:09:19 +01:00
|
|
|
) -> None:
|
2022-03-01 07:52:47 +01:00
|
|
|
group_memberships = [
|
|
|
|
GroupGroupMembership(supergroup=user_group, subgroup=subgroup) for subgroup in subgroups
|
|
|
|
]
|
|
|
|
GroupGroupMembership.objects.bulk_create(group_memberships)
|
|
|
|
|
|
|
|
subgroup_ids = [subgroup.id for subgroup in subgroups]
|
2022-12-12 03:29:10 +01:00
|
|
|
now = timezone_now()
|
2022-12-12 03:29:10 +01:00
|
|
|
audit_log_entries = [
|
|
|
|
RealmAuditLog(
|
|
|
|
realm=user_group.realm,
|
|
|
|
modified_user_group=user_group,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_SUBGROUP_MEMBERSHIP_ADDED,
|
2022-12-12 03:29:10 +01:00
|
|
|
event_time=now,
|
|
|
|
acting_user=acting_user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={"subgroup_ids": subgroup_ids},
|
2023-07-31 22:52:35 +02:00
|
|
|
),
|
|
|
|
*(
|
2022-12-12 03:29:10 +01:00
|
|
|
RealmAuditLog(
|
|
|
|
realm=user_group.realm,
|
|
|
|
modified_user_group_id=subgroup_id,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_SUPERGROUP_MEMBERSHIP_ADDED,
|
2022-12-12 03:29:10 +01:00
|
|
|
event_time=now,
|
|
|
|
acting_user=acting_user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={"supergroup_ids": [user_group.id]},
|
2022-12-12 03:29:10 +01:00
|
|
|
)
|
2023-07-31 22:52:35 +02:00
|
|
|
for subgroup_id in subgroup_ids
|
|
|
|
),
|
|
|
|
]
|
2022-12-12 03:29:10 +01:00
|
|
|
RealmAuditLog.objects.bulk_create(audit_log_entries)
|
2022-12-12 03:29:10 +01:00
|
|
|
|
2022-03-01 07:52:47 +01:00
|
|
|
do_send_subgroups_update_event("add_subgroups", user_group, subgroup_ids)
|
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic
|
2022-11-21 04:10:48 +01:00
|
|
|
def remove_subgroups_from_user_group(
|
2024-04-17 05:45:32 +02:00
|
|
|
user_group: NamedUserGroup,
|
2024-07-12 02:30:17 +02:00
|
|
|
subgroups: list[NamedUserGroup],
|
2024-04-17 05:45:32 +02:00
|
|
|
*,
|
2024-07-12 02:30:23 +02:00
|
|
|
acting_user: UserProfile | None,
|
2022-11-21 04:10:48 +01:00
|
|
|
) -> None:
|
2022-03-01 07:52:47 +01:00
|
|
|
GroupGroupMembership.objects.filter(supergroup=user_group, subgroup__in=subgroups).delete()
|
|
|
|
|
|
|
|
subgroup_ids = [subgroup.id for subgroup in subgroups]
|
2022-12-12 03:29:10 +01:00
|
|
|
now = timezone_now()
|
2022-12-12 03:29:10 +01:00
|
|
|
audit_log_entries = [
|
|
|
|
RealmAuditLog(
|
|
|
|
realm=user_group.realm,
|
|
|
|
modified_user_group=user_group,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_SUBGROUP_MEMBERSHIP_REMOVED,
|
2022-12-12 03:29:10 +01:00
|
|
|
event_time=now,
|
|
|
|
acting_user=acting_user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={"subgroup_ids": subgroup_ids},
|
2023-07-31 22:52:35 +02:00
|
|
|
),
|
|
|
|
*(
|
2022-12-12 03:29:10 +01:00
|
|
|
RealmAuditLog(
|
|
|
|
realm=user_group.realm,
|
|
|
|
modified_user_group_id=subgroup_id,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_DIRECT_SUPERGROUP_MEMBERSHIP_REMOVED,
|
2022-12-12 03:29:10 +01:00
|
|
|
event_time=now,
|
|
|
|
acting_user=acting_user,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={"supergroup_ids": [user_group.id]},
|
2022-12-12 03:29:10 +01:00
|
|
|
)
|
2023-07-31 22:52:35 +02:00
|
|
|
for subgroup_id in subgroup_ids
|
|
|
|
),
|
|
|
|
]
|
2022-12-12 03:29:10 +01:00
|
|
|
RealmAuditLog.objects.bulk_create(audit_log_entries)
|
2022-12-12 03:29:10 +01:00
|
|
|
|
2022-03-01 07:52:47 +01:00
|
|
|
do_send_subgroups_update_event("remove_subgroups", user_group, subgroup_ids)
|
|
|
|
|
|
|
|
|
2024-05-15 15:44:18 +02:00
|
|
|
@transaction.atomic(savepoint=False)
|
|
|
|
def do_deactivate_user_group(
|
|
|
|
user_group: NamedUserGroup, *, acting_user: UserProfile | None
|
|
|
|
) -> None:
|
|
|
|
user_group.deactivated = True
|
|
|
|
user_group.save(update_fields=["deactivated"])
|
|
|
|
|
|
|
|
now = timezone_now()
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=user_group.realm,
|
|
|
|
modified_user_group_id=user_group.id,
|
|
|
|
event_type=AuditLogEventType.USER_GROUP_DEACTIVATED,
|
|
|
|
event_time=now,
|
|
|
|
acting_user=acting_user,
|
|
|
|
)
|
|
|
|
|
|
|
|
do_send_user_group_update_event(user_group, dict(deactivated=True))
|
|
|
|
|
2024-09-12 11:46:48 +02:00
|
|
|
event = dict(type="user_group", op="remove", group_id=user_group.id)
|
|
|
|
send_event_on_commit(user_group.realm, event, active_user_ids(user_group.realm_id))
|
|
|
|
|
2024-05-15 15:44:18 +02:00
|
|
|
|
2023-06-05 22:34:52 +02:00
|
|
|
@transaction.atomic(savepoint=False)
|
2023-06-15 05:24:23 +02:00
|
|
|
def do_change_user_group_permission_setting(
|
2024-04-17 05:45:32 +02:00
|
|
|
user_group: NamedUserGroup,
|
2023-06-15 05:24:23 +02:00
|
|
|
setting_name: str,
|
|
|
|
setting_value_group: UserGroup,
|
|
|
|
*,
|
2024-08-09 14:04:35 +02:00
|
|
|
old_setting_api_value: int | AnonymousSettingGroupDict | None = None,
|
2024-07-12 02:30:23 +02:00
|
|
|
acting_user: UserProfile | None,
|
2023-06-15 05:24:23 +02:00
|
|
|
) -> None:
|
2023-06-05 22:34:52 +02:00
|
|
|
old_value = getattr(user_group, setting_name)
|
2023-06-15 05:24:23 +02:00
|
|
|
setattr(user_group, setting_name, setting_value_group)
|
|
|
|
user_group.save()
|
2024-04-30 15:16:58 +02:00
|
|
|
|
2024-08-09 14:04:35 +02:00
|
|
|
if old_setting_api_value is None:
|
|
|
|
# Most production callers will have computed this as part of
|
|
|
|
# verifying whether there's an actual change to make, but it
|
|
|
|
# feels quite clumsy to have to pass it from unit tests, so we
|
|
|
|
# compute it here if not provided by the caller.
|
|
|
|
old_setting_api_value = get_group_setting_value_for_api(old_value)
|
2024-04-30 15:16:58 +02:00
|
|
|
new_setting_api_value = get_group_setting_value_for_api(setting_value_group)
|
|
|
|
|
|
|
|
if not hasattr(old_value, "named_user_group") and hasattr(
|
|
|
|
setting_value_group, "named_user_group"
|
|
|
|
):
|
|
|
|
# We delete the UserGroup which the setting was set to
|
|
|
|
# previously if it does not have any linked NamedUserGroup
|
|
|
|
# object, as it is not used anywhere else. A new UserGroup
|
|
|
|
# object would be created if the setting is later set to
|
|
|
|
# a combination of users and groups.
|
|
|
|
old_value.delete()
|
|
|
|
|
2023-06-05 22:34:52 +02:00
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=user_group.realm,
|
|
|
|
acting_user=acting_user,
|
2024-09-06 17:06:03 +02:00
|
|
|
event_type=AuditLogEventType.USER_GROUP_GROUP_BASED_SETTING_CHANGED,
|
2023-06-05 22:34:52 +02:00
|
|
|
event_time=timezone_now(),
|
|
|
|
modified_user_group=user_group,
|
2023-07-13 19:46:06 +02:00
|
|
|
extra_data={
|
2024-04-30 15:16:58 +02:00
|
|
|
RealmAuditLog.OLD_VALUE: get_group_setting_value_for_audit_log_data(
|
|
|
|
old_setting_api_value
|
|
|
|
),
|
|
|
|
RealmAuditLog.NEW_VALUE: get_group_setting_value_for_audit_log_data(
|
|
|
|
new_setting_api_value
|
|
|
|
),
|
2023-07-13 19:46:06 +02:00
|
|
|
"property": setting_name,
|
|
|
|
},
|
2023-06-05 22:34:52 +02:00
|
|
|
)
|
2023-06-15 05:24:23 +02:00
|
|
|
|
2024-07-12 02:30:23 +02:00
|
|
|
event_data_dict: dict[str, str | int | AnonymousSettingGroupDict] = {
|
2024-04-30 15:16:58 +02:00
|
|
|
setting_name: new_setting_api_value
|
|
|
|
}
|
2023-06-15 05:24:23 +02:00
|
|
|
do_send_user_group_update_event(user_group, event_data_dict)
|