From 1537f2caabb979b3a225e8932ba702cdf6bb39d9 Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Thu, 2 Oct 2025 13:40:52 -0300 Subject: [PATCH] FIX: Use persona's allowed groups for AI helper's custom prompts access (#35150) --- plugins/discourse-ai/config/locales/server.en.yml | 1 - plugins/discourse-ai/lib/ai_helper/entry_point.rb | 10 +++++++++- .../spec/system/ai_helper/ai_composer_helper_spec.rb | 7 ++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/plugins/discourse-ai/config/locales/server.en.yml b/plugins/discourse-ai/config/locales/server.en.yml index a1570d3f128..da5c41abba8 100644 --- a/plugins/discourse-ai/config/locales/server.en.yml +++ b/plugins/discourse-ai/config/locales/server.en.yml @@ -85,7 +85,6 @@ en: composer_ai_helper_allowed_groups: "Users on these groups will see the AI helper button in the composer." ai_helper_allowed_in_pm: "Enable the composer's AI helper in PMs." ai_helper_model: "Model to use for the AI helper." - ai_helper_custom_prompts_allowed_groups: "Users on these groups will see the custom prompt option in the AI helper." ai_helper_automatic_chat_thread_title_delay: "Delay in minutes before the AI helper automatically sets the chat thread title." ai_helper_automatic_chat_thread_title: "Automatically set the chat thread titles based on thread contents." ai_helper_illustrate_post_model: "Model to use for the composer AI helper's illustrate post feature" diff --git a/plugins/discourse-ai/lib/ai_helper/entry_point.rb b/plugins/discourse-ai/lib/ai_helper/entry_point.rb index e7b85f88763..ac240e9fe3e 100644 --- a/plugins/discourse-ai/lib/ai_helper/entry_point.rb +++ b/plugins/discourse-ai/lib/ai_helper/entry_point.rb @@ -16,7 +16,15 @@ module DiscourseAi end plugin.add_to_serializer(:current_user, :can_use_custom_prompts) do - scope.user.in_any_groups?(SiteSetting.ai_helper_custom_prompts_allowed_groups_map) + return [] if !SiteSetting.ai_helper_enabled + + custom_prompt_allowed_group_ids = + DB.query_single( + "SELECT allowed_group_ids FROM ai_personas WHERE id = :customp_prompt_persona_id", + customp_prompt_persona_id: SiteSetting.ai_helper_custom_prompt_persona, + ).flatten + + scope.user.in_any_groups?(custom_prompt_allowed_group_ids) end plugin.on(:chat_message_created) do |message, channel, user, extra| diff --git a/plugins/discourse-ai/spec/system/ai_helper/ai_composer_helper_spec.rb b/plugins/discourse-ai/spec/system/ai_helper/ai_composer_helper_spec.rb index 173fbe6ce9e..786944165eb 100644 --- a/plugins/discourse-ai/spec/system/ai_helper/ai_composer_helper_spec.rb +++ b/plugins/discourse-ai/spec/system/ai_helper/ai_composer_helper_spec.rb @@ -5,10 +5,15 @@ RSpec.describe "AI Composer helper", type: :system do fab!(:non_member_group) { Fabricate(:group) } fab!(:embedding_definition) + fab!(:custom_prompts_persona) do + Fabricate(:ai_persona, allowed_group_ids: [Group::AUTO_GROUPS[:admins]]) + end + before do enable_current_plugin Group.find_by(id: Group::AUTO_GROUPS[:admins]).add(user) assign_fake_provider_to(:ai_default_llm_model) + SiteSetting.ai_helper_custom_prompt_persona = custom_prompts_persona.id SiteSetting.ai_helper_enabled = true Jobs.run_immediately! sign_in(user) @@ -116,7 +121,7 @@ RSpec.describe "AI Composer helper", type: :system do context "when not a member of custom prompt group" do let(:mode) { DiscourseAi::AiHelper::Assistant::CUSTOM_PROMPT } - before { SiteSetting.ai_helper_custom_prompts_allowed_groups = non_member_group.id.to_s } + before { custom_prompts_persona.update!(allowed_group_ids: [non_member_group.id]) } it "does not show custom prompt option" do trigger_composer_helper(input)