2017-11-01 10:04:16 +01:00
|
|
|
from django.http import HttpResponse, HttpRequest
|
2017-11-02 07:53:08 +01:00
|
|
|
from django.utils.translation import ugettext as _
|
2017-11-01 10:04:16 +01:00
|
|
|
|
2018-04-24 03:47:28 +02:00
|
|
|
from typing import List
|
2017-11-01 10:04:16 +01:00
|
|
|
|
2018-06-14 08:35:05 +02:00
|
|
|
from zerver.decorator import require_non_guest_human_user
|
2017-11-01 10:04:16 +01:00
|
|
|
from zerver.context_processors import get_realm_from_request
|
2017-11-02 07:53:08 +01:00
|
|
|
from zerver.lib.actions import check_add_user_group, do_update_user_group_name, \
|
2017-11-02 08:53:30 +01:00
|
|
|
do_update_user_group_description, bulk_add_members_to_user_group, \
|
2017-11-15 08:09:06 +01:00
|
|
|
remove_members_from_user_group, check_delete_user_group
|
2017-11-02 07:53:08 +01:00
|
|
|
from zerver.lib.exceptions import JsonableError
|
2017-11-01 10:04:16 +01:00
|
|
|
from zerver.lib.request import has_request_variables, REQ
|
|
|
|
from zerver.lib.response import json_success, json_error
|
|
|
|
from zerver.lib.users import user_ids_to_users
|
|
|
|
from zerver.lib.validator import check_list, check_string, check_int, \
|
|
|
|
check_short_string
|
2018-02-19 13:38:18 +01:00
|
|
|
from zerver.lib.user_groups import access_user_group_by_id, get_memberships_of_users, get_user_group_members
|
2017-11-02 08:53:30 +01:00
|
|
|
from zerver.models import UserProfile, UserGroup, UserGroupMembership
|
|
|
|
from zerver.views.streams import compose_views, FuncKwargPair
|
2017-11-01 10:04:16 +01:00
|
|
|
|
2018-06-14 08:35:05 +02:00
|
|
|
@require_non_guest_human_user
|
2017-11-01 10:04:16 +01:00
|
|
|
@has_request_variables
|
2017-12-29 14:34:49 +01:00
|
|
|
def add_user_group(request: HttpRequest, user_profile: UserProfile,
|
2018-04-24 03:47:28 +02:00
|
|
|
name: str=REQ(),
|
2017-12-29 14:34:49 +01:00
|
|
|
members: List[int]=REQ(validator=check_list(check_int), default=[]),
|
2018-04-24 03:47:28 +02:00
|
|
|
description: str=REQ()) -> HttpResponse:
|
2017-11-01 10:04:16 +01:00
|
|
|
user_profiles = user_ids_to_users(members, user_profile.realm)
|
|
|
|
check_add_user_group(user_profile.realm, name, user_profiles, description)
|
|
|
|
return json_success()
|
2017-11-02 07:53:08 +01:00
|
|
|
|
2018-06-14 08:35:05 +02:00
|
|
|
@require_non_guest_human_user
|
2017-11-02 07:53:08 +01:00
|
|
|
@has_request_variables
|
2017-12-29 14:34:49 +01:00
|
|
|
def edit_user_group(request: HttpRequest, user_profile: UserProfile,
|
|
|
|
user_group_id: int=REQ(validator=check_int),
|
2018-04-24 03:47:28 +02:00
|
|
|
name: str=REQ(default=""), description: str=REQ(default="")
|
2017-12-29 14:34:49 +01:00
|
|
|
) -> HttpResponse:
|
2017-11-02 07:53:08 +01:00
|
|
|
if not (name or description):
|
|
|
|
return json_error(_("No new data supplied"))
|
|
|
|
|
2018-02-19 13:38:18 +01:00
|
|
|
user_group = access_user_group_by_id(user_group_id, user_profile)
|
2017-11-02 07:53:08 +01:00
|
|
|
|
|
|
|
result = {}
|
|
|
|
if name != user_group.name:
|
|
|
|
do_update_user_group_name(user_group, name)
|
|
|
|
result['name'] = _("Name successfully updated.")
|
|
|
|
|
|
|
|
if description != user_group.description:
|
|
|
|
do_update_user_group_description(user_group, description)
|
|
|
|
result['description'] = _("Description successfully updated.")
|
|
|
|
|
|
|
|
return json_success(result)
|
2017-11-02 08:15:14 +01:00
|
|
|
|
2018-06-14 08:35:05 +02:00
|
|
|
@require_non_guest_human_user
|
2017-11-02 08:15:14 +01:00
|
|
|
@has_request_variables
|
2017-11-27 09:28:57 +01:00
|
|
|
def delete_user_group(request: HttpRequest, user_profile: UserProfile,
|
|
|
|
user_group_id: int=REQ(validator=check_int)) -> HttpResponse:
|
2018-02-19 13:38:18 +01:00
|
|
|
|
|
|
|
check_delete_user_group(user_group_id, user_profile)
|
2017-11-02 08:15:14 +01:00
|
|
|
return json_success()
|
2017-11-02 08:53:30 +01:00
|
|
|
|
2018-06-14 08:35:05 +02:00
|
|
|
@require_non_guest_human_user
|
2017-11-02 08:53:30 +01:00
|
|
|
@has_request_variables
|
2017-12-29 14:34:49 +01:00
|
|
|
def update_user_group_backend(request: HttpRequest, user_profile: UserProfile,
|
|
|
|
user_group_id: int=REQ(validator=check_int),
|
|
|
|
delete: List[int]=REQ(validator=check_list(check_int), default=[]),
|
|
|
|
add: List[int]=REQ(validator=check_list(check_int), default=[])
|
|
|
|
) -> HttpResponse:
|
2017-11-02 08:53:30 +01:00
|
|
|
if not add and not delete:
|
|
|
|
return json_error(_('Nothing to do. Specify at least one of "add" or "delete".'))
|
|
|
|
|
|
|
|
method_kwarg_pairs = [
|
|
|
|
(add_members_to_group_backend,
|
|
|
|
dict(user_group_id=user_group_id, members=add)),
|
|
|
|
(remove_members_from_group_backend,
|
|
|
|
dict(user_group_id=user_group_id, members=delete))
|
|
|
|
] # type: List[FuncKwargPair]
|
|
|
|
return compose_views(request, user_profile, method_kwarg_pairs)
|
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def add_members_to_group_backend(request: HttpRequest, user_profile: UserProfile,
|
|
|
|
user_group_id: int, members: List[int]) -> HttpResponse:
|
2017-11-02 08:53:30 +01:00
|
|
|
if not members:
|
|
|
|
return json_success()
|
|
|
|
|
2018-02-19 13:38:18 +01:00
|
|
|
user_group = access_user_group_by_id(user_group_id, user_profile)
|
2017-11-02 08:53:30 +01:00
|
|
|
user_profiles = user_ids_to_users(members, user_profile.realm)
|
|
|
|
existing_member_ids = set(get_memberships_of_users(user_group, user_profiles))
|
2018-02-19 13:38:18 +01:00
|
|
|
|
2017-11-02 08:53:30 +01:00
|
|
|
for user_profile in user_profiles:
|
|
|
|
if user_profile.id in existing_member_ids:
|
|
|
|
raise JsonableError(_("User %s is already a member of this group" % (user_profile.id,)))
|
|
|
|
|
2017-11-14 08:40:53 +01:00
|
|
|
bulk_add_members_to_user_group(user_group, user_profiles)
|
2017-11-02 08:53:30 +01:00
|
|
|
return json_success()
|
|
|
|
|
2017-11-27 09:28:57 +01:00
|
|
|
def remove_members_from_group_backend(request: HttpRequest, user_profile: UserProfile,
|
|
|
|
user_group_id: int, members: List[int]) -> HttpResponse:
|
2017-11-02 08:53:30 +01:00
|
|
|
if not members:
|
|
|
|
return json_success()
|
|
|
|
|
|
|
|
user_profiles = user_ids_to_users(members, user_profile.realm)
|
2018-02-19 13:38:18 +01:00
|
|
|
user_group = access_user_group_by_id(user_group_id, user_profile)
|
|
|
|
group_member_ids = get_user_group_members(user_group)
|
|
|
|
for member in members:
|
|
|
|
if (member not in group_member_ids):
|
|
|
|
raise JsonableError(_("There is no member '%s' in this user group" % (member,)))
|
|
|
|
|
2017-11-02 08:53:30 +01:00
|
|
|
remove_members_from_user_group(user_group, user_profiles)
|
|
|
|
return json_success()
|