diff --git a/app/assets/javascripts/discourse/components/tag-chooser.js.es6 b/app/assets/javascripts/discourse/components/tag-chooser.js.es6 index ce99050e5..64d5fc8e6 100644 --- a/app/assets/javascripts/discourse/components/tag-chooser.js.es6 +++ b/app/assets/javascripts/discourse/components/tag-chooser.js.es6 @@ -43,6 +43,8 @@ export default Ember.TextField.extend({ this.set('allowCreate', site.get('can_create_tag')); } + this.set('termMatchesForbidden', false); + if (this.get('unlimitedTagCount')) { limit = null; } else if (this.get('limit')) { @@ -78,7 +80,7 @@ export default Ember.TextField.extend({ term = term.replace(filterRegexp, '').trim(); // No empty terms, make sure the user has permission to create the tag - if (!term.length || !self.get('allowCreate')) return; + if (!term.length || !self.get('allowCreate') || self.get('termMatchesForbidden')) return; if ($(data).filter(function() { return this.text.localeCompare(term) === 0; @@ -119,6 +121,7 @@ export default Ember.TextField.extend({ if (self.siteSettings.tags_sort_alphabetically) { data.results = data.results.sort(function(a,b) { return a.id > b.id; }); } + self.set('termMatchesForbidden', data.forbidden ? true : false); return data; } }, diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index c5b9a44c5..d4290eaeb 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -156,7 +156,15 @@ class TagsController < ::ApplicationController tags << { id: t.name, text: t.name, count: 0 } end - render json: { results: tags } + json_response = { results: tags } + + t = DiscourseTagging.clean_tag(params[:q]) + if Tag.where(name: t).exists? && !tags.find { |h| h[:id] == t } + # filter_allowed_tags determined that the tag entered is not allowed + json_response[:forbidden] = t + end + + render json: json_response end def notifications diff --git a/spec/controllers/tags_controller_spec.rb b/spec/controllers/tags_controller_spec.rb index c79022a1e..82ea6c13a 100644 --- a/spec/controllers/tags_controller_spec.rb +++ b/spec/controllers/tags_controller_spec.rb @@ -39,4 +39,38 @@ describe TagsController do end end end + + describe 'search' do + context 'tagging disabled' do + it "returns 404" do + xhr :get, :search, q: 'stuff' + expect(response.status).to eq(404) + end + end + + context 'tagging enabled' do + before do + SiteSetting.tagging_enabled = true + end + + it "can return some tags" do + tag_names = ['stuff', 'stinky', 'stumped'] + tag_names.each { |name| Fabricate(:tag, name: name) } + xhr :get, :search, q: 'stu' + expect(response).to be_success + json = ::JSON.parse(response.body) + expect(json["results"].map{|j| j["id"]}.sort).to eq(['stuff', 'stumped']) + end + + it "can say if given tag is not allowed" do + yup, nope = Fabricate(:tag, name: 'yup'), Fabricate(:tag, name: 'nope') + category = Fabricate(:category, tags: [yup]) + xhr :get, :search, q: 'nope', categoryId: category.id + expect(response).to be_success + json = ::JSON.parse(response.body) + expect(json["results"].map{|j| j["id"]}.sort).to eq([]) + expect(json["forbidden"]).to be_present + end + end + end end