diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 96bdd775b22..c326faf39c8 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -207,6 +207,7 @@ en: too_many_mentions_newuser: one: "Sorry, new users can only mention one other user in a post." other: "Sorry, new users can only mention %{count} users in a post." + no_images_allowed_trust: "Sorry, you can't put images in a post" no_images_allowed: "Sorry, new users can't put images in posts." too_many_images: one: "Sorry, new users can only put one image in a post." @@ -1303,6 +1304,7 @@ en: 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" + min_trust_to_post_images: "The minimum trust level required to include images in a post" 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 c3bff313e51..6a2cf5f3fc8 100644 --- a/config/site_settings.yml +++ b/config/site_settings.yml @@ -911,6 +911,9 @@ trust: min_trust_to_post_links: default: 0 enum: 'TrustLevelSetting' + min_trust_to_post_images: + default: 0 + enum: 'TrustLevelSetting' allow_flagging_staff: true tl1_requires_topics_entered: 5 tl1_requires_read_posts: diff --git a/lib/validators/post_validator.rb b/lib/validators/post_validator.rb index 18e8d4330d3..076dd2d086c 100644 --- a/lib/validators/post_validator.rb +++ b/lib/validators/post_validator.rb @@ -82,8 +82,25 @@ class Validators::PostValidator < ActiveModel::Validator # Ensure new users can not put too many images in a post def max_images_validator(post) - return if acting_user_is_trusted?(post) || private_message?(post) - add_error_if_count_exceeded(post, :no_images_allowed, :too_many_images, post.image_count, SiteSetting.newuser_max_images) + return if post.acting_user.blank? + + if post.acting_user.trust_level < TrustLevel[SiteSetting.min_trust_to_post_images] + add_error_if_count_exceeded( + post, + :no_images_allowed_trust, + :no_images_allowed_trust, + post.image_count, + 0 + ) + elsif post.acting_user.trust_level == TrustLevel[0] + add_error_if_count_exceeded( + post, + :no_images_allowed, + :too_many_images, + post.image_count, + SiteSetting.newuser_max_images + ) + end end # Ensure new users can not put too many attachments in a post diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 8ec72679688..9ccf9a80df2 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -188,6 +188,22 @@ describe Post do expect(post_with_avatars.image_count).to eq(0) end + it "allows images by default" do + expect(post_one_image).to be_valid + end + + it "doesn't allow more than `min_trust_to_post_images`" do + SiteSetting.min_trust_to_post_images = 4 + post_one_image.user.trust_level = 3 + expect(post_one_image).not_to be_valid + end + + it "doesn't allow more than `min_trust_to_post_images`" do + SiteSetting.min_trust_to_post_images = 4 + post_one_image.user.trust_level = 4 + expect(post_one_image).to be_valid + end + it "doesn't count favicons as images" do PrettyText.stubs(:cook).returns(post_with_favicon.raw) expect(post_with_favicon.image_count).to eq(0)