diff --git a/static/js/settings_org.js b/static/js/settings_org.js index 883bf85b3e..487e6e45c1 100644 --- a/static/js/settings_org.js +++ b/static/js/settings_org.js @@ -924,6 +924,37 @@ function enable_or_disable_save_button($subsection_elem) { $subsection_elem.find(".subsection-changes-save button").prop("disabled", disable_save_btn); } +function populate_data_for_request(subsection, for_realm_default_settings) { + const data = {}; + const properties_elements = get_subsection_property_elements(subsection); + + for (const input_elem of properties_elements) { + const $input_elem = $(input_elem); + if (check_property_changed($input_elem, for_realm_default_settings)) { + const input_value = get_input_element_value($input_elem); + if (input_value !== undefined) { + let property_name; + if (for_realm_default_settings) { + property_name = extract_property_name($input_elem, for_realm_default_settings); + } else if ($input_elem.attr("id").startsWith("id_authmethod")) { + // Authentication Method component IDs include authentication method name + // for uniqueness, anchored to "id_authmethod" prefix, e.g. "id_authmethodapple_". + // We need to strip that whole construct down to extract the actual property name. + // The [\da-z]+ part of the regexp covers the auth method name itself. + // We assume it's not an empty string and can contain only digits and lowercase ASCII letters, + // this is ensured by a respective allowlist-based filter in populate_auth_methods(). + [, property_name] = /^id_authmethod[\da-z]+_(.*)$/.exec($input_elem.attr("id")); + } else { + [, property_name] = /^id_realm_(.*)$/.exec($input_elem.attr("id")); + } + data[property_name] = input_value; + } + } + } + + return data; +} + export function register_save_discard_widget_handlers( $container, patch_url, @@ -1035,42 +1066,6 @@ export function register_save_discard_widget_handlers( return data; } - function populate_data_for_request(subsection) { - const data = {}; - const properties_elements = get_subsection_property_elements(subsection); - - for (const input_elem of properties_elements) { - const $input_elem = $(input_elem); - if (check_property_changed($input_elem, for_realm_default_settings)) { - const input_value = get_input_element_value($input_elem); - if (input_value !== undefined) { - let property_name; - if (for_realm_default_settings) { - property_name = extract_property_name( - $input_elem, - for_realm_default_settings, - ); - } else if ($input_elem.attr("id").startsWith("id_authmethod")) { - // Authentication Method component IDs include authentication method name - // for uniqueness, anchored to "id_authmethod" prefix, e.g. "id_authmethodapple_". - // We need to strip that whole construct down to extract the actual property name. - // The [\da-z]+ part of the regexp covers the auth method name itself. - // We assume it's not an empty string and can contain only digits and lowercase ASCII letters, - // this is ensured by a respective allowlist-based filter in populate_auth_methods(). - [, property_name] = /^id_authmethod[\da-z]+_(.*)$/.exec( - $input_elem.attr("id"), - ); - } else { - [, property_name] = /^id_realm_(.*)$/.exec($input_elem.attr("id")); - } - data[property_name] = input_value; - } - } - } - - return data; - } - $container.on("click", ".subsection-header .subsection-changes-save button", (e) => { e.preventDefault(); e.stopPropagation(); @@ -1088,7 +1083,7 @@ export function register_save_discard_widget_handlers( } const data = { - ...populate_data_for_request($subsection_elem), + ...populate_data_for_request($subsection_elem, for_realm_default_settings), ...extra_data, }; save_organization_settings(data, $save_button, patch_url);