From 3cb25b019e2ab20156104dbf7ab44f901111fef7 Mon Sep 17 00:00:00 2001
From: Neil Lalonde <neillalonde@gmail.com>
Date: Fri, 19 Dec 2014 16:47:39 -0500
Subject: [PATCH] FIX: when private messages are disabled in settings, flag
 modal shouldn't show private message options

---
 lib/guardian/post_guardian.rb    | 6 ++++++
 spec/components/guardian_spec.rb | 7 +++++++
 2 files changed, 13 insertions(+)

diff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rb
index 114bb141281..06ab6a4fc4f 100644
--- a/lib/guardian/post_guardian.rb
+++ b/lib/guardian/post_guardian.rb
@@ -9,6 +9,9 @@ module PostGuardian
     already_did_flagging      = taken.any? && (taken & PostActionType.flag_types.values).any?
 
     if authenticated? && post
+
+      return false if action_key == :notify_moderators && !SiteSetting.enable_private_messages
+
       # we allow flagging for trust level 1 and higher
       (is_flag && @user.has_trust_level?(TrustLevel[1]) && not(already_did_flagging)) ||
 
@@ -27,6 +30,9 @@ module PostGuardian
       # new users can't notify_user because they are not allowed to send private messages
       not(action_key == :notify_user && !@user.has_trust_level?(TrustLevel[1])) &&
 
+      # can't send private messages if they're disabled globally
+      not(action_key == :notify_user && !SiteSetting.enable_private_messages) &&
+
       # no voting more than once on single vote topics
       not(action_key == :vote && opts[:voted_in_topic] && post.topic.has_meta_data_boolean?(:single_vote))
     end
diff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rb
index d5ec6ccf450..cd0cc20aa74 100644
--- a/spec/components/guardian_spec.rb
+++ b/spec/components/guardian_spec.rb
@@ -62,6 +62,13 @@ describe Guardian do
       Guardian.new(user).post_can_act?(post, :off_topic, taken_actions: {PostActionType.types[:spam] => 1}).should be_falsey
     end
 
+    it "returns false for notify_user if private messages are disabled" do
+      SiteSetting.stubs(:enable_private_messages).returns(false)
+      user.trust_level = TrustLevel[2]
+      Guardian.new(user).post_can_act?(post, :notify_user).should be_falsey
+      Guardian.new(user).post_can_act?(post, :notify_moderators).should be_falsey
+    end
+
     describe "trust levels" do
       it "returns true for a new user liking something" do
         user.trust_level = TrustLevel[0]