user_groups: Add require_system_group parameter.

This commit adds require_system_group parameter to
get_realm_user_groups_for_dropdown_list_widget.
We currently pass this parameter as "true" always,
but this will be needed in future when we will allow
to set groups other than system groups in settings.
This commit is contained in:
Sahil Batra 2023-02-17 12:50:12 +05:30 committed by Tim Abbott
parent 3e7de4cc64
commit 416c7c2199
4 changed files with 57 additions and 12 deletions

View File

@ -299,11 +299,22 @@ run_test("get_realm_user_groups_for_dropdown_list_widget", () => {
is_system_group: true,
direct_subgroup_ids: new Set([5]),
};
const students = {
description: "Students group",
name: "Students",
id: 8,
members: new Set([1, 2]),
is_system_group: false,
direct_subgroup_ids: new Set([4, 5]),
};
assert.throws(() => user_groups.get_realm_user_groups_for_dropdown_list_widget(false, false), {
name: "Error",
message: "Unknown group name: @role:internet",
});
assert.throws(
() => user_groups.get_realm_user_groups_for_dropdown_list_widget(true, false, false),
{
name: "Error",
message: "Unknown group name: @role:internet",
},
);
let expected_groups_list = [
{name: "translated: Admins, moderators, members and guests", value: "5"},
@ -315,11 +326,20 @@ run_test("get_realm_user_groups_for_dropdown_list_widget", () => {
];
user_groups.initialize({
realm_user_groups: [owners, admins, moderators, members, everyone, full_members, internet],
realm_user_groups: [
owners,
admins,
moderators,
members,
everyone,
full_members,
internet,
students,
],
});
assert.deepEqual(
user_groups.get_realm_user_groups_for_dropdown_list_widget(true, false),
user_groups.get_realm_user_groups_for_dropdown_list_widget(true, true, false),
expected_groups_list,
);
@ -332,7 +352,7 @@ run_test("get_realm_user_groups_for_dropdown_list_widget", () => {
{name: "translated: Admins", value: "2"},
];
assert.deepEqual(
user_groups.get_realm_user_groups_for_dropdown_list_widget(false, true),
user_groups.get_realm_user_groups_for_dropdown_list_widget(true, false, true),
expected_groups_list,
);
@ -344,7 +364,7 @@ run_test("get_realm_user_groups_for_dropdown_list_widget", () => {
{name: "translated: Admins", value: "2"},
];
assert.deepEqual(
user_groups.get_realm_user_groups_for_dropdown_list_widget(true, true),
user_groups.get_realm_user_groups_for_dropdown_list_widget(true, true, true),
expected_groups_list,
);
@ -358,7 +378,20 @@ run_test("get_realm_user_groups_for_dropdown_list_widget", () => {
{name: "translated: Owners", value: "1"},
];
assert.deepEqual(
user_groups.get_realm_user_groups_for_dropdown_list_widget(false, false),
user_groups.get_realm_user_groups_for_dropdown_list_widget(true, false, false),
expected_groups_list,
);
expected_groups_list = [
{name: "translated: Admins, moderators, members and guests", value: "5"},
{name: "translated: Admins, moderators and members", value: "4"},
{name: "translated: Admins, moderators and full members", value: "6"},
{name: "translated: Admins and moderators", value: "3"},
{name: "translated: Admins", value: "2"},
{name: "Students", value: "8"},
];
assert.deepEqual(
user_groups.get_realm_user_groups_for_dropdown_list_widget(false, true, true),
expected_groups_list,
);
});

View File

@ -206,7 +206,7 @@ export function show_settings_for(node) {
const opts = {
widget_name: "can_remove_subscribers_group_id",
data: user_groups.get_realm_user_groups_for_dropdown_list_widget(true, true),
data: user_groups.get_realm_user_groups_for_dropdown_list_widget(true, true, true),
default_text: $t({defaultMessage: "No user groups"}),
include_current_item: false,
value: sub.can_remove_subscribers_group_id,

View File

@ -630,7 +630,7 @@ export function setup_page(callback) {
const opts = {
widget_name: "new_stream_can_remove_subscribers_group_id",
data: user_groups.get_realm_user_groups_for_dropdown_list_widget(true, true),
data: user_groups.get_realm_user_groups_for_dropdown_list_widget(true, true, true),
default_text: $t({defaultMessage: "No user groups"}),
include_current_item: false,
value: user_groups.get_user_group_from_name("@role:administrators").id,

View File

@ -203,10 +203,11 @@ export function is_user_in_group(user_group_id: number, user_id: number): boolea
}
export function get_realm_user_groups_for_dropdown_list_widget(
require_system_group: boolean,
exclude_internet_group: boolean,
exclude_owners_group: boolean,
): UserGroupForDropdownListWidget[] {
return settings_config.system_user_groups_list
const system_user_groups = settings_config.system_user_groups_list
.filter((group) => {
if (exclude_internet_group && group.name === "@role:internet") {
return false;
@ -228,4 +229,15 @@ export function get_realm_user_groups_for_dropdown_list_widget(
value: user_group.id.toString(),
};
});
if (require_system_group) {
return system_user_groups;
}
const user_groups_excluding_system_groups = get_realm_user_groups().map((group) => ({
name: group.name,
value: group.id.toString(),
}));
return system_user_groups.concat(user_groups_excluding_system_groups);
}