diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 89214a89674..a2a6dc88ae0 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -215,6 +215,7 @@ en:
one: "Sorry, new users can only put one attachment in a post."
other: "Sorry, new users can only put %{count} attachments in a post."
no_links_allowed: "Sorry, new users can't put links in posts."
+ links_require_trust: "Sorry, you can't include links in your posts."
too_many_links:
one: "Sorry, new users can only put one link in a post."
other: "Sorry, new users can only put %{count} links in a post."
@@ -1292,6 +1293,7 @@ en:
min_trust_to_send_email_messages: "The minimum trust level required to send new personal messages via email (to staged users)."
min_trust_to_flag_posts: "The minimum trust level required to flag posts"
+ min_trust_to_post_links: "The minimum trust level required to include links in posts"
newuser_max_links: "How many links a new user can add to a post."
newuser_max_images: "How many images a new user can add to a post."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index d283e027c25..f8ff2bb1a88 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -897,6 +897,9 @@ trust:
min_trust_to_flag_posts:
default: 1
enum: 'TrustLevelSetting'
+ min_trust_to_post_links:
+ default: 0
+ enum: 'TrustLevelSetting'
tl1_requires_topics_entered: 5
tl1_requires_read_posts:
default: 30
diff --git a/lib/validators/post_validator.rb b/lib/validators/post_validator.rb
index a9b1f468c45..354e0738de5 100644
--- a/lib/validators/post_validator.rb
+++ b/lib/validators/post_validator.rb
@@ -15,7 +15,8 @@ class Validators::PostValidator < ActiveModel::Validator
max_mention_validator(record)
max_images_validator(record)
max_attachments_validator(record)
- max_links_validator(record)
+ can_post_links_validator(record)
+ newuser_links_validator(record)
unique_post_validator(record)
end
@@ -91,8 +92,14 @@ class Validators::PostValidator < ActiveModel::Validator
add_error_if_count_exceeded(post, :no_attachments_allowed, :too_many_attachments, post.attachment_count, SiteSetting.newuser_max_attachments)
end
+ def can_post_links_validator(post)
+ return if acting_user_is_trusted?(post, SiteSetting.min_trust_to_post_links) || private_message?(post)
+
+ post.errors.add(:base, I18n.t(:links_require_trust))
+ end
+
# Ensure new users can not put too many links in a post
- def max_links_validator(post)
+ def newuser_links_validator(post)
return if acting_user_is_trusted?(post) || private_message?(post)
add_error_if_count_exceeded(post, :no_links_allowed, :too_many_links, post.link_count, SiteSetting.newuser_max_links)
end
@@ -113,8 +120,8 @@ class Validators::PostValidator < ActiveModel::Validator
private
- def acting_user_is_trusted?(post)
- post.acting_user.present? && post.acting_user.has_trust_level?(TrustLevel[1])
+ def acting_user_is_trusted?(post, level = 1)
+ post.acting_user.present? && post.acting_user.has_trust_level?(TrustLevel[level])
end
def private_message?(post)
diff --git a/spec/components/validators/post_validator_spec.rb b/spec/components/validators/post_validator_spec.rb
index 008e92fa16b..09cf5c2172d 100644
--- a/spec/components/validators/post_validator_spec.rb
+++ b/spec/components/validators/post_validator_spec.rb
@@ -184,7 +184,7 @@ describe Validators::PostValidator do
validator.expects(:max_mention_validator).never
validator.expects(:max_images_validator).never
validator.expects(:max_attachments_validator).never
- validator.expects(:max_links_validator).never
+ validator.expects(:newuser_links_validator).never
validator.expects(:unique_post_validator).never
validator.validate(post)
end
diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb
index 3582aa4db69..866cfe8867c 100644
--- a/spec/models/post_spec.rb
+++ b/spec/models/post_spec.rb
@@ -352,7 +352,7 @@ describe Post do
end
- describe "maximum links" do
+ describe "maximums" do
let(:newuser) { Fabricate(:user, trust_level: TrustLevel[0]) }
let(:post_one_link) { post_with_body("[sherlock](http://www.bbc.co.uk/programmes/b018ttws)", newuser) }
let(:post_two_links) { post_with_body("discourse twitter", newuser) }
@@ -391,11 +391,17 @@ describe Post do
end
end
- it "allows multiple images for basic accounts" do
+ it "allows multiple links for basic accounts" do
post_two_links.user.trust_level = TrustLevel[1]
expect(post_two_links).to be_valid
end
+ it "doesn't allow allow links if `min_trust_to_post_links` is not met" do
+ SiteSetting.min_trust_to_post_links = 2
+ post_two_links.user.trust_level = TrustLevel[1]
+ expect(post_one_link).not_to be_valid
+ end
+
end
end