settings: Use server_supported_permission_settings in webapp.

Since we now get the configuration for group permission settings
from server, we can directly use that in webapp instead of
defining it again.
This commit is contained in:
Sahil Batra 2023-10-31 09:06:05 +05:30 committed by Tim Abbott
parent 47c8e369cf
commit c756259094
8 changed files with 65 additions and 35 deletions

View File

@ -1,38 +1,14 @@
type GroupPermissionSetting = {
require_system_group: boolean;
allow_internet_group: boolean;
allow_owners_group: boolean;
allow_nobody_group: boolean;
allow_everyone_group: boolean;
};
const group_permission_config_dict = new Map<string, GroupPermissionSetting>([
[
"can_remove_subscribers_group",
{
require_system_group: true,
allow_internet_group: false,
allow_owners_group: false,
allow_nobody_group: false,
allow_everyone_group: true,
},
],
[
"create_multiuse_invite_group",
{
require_system_group: true,
allow_internet_group: false,
allow_owners_group: false,
allow_nobody_group: true,
allow_everyone_group: false,
},
],
]);
import {page_params} from "./page_params";
import type {GroupPermissionSetting} from "./types";
export function get_group_permission_setting_config(
setting_name: string,
setting_type: "realm" | "stream" | "group",
): GroupPermissionSetting | undefined {
const permission_config_dict = group_permission_config_dict.get(setting_name);
const permission_settings_dict = page_params.server_supported_permission_settings;
const permission_config_dict = permission_settings_dict[setting_type][setting_name];
if (!permission_config_dict) {
throw new Error(`Invalid setting: ${setting_name}`);
}

View File

@ -1,5 +1,7 @@
import $ from "jquery";
import type {GroupPermissionSetting} from "./types";
const t1 = performance.now();
export const page_params: {
apps_page_url: string;
@ -93,6 +95,11 @@ export const page_params: {
server_sentry_environment: string | undefined;
server_sentry_sample_rate: number | undefined;
server_sentry_trace_rate: number | undefined;
server_supported_permission_settings: {
realm: Record<string, GroupPermissionSetting>;
stream: Record<string, GroupPermissionSetting>;
group: Record<string, GroupPermissionSetting>;
};
server_web_public_streams_enabled: boolean;
show_billing: boolean;
show_plans: boolean;

View File

@ -714,6 +714,7 @@ export function init_dropdown_widgets() {
get_options: () =>
user_groups.get_realm_user_groups_for_dropdown_list_widget(
"create_multiuse_invite_group",
"realm",
),
$events_container: $("#settings_overlay_container #organization-permissions"),
item_click_callback(event, dropdown) {

View File

@ -196,6 +196,7 @@ function setup_dropdown(sub, slim_sub) {
get_options: () =>
user_groups.get_realm_user_groups_for_dropdown_list_widget(
"can_remove_subscribers_group",
"stream",
),
item_click_callback(event, dropdown) {
dropdown.hide();

View File

@ -66,6 +66,7 @@ export function dropdown_setup() {
get_options: () =>
user_groups.get_realm_user_groups_for_dropdown_list_widget(
"can_remove_subscribers_group",
"stream",
),
item_click_callback(event, dropdown) {
dropdown.hide();

View File

@ -192,3 +192,14 @@ export type UpdateMessageEvent = {
// TODO/typescript: Move the User and Stream placeholder
// types to their appropriate modules.
export type User = Record<string, never>;
export type GroupPermissionSetting = {
require_system_group: boolean;
allow_internet_group: boolean;
allow_owners_group: boolean;
allow_nobody_group: boolean;
allow_everyone_group: boolean;
default_group_name: string;
id_field_name: string;
default_for_system_groups: string | null;
};

View File

@ -223,9 +223,12 @@ export function is_user_in_group(user_group_id: number, user_id: number): boolea
export function get_realm_user_groups_for_dropdown_list_widget(
setting_name: string,
setting_type: "realm" | "stream" | "group",
): UserGroupForDropdownListWidget[] {
const group_setting_config =
group_permission_settings.get_group_permission_setting_config(setting_name);
const group_setting_config = group_permission_settings.get_group_permission_setting_config(
setting_name,
setting_type,
);
if (group_setting_config === undefined) {
return [];

View File

@ -5,6 +5,7 @@ const {strict: assert} = require("assert");
const {zrequire} = require("./lib/namespace");
const {run_test} = require("./lib/test");
const blueslip = require("./lib/zblueslip");
const {page_params} = require("./lib/zpage_params");
const user_groups = zrequire("user_groups");
@ -317,6 +318,31 @@ run_test("get_realm_user_groups_for_dropdown_list_widget", () => {
direct_subgroup_ids: new Set([4, 5]),
};
page_params.server_supported_permission_settings = {
stream: {
can_remove_subscribers_group: {
require_system_group: true,
allow_internet_group: false,
allow_owners_group: false,
allow_nobody_group: false,
allow_everyone_group: true,
default_group_name: "role:administrators",
id_field_name: "can_remove_subscribers_group_id",
},
},
realm: {
create_multiuse_invite_group: {
require_system_group: true,
allow_internet_group: false,
allow_owners_group: false,
allow_nobody_group: true,
allow_everyone_group: false,
default_group_name: "role:administrators",
id_field_name: "create_multiuse_invite_group_id",
},
},
};
const expected_groups_list = [
{name: "translated: Admins, moderators, members and guests", unique_id: 6},
{name: "translated: Admins, moderators and members", unique_id: 5},
@ -340,12 +366,16 @@ run_test("get_realm_user_groups_for_dropdown_list_widget", () => {
});
assert.deepEqual(
user_groups.get_realm_user_groups_for_dropdown_list_widget("can_remove_subscribers_group"),
user_groups.get_realm_user_groups_for_dropdown_list_widget(
"can_remove_subscribers_group",
"stream",
),
expected_groups_list,
);
assert.throws(
() => user_groups.get_realm_user_groups_for_dropdown_list_widget("invalid_setting"),
() =>
user_groups.get_realm_user_groups_for_dropdown_list_widget("invalid_setting", "stream"),
{
name: "Error",
message: "Invalid setting: invalid_setting",