Skip to content

A platform for community discussion. Free, open, simple.

FIX: staff should not be able to PM groups that "Nobody" can message (#16163)

Vinoth Kannan8a9a7a77d811
diff --git a/lib/guardian.rb b/lib/guardian.rb
index 719b192851b..43ac22b0bf2 100644
--- a/lib/guardian.rb
+++ b/lib/guardian.rb
@@ -47,6 +47,9 @@ class Guardian
def silenced?
false
end
+ def is_system_user?
+ false
+ end
def secure_category_ids
[]
end
@@ -422,6 +425,7 @@ class Guardian
def can_send_private_message?(target, notify_moderators: false)
is_user = target.is_a?(User)
is_group = target.is_a?(Group)
+ from_system = @user.is_system_user?
(is_group || is_user) &&
# User is authenticated
@@ -435,7 +439,7 @@ class Guardian
# Can't send PMs to suspended users
(is_staff? || is_group || !target.suspended?) &&
# Check group messageable level
- (is_staff? || is_user || Group.messageable(@user).where(id: target.id).exists? || notify_moderators) &&
+ (from_system || is_user || Group.messageable(@user).where(id: target.id).exists? || notify_moderators) &&
# Silenced users can only send PM to staff
(!is_silenced? || target.staff?)
end
diff --git a/spec/lib/guardian_spec.rb b/spec/lib/guardian_spec.rb
index 64d79ec2c1d..4eb53beb696 100644
--- a/spec/lib/guardian_spec.rb
+++ b/spec/lib/guardian_spec.rb
@@ -336,14 +336,13 @@ describe Guardian do
it "respects the group's messageable_level" do
Group::ALIAS_LEVELS.each do |level, _|
group.update!(messageable_level: Group::ALIAS_LEVELS[level])
- output = level == :everyone ? true : false
+ user_output = level == :everyone ? true : false
+ admin_output = level != :nobody
+ mod_output = [:nobody, :only_admins].exclude?(level)
- expect(Guardian.new(user).can_send_private_message?(group)).to eq(output)
- end
-
- Group::ALIAS_LEVELS.each do |level, _|
- group.update!(messageable_level: Group::ALIAS_LEVELS[level])
- expect(Guardian.new(admin).can_send_private_message?(group)).to eq(true)
+ expect(Guardian.new(user).can_send_private_message?(group)).to eq(user_output)
+ expect(Guardian.new(admin).can_send_private_message?(group)).to eq(admin_output)
+ expect(Guardian.new(moderator).can_send_private_message?(group)).to eq(mod_output)
end
end
diff --git a/spec/lib/topic_query_spec.rb b/spec/lib/topic_query_spec.rb
index 322735e6b19..6fa6367b884 100644
--- a/spec/lib/topic_query_spec.rb
+++ b/spec/lib/topic_query_spec.rb
@@ -1079,7 +1079,7 @@ describe TopicQuery do
end
let(:group_with_user) do
- group = Fabricate(:group)
+ group = Fabricate(:group, messageable_level: Group::ALIAS_LEVELS[:everyone])
group.add(user)
group.save
group