A platform for community discussion. Free, open, simple.
FEATURE: New site setting, whitelisted_link_domains
Robin Wardfd54c92a52c8
diff --git a/app/assets/javascripts/discourse/components/composer-editor.js.es6 b/app/assets/javascripts/discourse/components/composer-editor.js.es6index 86b4d39f87d..a93f95ffbad 100644--- a/app/assets/javascripts/discourse/components/composer-editor.js.es6+++ b/app/assets/javascripts/discourse/components/composer-editor.js.es6@@ -47,6 +47,11 @@ export default Ember.Component.extend({}},+ @computed+ showLink() {+ return this.currentUser && this.currentUser.get('link_posting_access') !== 'none';+ },+@computed('composer.requiredCategoryMissing', 'composer.replyLength')disableTextarea(requiredCategoryMissing, replyLength) {return requiredCategoryMissing && replyLength === 0;diff --git a/app/assets/javascripts/discourse/templates/components/composer-editor.hbs b/app/assets/javascripts/discourse/templates/components/composer-editor.hbsindex ebf9a3d856f..1e99d9108af 100644--- a/app/assets/javascripts/discourse/templates/components/composer-editor.hbs+++ b/app/assets/javascripts/discourse/templates/components/composer-editor.hbs@@ -11,7 +11,7 @@validation=validationloading=composer.loadingforcePreview=forcePreview- showLink=currentUser.can_post_link+ showLink=showLinkcomposerEvents=trueonExpandPopupMenuOptions="onExpandPopupMenuOptions"onPopupMenuAction=onPopupMenuActiondiff --git a/app/models/post_analyzer.rb b/app/models/post_analyzer.rbindex 5de5a83f8ec..90c81f096ed 100644--- a/app/models/post_analyzer.rb+++ b/app/models/post_analyzer.rb@@ -6,11 +6,11 @@ class PostAnalyzerdef initialize(raw, topic_id)@raw = raw@topic_id = topic_id- @found_oneboxes = false+ @onebox_urls = []enddef found_oneboxes?- @found_oneboxes+ @onebox_urls.present?enddef has_oneboxes?@@ -32,7 +32,7 @@ class PostAnalyzerendresult = Oneboxer.apply(cooked) do |url|- @found_oneboxes = true+ @onebox_urls << urlOneboxer.invalidate(url) if opts[:invalidate_oneboxes]Oneboxer.cached_onebox(url)end@@ -86,18 +86,20 @@ class PostAnalyzer# Count how many hosts are linked in the postdef linked_hosts- return {} if raw_links.blank?+ all_links = raw_links + @onebox_urls++ return {} if all_links.blank?return @linked_hosts if @linked_hosts.present?@linked_hosts = {}- raw_links.each do |u|+ all_links.each do |u|beginuri = self.class.parse_uri_rfc2396(u)host = uri.host@linked_hosts[host] ||= 1 unless host.nil?- rescue URI::InvalidURIError- # An invalid URI does not count as a raw link.+ rescue URI::InvalidURIError, URI::InvalidComponentError+ # An invalid URI does not count as a hostnextendenddiff --git a/app/serializers/current_user_serializer.rb b/app/serializers/current_user_serializer.rbindex 37395c5b8b2..58a078c68d0 100644--- a/app/serializers/current_user_serializer.rb+++ b/app/serializers/current_user_serializer.rb@@ -40,11 +40,11 @@ class CurrentUserSerializer < BasicUserSerializer:primary_group_id,:primary_group_name,:can_create_topic,- :can_post_link,+ :link_posting_access,:external_id- def can_post_link- scope.can_post_link?+ def link_posting_access+ scope.link_posting_accessenddef can_create_topicdiff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex acd82130269..8f1e6f0b6e4 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -1394,6 +1394,8 @@ en: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"+ whitelisted_link_domains: "Domains that users may link to even if they don't have the appropriate trust level to post links"+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."newuser_max_attachments: "How many attachments a new user can add to a post."diff --git a/config/site_settings.yml b/config/site_settings.ymlindex b972b8f80d4..1d8f9555e75 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -614,6 +614,9 @@ posting:newuser_max_replies_per_topic: 3newuser_max_mentions_per_post: 2title_max_word_length: 30+ whitelisted_link_domains:+ default: ''+ type: listnewuser_max_links: 2newuser_max_images:client: truediff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rbindex 81eb95f9f0e..ff7dbf2f15d 100644--- a/lib/guardian/post_guardian.rb+++ b/lib/guardian/post_guardian.rb@@ -1,9 +1,25 @@#mixin for all guardian methods dealing with post permissionsmodule PostGuardian- def can_post_link?- authenticated? &&- @user.has_trust_level?(TrustLevel[SiteSetting.min_trust_to_post_links])+ def unrestricted_link_posting?+ authenticated? && @user.has_trust_level?(TrustLevel[SiteSetting.min_trust_to_post_links])+ end++ def link_posting_access+ if unrestricted_link_posting?+ 'full'+ elsif SiteSetting.whitelisted_link_domains.present?+ 'limited'+ else+ 'none'+ end+ end++ def can_post_link?(host: nil)+ return false if host.blank?++ unrestricted_link_posting? ||+ SiteSetting.whitelisted_link_domains.split('|').include?(host)end# Can the user act on the post in a particular way.diff --git a/lib/validators/post_validator.rb b/lib/validators/post_validator.rbindex f0b7b54a014..f8c58ec2e64 100644--- a/lib/validators/post_validator.rb+++ b/lib/validators/post_validator.rb@@ -109,10 +109,12 @@ class Validators::PostValidator < ActiveModel::Validatorenddef can_post_links_validator(post)- if (post.link_count == 0 && !post.has_oneboxes?) ||- Guardian.new(post.acting_user).can_post_link? ||- private_message?(post)+ if (post.link_count == 0 && !post.has_oneboxes?) || private_message?(post)+ return newuser_links_validator(post)+ end+ guardian = Guardian.new(post.acting_user)+ if post.linked_hosts.keys.all? { |h| guardian.can_post_link?(host: h) }return newuser_links_validator(post)enddiff --git a/spec/components/guardian_spec.rb b/spec/components/guardian_spec.rbindex 5b29ff6f430..f0d2b03ec20 100644--- a/spec/components/guardian_spec.rb+++ b/spec/components/guardian_spec.rb@@ -26,21 +26,59 @@ describe Guardian doexpect { Guardian.new(user) }.not_to raise_errorend+ describe "link_posting_access" do+ it "is none for anonymous users" do+ expect(Guardian.new.link_posting_access).to eq('none')+ end++ it "is full for regular users" do+ expect(Guardian.new(user).link_posting_access).to eq('full')+ end++ it "is none for a user of a low trust level" do+ user.trust_level = 0+ SiteSetting.min_trust_to_post_links = 1+ expect(Guardian.new(user).link_posting_access).to eq('none')+ end++ it "is limited for a user of a low trust level with a whitelist" do+ SiteSetting.whitelisted_link_domains = 'example.com'+ user.trust_level = 0+ SiteSetting.min_trust_to_post_links = 1+ expect(Guardian.new(user).link_posting_access).to eq('limited')+ end+ end+describe "can_post_link?" do+ let(:host) { "discourse.org" }+it "returns false for anonymous users" do- expect(Guardian.new.can_post_link?).to eq(false)+ expect(Guardian.new.can_post_link?(host: host)).to eq(false)endit "returns true for a regular user" do- expect(Guardian.new(user).can_post_link?).to eq(true)+ expect(Guardian.new(user).can_post_link?(host: host)).to eq(true)endit "supports customization by site setting" douser.trust_level = 0SiteSetting.min_trust_to_post_links = 0- expect(Guardian.new(user).can_post_link?).to eq(true)+ expect(Guardian.new(user).can_post_link?(host: host)).to eq(true)SiteSetting.min_trust_to_post_links = 1- expect(Guardian.new(user).can_post_link?).to eq(false)+ expect(Guardian.new(user).can_post_link?(host: host)).to eq(false)+ end++ describe "whitelisted host" do+ before do+ SiteSetting.whitelisted_link_domains = host+ end++ it "allows a new user to post the link to the host" do+ user.trust_level = 0+ SiteSetting.min_trust_to_post_links = 1+ expect(Guardian.new(user).can_post_link?(host: host)).to eq(true)+ expect(Guardian.new(user).can_post_link?(host: 'another-host.com')).to eq(false)+ endendenddiff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rbindex 8a7d5776848..ee3ed2d57b2 100644--- a/spec/models/post_spec.rb+++ b/spec/models/post_spec.rb@@ -452,6 +452,13 @@ describe Post dopost_two_links.user.trust_level = TrustLevel[1]expect(post_one_link).not_to be_validend++ it "will skip the check for whitelisted domains" do+ SiteSetting.whitelisted_link_domains = 'www.bbc.co.uk'+ SiteSetting.min_trust_to_post_links = 2+ post_two_links.user.trust_level = TrustLevel[1]+ expect(post_one_link).to be_valid+ endendend