A platform for community discussion. Free, open, simple.
FIX: Serialize visible `allowed_tag_groups` to all users (#42654)
Gabriel Grubbabba80124b4f1
diff --git a/app/serializers/category_serializer.rb b/app/serializers/category_serializer.rbindex dca23e7db43..cbe8964e45f 100644--- a/app/serializers/category_serializer.rb+++ b/app/serializers/category_serializer.rb@@ -194,10 +194,12 @@ class CategorySerializer < SiteCategorySerializerenddef include_allowed_tag_groups?- can_edit_tags?+ SiteSetting.tagging_enabledenddef allowed_tag_groups- object.tag_groups.map(&:name)+ return object.tag_groups.map(&:name) if can_edit_tags?++ TagGroup.visible(scope || Guardian.new).where(id: object.tag_groups.map(&:id)).pluck(:name)endenddiff --git a/spec/serializers/category_serializer_spec.rb b/spec/serializers/category_serializer_spec.rbindex ea0ffb46176..189df97a2de 100644--- a/spec/serializers/category_serializer_spec.rb+++ b/spec/serializers/category_serializer_spec.rb@@ -398,14 +398,25 @@ RSpec.describe CategorySerializer dosubject(:json) { described_class.new(category, scope: scope, root: false).as_json }fab!(:attached_tag_group) { Fabricate(:tag_group, name: "category-allowed-group") }+ fab!(:restricted_tag_group) do+ Fabricate(:tag_group, name: "category-restricted-group", permissions: { "staff" => 1 })+ end- before { category.tag_groups << attached_tag_group }+ before { category.tag_groups << [attached_tag_group, restricted_tag_group] }context "for a non-editor" dolet(:scope) { user.guardian }- it "is not included" do- expect(json).not_to have_key(:allowed_tag_groups)+ it "includes only the names of tag groups visible to the user" do+ expect(json[:allowed_tag_groups]).to contain_exactly(attached_tag_group.name)+ end+ end++ context "for an anonymous user" do+ let(:scope) { Guardian.new }++ it "includes only the names of publicly visible tag groups" do+ expect(json[:allowed_tag_groups]).to contain_exactly(attached_tag_group.name)endend@@ -413,7 +424,10 @@ RSpec.describe CategorySerializer dolet(:scope) { admin.guardian }it "is included with all tag-group names" do- expect(json[:allowed_tag_groups]).to contain_exactly(attached_tag_group.name)+ expect(json[:allowed_tag_groups]).to contain_exactly(+ attached_tag_group.name,+ restricted_tag_group.name,+ )endend