diff --git a/frontend_tests/node_tests/dispatch.js b/frontend_tests/node_tests/dispatch.js index e9b10388a7..1e75fa3ddc 100644 --- a/frontend_tests/node_tests/dispatch.js +++ b/frontend_tests/node_tests/dispatch.js @@ -54,6 +54,10 @@ set_global('message_edit', { update_message_topic_editing_pencil: noop, }); +set_global('settings_bots', { + update_bot_permissions_ui: noop, +}); + // page_params is highly coupled to dispatching now set_global('page_params', {test_suite: false}); var page_params = global.page_params; @@ -195,11 +199,11 @@ var event_fixtures = { value: false, }, - realm__update__create_generic_bot_by_admins_only: { + realm__update__bot_creation_policy: { type: 'realm', op: 'update', - property: 'create_generic_bot_by_admins_only', - value: false, + property: 'bot_creation_policy', + value: 1, }, realm__update_dict__default: { diff --git a/static/js/admin.js b/static/js/admin.js index 13a3b0e83a..a34bcbac03 100644 --- a/static/js/admin.js +++ b/static/js/admin.js @@ -41,8 +41,6 @@ function _setup_page() { realm_email_changes_disabled: page_params.realm_email_changes_disabled, realm_add_emoji_by_admins_only: page_params.realm_add_emoji_by_admins_only, can_admin_emojis: page_params.is_admin || !page_params.realm_add_emoji_by_admins_only, - realm_create_generic_bot_by_admins_only: - page_params.realm_create_generic_bot_by_admins_only, realm_allow_message_deleting: page_params.realm_allow_message_deleting, realm_allow_message_editing: page_params.realm_allow_message_editing, realm_message_content_edit_limit_minutes: @@ -62,10 +60,14 @@ function _setup_page() { realm_send_welcome_emails: page_params.realm_send_welcome_emails, }; + options.bot_creation_policy_values = settings_bots.bot_creation_policy_values; var admin_tab = templates.render('admin_tab', options); $("#settings_content .organization-box").html(admin_tab); $("#settings_content .alert").removeClass("show"); + settings_bots.update_bot_settings_tip(); + $("#id_realm_bot_creation_policy").val(page_params.realm_bot_creation_policy); + // Since we just swapped in a whole new page, we need to // tell admin_sections nothing is loaded. admin_sections.reset_sections(); diff --git a/static/js/server_events_dispatch.js b/static/js/server_events_dispatch.js index d7eeb00301..c54f834027 100644 --- a/static/js/server_events_dispatch.js +++ b/static/js/server_events_dispatch.js @@ -56,7 +56,7 @@ exports.dispatch_normal_event = function dispatch_normal_event(event) { allow_edit_history: noop, allow_message_deleting: noop, allow_message_editing: noop, - create_generic_bot_by_admins_only: noop, + bot_creation_policy: settings_bots.update_bot_permissions_ui, create_stream_by_admins_only: noop, default_language: settings_org.reset_realm_default_language, description: settings_org.update_realm_description, diff --git a/static/js/settings.js b/static/js/settings.js index 5cf22943cf..6430bd5016 100644 --- a/static/js/settings.js +++ b/static/js/settings.js @@ -99,12 +99,17 @@ function _setup_page() { return tab; }()); + settings_bots.setup_bot_creation_policy_values(); + var settings_tab = templates.render('settings_tab', { full_name: people.my_full_name(), page_params: page_params, zuliprc: 'zuliprc', flaskbotrc: 'flaskbotrc', timezones: moment.tz.names(), + admin_only_bot_creation: page_params.is_admin || + page_params.realm_bot_creation_policy !== + settings_bots.bot_creation_policy_values.admins_only.code, }); $(".settings-box").html(settings_tab); diff --git a/static/js/settings_bots.js b/static/js/settings_bots.js index aa60348985..41b828f28b 100644 --- a/static/js/settings_bots.js +++ b/static/js/settings_bots.js @@ -83,6 +83,57 @@ exports.generate_flaskbotrc_content = function (email, api_key) { "\n"; }; +exports.bot_creation_policy_values = {}; + +exports.setup_bot_creation_policy_values = function () { + exports.bot_creation_policy_values = { + everyone: { + code: 1, + description: i18n.t("Everyone"), + }, + admins_only: { + code: 3, + description: i18n.t("Admins only"), + }, + restricted: { + code: 2, + description: i18n.t("Everyone, but only admins can add generic bots"), + }, + }; +}; + +exports.update_bot_settings_tip = function () { + var permission_type = exports.bot_creation_policy_values; + var current_permission = page_params.realm_bot_creation_policy; + var tip_text; + if (current_permission === permission_type.admins_only.code) { + tip_text = i18n.t("Only organization administrators can add bots to this organization"); + } else if (current_permission === permission_type.restricted.code) { + tip_text = i18n.t("Only orgainzation administrators can add generic bots"); + } else { + tip_text = i18n.t("Anyone in this organization can add bots"); + } + $(".bot-settings-tip").text(tip_text); +}; + +exports.update_bot_permissions_ui = function () { + exports.update_bot_settings_tip(); + $('#bot_table_error').hide(); + $("#id_realm_bot_creation_policy").val(page_params.realm_bot_creation_policy); + if (page_params.realm_bot_creation_policy === + exports.bot_creation_policy_values.admins_only.code && + !page_params.is_admin) { + $('#create_bot_form').hide(); + $('.add-a-new-bot-tab').hide(); + $('.account-api-key-section').hide(); + $("#bots_lists_navbar .active-bots-tab").click(); + } else { + $('#create_bot_form').show(); + $('.add-a-new-bot-tab').show(); + $('.account-api-key-section').show(); + } +}; + exports.set_up = function () { $('#payload_url_inputbox').hide(); $('#create_payload_url').val(''); @@ -393,6 +444,7 @@ exports.set_up = function () { $("#add-a-new-bot-form").hide(); $("#active_bots_list").show(); $("#inactive_bots_list").hide(); + $('#bot_table_error').hide(); }); $("#bots_lists_navbar .inactive-bots-tab").click(function (e) { diff --git a/static/js/settings_org.js b/static/js/settings_org.js index 6e70341269..94bfc4c40f 100644 --- a/static/js/settings_org.js +++ b/static/js/settings_org.js @@ -309,10 +309,9 @@ function _set_up() { checked_msg: i18n.t("Only administrators may now add new emoji!"), unchecked_msg: i18n.t("Any user may now add new emoji!"), }, - create_generic_bot_by_admins_only: { - type: 'bool', - checked_msg: i18n.t("Only administrators may now create new generic bots!"), - unchecked_msg: i18n.t("Any user may now create new generic bots!"), + bot_creation_policy: { + type: 'integer', + msg: i18n.t("Permissions changed"), }, }, }; @@ -361,7 +360,7 @@ function _set_up() { return; } - if (setting_type === 'text') { + if (setting_type === 'text' || setting_type === 'integer') { ui_report.success(field_info.msg, property_type_status_element(key)); return; diff --git a/static/styles/settings.css b/static/styles/settings.css index 07293d68c3..e38aec347f 100644 --- a/static/styles/settings.css +++ b/static/styles/settings.css @@ -353,7 +353,7 @@ input[type=checkbox] + .inline-block { margin-top: 10px; } -.organization-submission { +.org-settings-form .organization-submission { margin-top: 0px; } @@ -1192,7 +1192,8 @@ input[type=checkbox].inline-block { transform: translateY(-50%); } -#id_realm_create_stream_permission { +#id_realm_create_stream_permission, +#id_realm_bot_creation_policy { width: 100%; } diff --git a/static/templates/settings/bot-list-admin.handlebars b/static/templates/settings/bot-list-admin.handlebars index ccd6c680ba..17578d98da 100644 --- a/static/templates/settings/bot-list-admin.handlebars +++ b/static/templates/settings/bot-list-admin.handlebars @@ -1,4 +1,5 @@