From 0eca9cb3193f97042a68e51a27acbb8e30aa55a6 Mon Sep 17 00:00:00 2001 From: Ujjawal Modi Date: Fri, 3 Mar 2023 18:44:01 +0530 Subject: [PATCH] bots: Improve bots permission banner. Added a separate template file for banner messages in bots panel under personal and organisation settings. Banners for bots panel under personal settings are shown only when user cannot add bots. Banners for bots panel under organisation settings are shown only when user cannot add bots or user is administrator. Fixes #24155 --- web/src/admin.js | 2 +- web/src/settings.js | 1 + web/src/settings_bots.js | 36 +++++++++++++-------- web/templates/settings/bot_list_admin.hbs | 3 +- web/templates/settings/bot_settings.hbs | 3 +- web/templates/settings/bot_settings_tip.hbs | 7 ++++ 6 files changed, 35 insertions(+), 17 deletions(-) create mode 100644 web/templates/settings/bot_settings_tip.hbs diff --git a/web/src/admin.js b/web/src/admin.js index 51ddad1318..f1eb4849e9 100644 --- a/web/src/admin.js +++ b/web/src/admin.js @@ -218,7 +218,7 @@ export function build_page() { $("#settings_content .organization-box").html(rendered_admin_tab); $("#settings_content .alert").removeClass("show"); - settings_bots.update_bot_settings_tip(); + settings_bots.update_bot_settings_tip($("#admin-bot-settings-tip"), true); settings_invites.update_invite_users_setting_tip(); insert_tip_box(); diff --git a/web/src/settings.js b/web/src/settings.js index 5198b4ec7e..c19b7c0d48 100644 --- a/web/src/settings.js +++ b/web/src/settings.js @@ -114,6 +114,7 @@ export function build_page() { owner_is_only_user_in_organization: people.get_active_human_count() === 1, }); + settings_bots.update_bot_settings_tip($("#personal-bot-settings-tip"), false); $(".settings-box").html(rendered_settings_tab); } diff --git a/web/src/settings_bots.js b/web/src/settings_bots.js index b264f2fe35..e181f3fe4e 100644 --- a/web/src/settings_bots.js +++ b/web/src/settings_bots.js @@ -4,6 +4,7 @@ import $ from "jquery"; import render_settings_deactivation_bot_modal from "../templates/confirm_dialog/confirm_deactivate_bot.hbs"; import render_add_new_bot_form from "../templates/settings/add_new_bot_form.hbs"; import render_bot_avatar_row from "../templates/settings/bot_avatar_row.hbs"; +import render_bot_settings_tip from "../templates/settings/bot_settings_tip.hbs"; import render_edit_bot_form from "../templates/settings/edit_bot_form.hbs"; import render_settings_edit_embedded_bot_service from "../templates/settings/edit_embedded_bot_service.hbs"; import render_settings_edit_outgoing_webhook_service from "../templates/settings/edit_outgoing_webhook_service.hbs"; @@ -156,20 +157,26 @@ export function can_create_new_bots() { return page_params.realm_bot_creation_policy !== bot_creation_policy_values.admins_only.code; } -export function update_bot_settings_tip() { - const permission_type = bot_creation_policy_values; - const current_permission = page_params.realm_bot_creation_policy; - let tip_text; - if (current_permission === permission_type.admins_only.code) { - tip_text = $t({ - defaultMessage: "Only organization administrators can add bots to this organization.", - }); - } else if (current_permission === permission_type.restricted.code) { - tip_text = $t({defaultMessage: "Only organization administrators can add generic bots."}); - } else { - tip_text = $t({defaultMessage: "Anyone in this organization can add bots."}); +export function update_bot_settings_tip($tip_container, for_org_settings) { + if ( + !page_params.is_admin && + page_params.realm_bot_creation_policy === bot_creation_policy_values.everyone.code + ) { + $tip_container.hide(); + return; } - $(".bot-settings-tip").text(tip_text); + + if (page_params.is_admin && !for_org_settings) { + $tip_container.hide(); + return; + } + + const rendered_tip = render_bot_settings_tip({ + realm_bot_creation_policy: page_params.realm_bot_creation_policy, + permission_type: bot_creation_policy_values, + }); + $tip_container.show(); + $tip_container.html(rendered_tip); } function update_add_bot_button() { @@ -183,7 +190,8 @@ function update_add_bot_button() { } export function update_bot_permissions_ui() { - update_bot_settings_tip(); + update_bot_settings_tip($("#admin-bot-settings-tip"), true); + update_bot_settings_tip($("#personal-bot-settings-tip"), false); update_add_bot_button(); $("#id_realm_bot_creation_policy").val(page_params.realm_bot_creation_policy); } diff --git a/web/templates/settings/bot_list_admin.hbs b/web/templates/settings/bot_list_admin.hbs index 14c4dc5837..9f912a9fad 100644 --- a/web/templates/settings/bot_list_admin.hbs +++ b/web/templates/settings/bot_list_admin.hbs @@ -1,5 +1,6 @@
-
+
+
diff --git a/web/templates/settings/bot_settings.hbs b/web/templates/settings/bot_settings.hbs index beafdb1c44..85ec703a8f 100644 --- a/web/templates/settings/bot_settings.hbs +++ b/web/templates/settings/bot_settings.hbs @@ -8,7 +8,8 @@ {{#*inline "z-api"}}{{> @partial-block}}{{/inline}} {{/tr}}
-
+
+
diff --git a/web/templates/settings/bot_settings_tip.hbs b/web/templates/settings/bot_settings_tip.hbs new file mode 100644 index 0000000000..711bc9b888 --- /dev/null +++ b/web/templates/settings/bot_settings_tip.hbs @@ -0,0 +1,7 @@ +{{#if (eq realm_bot_creation_policy permission_type.admins_only.code)}} +
{{t "Only organization administrators can add bots to this organization." }}
+{{else if (eq realm_bot_creation_policy permission_type.restricted.code) }} +
{{t "Only organization administrators can add generic bots." }}
+{{else}} +
{{t "Anyone in this organization can add bots." }}
+{{/if}}