A platform for community discussion. Free, open, simple.
FEATURE: Add setting to prevent anonymous users from using chat (#31842)
Osama Sayegha8b3e539c763
diff --git a/plugins/chat/app/validators/chat/allow_chat_in_anonymous_mode_validator.rb b/plugins/chat/app/validators/chat/allow_chat_in_anonymous_mode_validator.rbnew file mode 100644index 00000000000..92c9afd2f02--- /dev/null+++ b/plugins/chat/app/validators/chat/allow_chat_in_anonymous_mode_validator.rb@@ -0,0 +1,20 @@+# frozen_string_literal: true++module Chat+ class AllowChatInAnonymousModeValidator+ def initialize(opts = {})+ @opts = opts+ end++ def valid_value?(val)+ return true if val == "f"+ return true if SiteSetting.allow_anonymous_mode++ false+ end++ def error_message+ I18n.t("site_settings.errors.allow_chat_in_anonymous_mode_invalid")+ end+ end+enddiff --git a/plugins/chat/config/locales/server.en.yml b/plugins/chat/config/locales/server.en.ymlindex 91ac5c90a3d..67a80b4d0d6 100644--- a/plugins/chat/config/locales/server.en.yml+++ b/plugins/chat/config/locales/server.en.yml@@ -24,10 +24,12 @@ en:chat_editing_grace_period_max_diff_low_trust: "Maximum number of character changes allowed in chat editing grace period, if more changed display the (edited) tag by the chat message (trust level 0 and 1)."chat_editing_grace_period_max_diff_high_trust: "Maximum number of character changes allowed in chat editing grace period, if more changed display the (edited) tag by the chat message (trust level 2 and up)."chat_preferred_index: "Preferred tab when loading /chat."+ allow_chat_in_anonymous_mode: "Enable this setting to allow users who are browsing your site anonymously to use chat. This setting requires the `allow anonymous mode` setting to be enabled."errors:chat_default_channel: "The default chat channel must be a public channel."direct_message_enabled_groups_invalid: "You must specify at least one group for this setting. If you do not want anyone except staff to send direct messages, choose the staff group."chat_upload_not_allowed_secure_uploads: "Chat uploads are not allowed when secure uploads site setting is enabled."+ allow_chat_in_anonymous_mode_invalid: "This setting requires the 'allow anonymous mode' setting to be enabled first."system_messages:private_channel_title: "Channel %{id}"chat_channel_archive_complete:diff --git a/plugins/chat/config/settings.yml b/plugins/chat/config/settings.ymlindex 2f396475162..1e3f77b01f3 100644--- a/plugins/chat/config/settings.yml+++ b/plugins/chat/config/settings.yml@@ -145,3 +145,6 @@ chat:- channels- direct_messages- my_threads+ allow_chat_in_anonymous_mode:+ default: false+ validator: "Chat::AllowChatInAnonymousModeValidator"diff --git a/plugins/chat/lib/chat/guardian_extensions.rb b/plugins/chat/lib/chat/guardian_extensions.rbindex 78d6ccc55e0..cbcbbf1cbf8 100644--- a/plugins/chat/lib/chat/guardian_extensions.rb+++ b/plugins/chat/lib/chat/guardian_extensions.rb@@ -13,7 +13,14 @@ module Chatdef can_chat?return false if anonymous?- @user.bot? || @user.in_any_groups?(Chat.allowed_group_ids)+ return true if @user.bot?++ if @user.anonymous?+ SiteSetting.allow_chat_in_anonymous_mode &&+ AnonymousShadowCreator.get_master(@user)&.guardian&.can_chat?+ else+ @user.in_any_groups?(Chat.allowed_group_ids)+ endenddef can_direct_message?diff --git a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rbindex 88e64ebc07f..ce0a0f2beba 100644--- a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb+++ b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb@@ -10,7 +10,7 @@ RSpec.describe Chat::GuardianExtensions dolet(:guardian) { Guardian.new(user) }let(:staff_guardian) { Guardian.new(staff) }- before { SiteSetting.chat_allowed_groups = chatters }+ before { SiteSetting.chat_allowed_groups = chatters.id }describe "#can_chat?" docontext "when the user is not in allowed to chat" do@@ -45,6 +45,48 @@ RSpec.describe Chat::GuardianExtensions doendend+ context "when user is a shadow account" do+ fab!(:non_chatter) { Fabricate(:user, refresh_auto_groups: true) }++ before { SiteSetting.allow_anonymous_mode = true }++ context "when allow_chat_in_anonymous_mode is true" do+ before { SiteSetting.allow_chat_in_anonymous_mode = true }++ it "allows shadow account to chat if master account can chat" do+ anonymous = AnonymousShadowCreator.get(user)+ expect(anonymous.id).to be_present+ expect(Guardian.new(anonymous).can_chat?).to eq(true)+ expect(Guardian.new(user).can_chat?).to eq(true)+ end++ it "doesn't allow shadow account to chat if master account can not chat" do+ anonymous = AnonymousShadowCreator.get(non_chatter)+ expect(anonymous.id).to be_present+ expect(Guardian.new(anonymous).can_chat?).to eq(false)+ expect(Guardian.new(non_chatter).can_chat?).to eq(false)+ end+ end++ context "when allow_chat_in_anonymous_mode is false" do+ before { SiteSetting.allow_chat_in_anonymous_mode = false }++ it "doesn't allow shadow account to chat even if master account can chat" do+ anonymous = AnonymousShadowCreator.get(user)+ expect(anonymous.id).to be_present+ expect(Guardian.new(anonymous).can_chat?).to eq(false)+ expect(Guardian.new(user).can_chat?).to eq(true)+ end++ it "doesn't allow shadow account to chat if master account can not chat" do+ anonymous = AnonymousShadowCreator.get(non_chatter)+ expect(anonymous.id).to be_present+ expect(Guardian.new(anonymous).can_chat?).to eq(false)+ expect(Guardian.new(non_chatter).can_chat?).to eq(false)+ end+ end+ end+it "allows TL1 to chat by default and by extension higher trust levels" doexpect(guardian.can_chat?).to eq(true)user.change_trust_level!(TrustLevel[3])@@ -698,7 +740,7 @@ RSpec.describe Chat::GuardianExtensions doenddescribe "#recipient_can_chat?" do- fab!(:other_user) { Fabricate(:user) }+ fab!(:other_user) { Fabricate(:user, groups: [chatters]) }fab!(:dm_channel) { Fabricate(:direct_message_channel, users: [user, other_user]) }alias_matcher :be_able_to_chat, :be_recipient_can_chatdiff --git a/plugins/chat/spec/validators/allow_chat_in_anonymous_mode_validator_spec.rb b/plugins/chat/spec/validators/allow_chat_in_anonymous_mode_validator_spec.rbnew file mode 100644index 00000000000..716d67b3a8c--- /dev/null+++ b/plugins/chat/spec/validators/allow_chat_in_anonymous_mode_validator_spec.rb@@ -0,0 +1,30 @@+# frozen_string_literal: true++RSpec.describe Chat::AllowChatInAnonymousModeValidator do+ context "when anonymous mode is disabled" do+ before { SiteSetting.allow_anonymous_mode = false }++ it "does not allow the allow_chat_in_anonymous_mode setting to be enabled" do+ expect { SiteSetting.allow_chat_in_anonymous_mode = true }.to raise_error(+ Discourse::InvalidParameters,+ "allow_chat_in_anonymous_mode: #{I18n.t("site_settings.errors.allow_chat_in_anonymous_mode_invalid")}",+ )+ end++ it "allows the setting to be disabled" do+ SiteSetting.allow_anonymous_mode = true+ SiteSetting.allow_chat_in_anonymous_mode = true++ SiteSetting.allow_anonymous_mode = false+ expect { SiteSetting.allow_likes_in_anonymous_mode = false }.not_to raise_error+ end+ end++ context "when anonymous mode is enabled" do+ before { SiteSetting.allow_anonymous_mode = true }++ it "allows the setting to be enabled" do+ expect { SiteSetting.allow_likes_in_anonymous_mode = true }.not_to raise_error+ end+ end+end