FIX: Seed all categories and tags configured as defaults for nav menu (#22793)

Context of this change:

There are two site settings which an admin can configured to set the
default categories and tags that are shown for a new user. `default_navigation_menu_categories`
is used to determine the default categories while
`default_navigation_menu_tags` is used to determine the default tags.

Prior to this change when seeding the defaults, we will filter out the
categories/tags that the user do not have permission to see. However,
this means that when the user does eventually gain permission down the
line, the default categories and tags do not appear.

What does this change do?

With this commit, we have changed it such that all the categories and tags
configured in the `default_navigation_menu_categories` and
`default_navigation_menu_tags` site settings are seeded regardless of
whether the user's visibility of the categories or tags. During
serialization, we will then filter out the categories and tags which the
user does not have visibility of.
This commit is contained in:
Alan Guo Xiang Tan
2023-07-27 10:52:33 +08:00
committed by GitHub
parent f3db8579d6
commit 0a56274596
8 changed files with 117 additions and 160 deletions

View File

@ -52,76 +52,18 @@ RSpec.describe User do
SiteSetting.default_navigation_menu_tags = "#{tag.name}|#{hidden_tag.name}"
end
it "creates the right sidebar section link records for categories and tags that a user can see" do
it "creates sidebar section link records for categories and tags that have been configured as defaults" do
user = Fabricate(:user)
expect(
SidebarSectionLink.where(linkable_type: "Category", user_id: user.id).pluck(:linkable_id),
).to contain_exactly(category.id)
expect(
SidebarSectionLink.where(linkable_type: "Tag", user_id: user.id).pluck(:linkable_id),
).to contain_exactly(tag.id)
admin = Fabricate(:admin)
expect(
SidebarSectionLink.where(linkable_type: "Category", user_id: admin.id).pluck(
:linkable_id,
),
).to contain_exactly(category.id, secured_category.id)
expect(
SidebarSectionLink.where(linkable_type: "Tag", user_id: admin.id).pluck(:linkable_id),
SidebarSectionLink.where(linkable_type: "Tag", user_id: user.id).pluck(:linkable_id),
).to contain_exactly(tag.id, hidden_tag.id)
end
it "should create and remove the right sidebar section link records when user is promoted/demoted as an admin" do
user = Fabricate(:user)
another_category = Fabricate(:category)
another_tag = Fabricate(:tag)
# User has customized their sidebar categories and tags
SidebarSectionLink.where(user: user).delete_all
SidebarSectionLinksUpdater.update_category_section_links(
user,
category_ids: [another_category.id],
)
SidebarSectionLinksUpdater.update_tag_section_links(user, tag_names: [another_tag.name])
# A user promoted to admin now has any default categories/tags they didn't previously have access to
user.update(admin: true)
expect(
SidebarSectionLink.where(linkable_type: "Category", user_id: user.id).pluck(:linkable_id),
).to contain_exactly(another_category.id, secured_category.id)
expect(
SidebarSectionLink.where(linkable_type: "Tag", user_id: user.id).pluck(:linkable_id),
).to contain_exactly(another_tag.id, hidden_tag.id)
# User still has their customized sidebar categories and tags after demotion
user.update(admin: false)
expect(
SidebarSectionLink.where(linkable_type: "Category", user_id: user.id).pluck(:linkable_id),
).to contain_exactly(another_category.id)
expect(
SidebarSectionLink.where(linkable_type: "Tag", user_id: user.id).pluck(:linkable_id),
).to contain_exactly(another_tag.id)
end
it "should not receive any new categories w/ suppress secured categories from admin enabled" do
SiteSetting.suppress_secured_categories_from_admin = true
user = Fabricate(:user)
SidebarSectionLink.where(user: user).delete_all # User has customized their sidebar categories
user.update(admin: true)
expect(
SidebarSectionLink.where(linkable_type: "Category", user_id: user.id).pluck(:linkable_id),
).to be_empty
user.update(admin: false)
expect(
SidebarSectionLink.where(linkable_type: "Category", user_id: user.id).pluck(:linkable_id),
).to be_empty
end
it "should not create any sidebar section link records when navigation_menu site setting is still legacy" do
SiteSetting.navigation_menu = "legacy"
@ -3372,6 +3314,29 @@ RSpec.describe User do
end
end
describe "#secured_sidebar_category_ids" do
fab!(:user) { Fabricate(:user) }
fab!(:category) { Fabricate(:category) }
fab!(:group) { Fabricate(:group) }
fab!(:secured_category) { Fabricate(:private_category, group: group) }
fab!(:category_sidebar_section_link) do
Fabricate(:category_sidebar_section_link, user: user, linkable: category)
end
fab!(:secured_category_sidebar_section_link) do
Fabricate(:category_sidebar_section_link, user: user, linkable: secured_category)
end
it "should only return the category ids of category sidebar section link records that the user is allowed to see" do
expect(user.secured_sidebar_category_ids).to contain_exactly(category.id)
user.update!(admin: true)
expect(user.secured_sidebar_category_ids).to contain_exactly(category.id, secured_category.id)
end
end
describe "#visible_sidebar_tags" do
fab!(:user) { Fabricate(:user) }
fab!(:tag) { Fabricate(:tag) }