user_groups: Move can_edit to settings_data module.

This fixes an import cycle.
This commit is contained in:
Tim Abbott 2023-09-22 10:32:14 -07:00
parent 3edf347f89
commit 458a0b3f3a
5 changed files with 59 additions and 22 deletions

View File

@ -183,6 +183,25 @@ export function user_can_edit_user_groups(): boolean {
return user_has_permission(page_params.realm_user_group_edit_policy); 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 { export function user_can_add_custom_emoji(): boolean {
return user_has_permission(page_params.realm_add_custom_emoji_policy); return user_has_permission(page_params.realm_add_custom_emoji_policy);
} }

View File

@ -13,7 +13,6 @@ import * as dialog_widget from "./dialog_widget";
import * as hash_util from "./hash_util"; import * as hash_util from "./hash_util";
import {$t, $t_html} from "./i18n"; import {$t, $t_html} from "./i18n";
import * as overlays from "./overlays"; import * as overlays from "./overlays";
import {page_params} from "./page_params";
import * as people from "./people"; import * as people from "./people";
import * as scroll_util from "./scroll_util"; import * as scroll_util from "./scroll_util";
import * as settings_data from "./settings_data"; import * as settings_data from "./settings_data";
@ -51,21 +50,6 @@ function get_user_group_for_target(target) {
return group; 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) { export function get_edit_container(group) {
return $( return $(
`#groups_overlay .user_group_settings_wrapper[data-group-id='${CSS.escape(group.id)}']`, `#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. // update_settings buttons.
if (can_edit(group_id)) { if (settings_data.can_edit_user_group(group_id)) {
enable_group_edit_settings(group); enable_group_edit_settings(group);
} else { } else {
disable_group_edit_settings(group); disable_group_edit_settings(group);
@ -157,7 +141,7 @@ export function update_settings_pane(group) {
export function show_settings_for(group) { export function show_settings_for(group) {
const html = render_user_group_settings({ const html = render_user_group_settings({
group, 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); 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 group_id = active_group_data.id;
const user_group = user_groups.get_user_group_from_id(group_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; return;
} }
function delete_user_group() { function delete_user_group() {

View File

@ -14,8 +14,8 @@ import * as ListWidget from "./list_widget";
import {page_params} from "./page_params"; import {page_params} from "./page_params";
import * as people from "./people"; import * as people from "./people";
import * as scroll_util from "./scroll_util"; import * as scroll_util from "./scroll_util";
import * as settings_data from "./settings_data";
import * as settings_users from "./settings_users"; import * as settings_users from "./settings_users";
import * as user_group_edit from "./user_group_edit";
import * as user_groups from "./user_groups"; import * as user_groups from "./user_groups";
export let pill_widget; export let pill_widget;
@ -49,7 +49,7 @@ function format_member_list_elem(person) {
user_id: person.user_id, user_id: person.user_id,
is_current_user: person.user_id === page_params.user_id, is_current_user: person.user_id === page_params.user_id,
email: person.delivery_email, 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),
}); });
} }

View File

@ -2,6 +2,7 @@ import $ from "jquery";
import {$t} from "./i18n"; import {$t} from "./i18n";
import {page_params} from "./page_params"; import {page_params} from "./page_params";
import * as settings_data from "./settings_data";
import * as stream_ui_updates from "./stream_ui_updates"; import * as stream_ui_updates from "./stream_ui_updates";
import * as user_group_edit from "./user_group_edit"; 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 $input_element = $add_members_container.find(".input").expectOne();
const $button_element = $add_members_container.find('button[name="add_member"]').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); $input_element.prop("disabled", false);
$button_element.prop("disabled", false); $button_element.prop("disabled", false);
$button_element.css("pointer-events", ""); $button_element.css("pointer-events", "");

View File

@ -371,3 +371,36 @@ run_test("user_can_create_multiuse_invite", () => {
page_params.user_id = member_user_id; page_params.user_id = member_user_id;
assert.equal(settings_data.user_can_create_multiuse_invite(), false); 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));
});