diff --git a/web/src/settings_data.ts b/web/src/settings_data.ts index e5659450a8..07c8371625 100644 --- a/web/src/settings_data.ts +++ b/web/src/settings_data.ts @@ -183,6 +183,25 @@ export function user_can_edit_user_groups(): boolean { return user_has_permission(page_params.realm_user_group_edit_policy); } +export function can_edit_user_group(group_id: number): boolean { + if (!page_params.user_id) { + return false; + } + + if (!user_can_edit_user_groups()) { + return false; + } + + // Admins and moderators are allowed to edit user groups even if they + // are not a member of that user group. Members can edit user groups + // only if they belong to that group. + if (page_params.is_admin || page_params.is_moderator) { + return true; + } + + return user_groups.is_direct_member_of(page_params.user_id, group_id); +} + export function user_can_add_custom_emoji(): boolean { return user_has_permission(page_params.realm_add_custom_emoji_policy); } diff --git a/web/src/user_group_edit.js b/web/src/user_group_edit.js index f6a54e9bed..553acf5cbe 100644 --- a/web/src/user_group_edit.js +++ b/web/src/user_group_edit.js @@ -13,7 +13,6 @@ import * as dialog_widget from "./dialog_widget"; import * as hash_util from "./hash_util"; import {$t, $t_html} from "./i18n"; import * as overlays from "./overlays"; -import {page_params} from "./page_params"; import * as people from "./people"; import * as scroll_util from "./scroll_util"; import * as settings_data from "./settings_data"; @@ -51,21 +50,6 @@ function get_user_group_for_target(target) { return group; } -export function can_edit(group_id) { - if (!settings_data.user_can_edit_user_groups()) { - return false; - } - - // Admins and moderators are allowed to edit user groups even if they - // are not a member of that user group. Members can edit user groups - // only if they belong to that group. - if (page_params.is_admin || page_params.is_moderator) { - return true; - } - - return user_groups.is_direct_member_of(people.my_current_user_id(), group_id); -} - export function get_edit_container(group) { return $( `#groups_overlay .user_group_settings_wrapper[data-group-id='${CSS.escape(group.id)}']`, @@ -141,7 +125,7 @@ export function handle_member_edit_event(group_id, user_ids) { } // update_settings buttons. - if (can_edit(group_id)) { + if (settings_data.can_edit_user_group(group_id)) { enable_group_edit_settings(group); } else { disable_group_edit_settings(group); @@ -157,7 +141,7 @@ export function update_settings_pane(group) { export function show_settings_for(group) { const html = render_user_group_settings({ group, - can_edit: can_edit(group.id), + can_edit: settings_data.can_edit_user_group(group.id), }); scroll_util.get_content_element($("#user_group_settings")).html(html); @@ -288,7 +272,7 @@ export function initialize() { const group_id = active_group_data.id; const user_group = user_groups.get_user_group_from_id(group_id); - if (!user_group || !can_edit(group_id)) { + if (!user_group || !settings_data.can_edit_user_group(group_id)) { return; } function delete_user_group() { diff --git a/web/src/user_group_edit_members.js b/web/src/user_group_edit_members.js index c5b6430468..64b55a411b 100644 --- a/web/src/user_group_edit_members.js +++ b/web/src/user_group_edit_members.js @@ -14,8 +14,8 @@ import * as ListWidget from "./list_widget"; import {page_params} from "./page_params"; import * as people from "./people"; import * as scroll_util from "./scroll_util"; +import * as settings_data from "./settings_data"; import * as settings_users from "./settings_users"; -import * as user_group_edit from "./user_group_edit"; import * as user_groups from "./user_groups"; export let pill_widget; @@ -49,7 +49,7 @@ function format_member_list_elem(person) { user_id: person.user_id, is_current_user: person.user_id === page_params.user_id, email: person.delivery_email, - can_remove_subscribers: user_group_edit.can_edit(current_group_id), + can_remove_subscribers: settings_data.can_edit_user_group(current_group_id), }); } diff --git a/web/src/user_group_ui_updates.js b/web/src/user_group_ui_updates.js index 5262d5c8bf..d429a47e1c 100644 --- a/web/src/user_group_ui_updates.js +++ b/web/src/user_group_ui_updates.js @@ -2,6 +2,7 @@ import $ from "jquery"; import {$t} from "./i18n"; import {page_params} from "./page_params"; +import * as settings_data from "./settings_data"; import * as stream_ui_updates from "./stream_ui_updates"; import * as user_group_edit from "./user_group_edit"; @@ -30,7 +31,7 @@ export function update_add_members_elements(group) { const $input_element = $add_members_container.find(".input").expectOne(); const $button_element = $add_members_container.find('button[name="add_member"]').expectOne(); - if (user_group_edit.can_edit(group.id)) { + if (settings_data.can_edit_user_group(group.id)) { $input_element.prop("disabled", false); $button_element.prop("disabled", false); $button_element.css("pointer-events", ""); diff --git a/web/tests/settings_data.test.js b/web/tests/settings_data.test.js index b0ea7837af..fba2a75373 100644 --- a/web/tests/settings_data.test.js +++ b/web/tests/settings_data.test.js @@ -371,3 +371,36 @@ run_test("user_can_create_multiuse_invite", () => { page_params.user_id = member_user_id; assert.equal(settings_data.user_can_create_multiuse_invite(), false); }); + +run_test("can_edit_user_group", () => { + const students = { + description: "Students group", + name: "Students", + id: 0, + members: new Set([1, 2]), + is_system_group: false, + direct_subgroup_ids: new Set([4, 5]), + can_mention_group: 2, + }; + user_groups.initialize({ + realm_user_groups: [students], + }); + + delete page_params.user_id; + assert.ok(!settings_data.can_edit_user_group(students.id)); + + page_params.user_id = 3; + page_params.is_guest = true; + assert.ok(!settings_data.can_edit_user_group(students.id)); + + page_params.is_guest = false; + page_params.is_moderator = true; + assert.ok(settings_data.can_edit_user_group(students.id)); + + page_params.is_moderator = false; + assert.ok(!settings_data.can_edit_user_group(students.id)); + + page_params.user_id = 2; + page_params.realm_waiting_period_threshold = 0; + assert.ok(settings_data.can_edit_user_group(students.id)); +});