FIX: Wrong scope used for notification levels user serializer (#13039)

This is a recent regression introduced by https://github.com/discourse/discourse/pull/12937 which makes it so that when looking at a user profile that is not your own, specifically the category and tag notification settings, you would see your own settings instead of the target user. This is only a problem for admins because regular users cannot see these details for other users.

The issue was that we were using `scope` in the serializer, which refers to the current user, rather than using a scope for the target user via `Guardian.new(user)`.

However, on further inspection the `notification_levels_for` method for `TagUser` and `CategoryUser` did not actually need to be accepting an instance of Guardian, all that it was using it for was to check guardian.anonymous? which is just a fancy way of saying user.blank?. Changed this method to just accept a user instead and send the user in from the serializer.
This commit is contained in:
Martin Brennan
2021-05-14 09:45:14 +10:00
committed by GitHub
parent 19182b1386
commit 38742bc208
11 changed files with 73 additions and 26 deletions

View File

@ -222,7 +222,6 @@ describe CategoryUser do
end
describe "#notification_levels_for" do
let(:guardian) { Guardian.new(user) }
let!(:category1) { Fabricate(:category) }
let!(:category2) { Fabricate(:category) }
let!(:category3) { Fabricate(:category) }
@ -239,7 +238,7 @@ describe CategoryUser do
SiteSetting.default_categories_muted = category5.id.to_s
end
it "every category from the default_categories_* site settings get overridden to regular, except for muted" do
levels = CategoryUser.notification_levels_for(guardian)
levels = CategoryUser.notification_levels_for(user)
expect(levels[category1.id]).to eq(CategoryUser.notification_levels[:regular])
expect(levels[category2.id]).to eq(CategoryUser.notification_levels[:regular])
expect(levels[category3.id]).to eq(CategoryUser.notification_levels[:regular])
@ -259,7 +258,7 @@ describe CategoryUser do
it "gets the category_user notification levels for all categories the user is tracking and does not
include categories the user is not tracking at all" do
category6 = Fabricate(:category)
levels = CategoryUser.notification_levels_for(guardian)
levels = CategoryUser.notification_levels_for(user)
expect(levels[category1.id]).to eq(CategoryUser.notification_levels[:watching])
expect(levels[category2.id]).to eq(CategoryUser.notification_levels[:tracking])
expect(levels[category3.id]).to eq(CategoryUser.notification_levels[:watching_first_post])

View File

@ -57,7 +57,7 @@ describe GroupUser do
group.watching_first_post_category_ids = [category5.id]
group.save!
expect { group.add(user) }.to change { CategoryUser.count }.by(5)
h = CategoryUser.notification_levels_for(Guardian.new(user))
h = CategoryUser.notification_levels_for(user)
expect(h[category1.id]).to eq(levels[:muted])
expect(h[category2.id]).to eq(levels[:regular])
expect(h[category3.id]).to eq(levels[:tracking])
@ -74,7 +74,7 @@ describe GroupUser do
group.watching_first_post_category_ids = [category2.id, category3.id, category4.id]
group.save!
group.add(user)
h = CategoryUser.notification_levels_for(Guardian.new(user))
h = CategoryUser.notification_levels_for(user)
expect(h[category1.id]).to eq(levels[:regular])
expect(h[category2.id]).to eq(levels[:watching_first_post])
expect(h[category3.id]).to eq(levels[:watching_first_post])
@ -89,7 +89,7 @@ describe GroupUser do
group.tracking_category_ids = [category4.id]
group.save!
group.add(user)
h = CategoryUser.notification_levels_for(Guardian.new(user))
h = CategoryUser.notification_levels_for(user)
expect(h[category1.id]).to eq(levels[:tracking])
expect(h[category2.id]).to eq(levels[:watching])
expect(h[category3.id]).to eq(levels[:muted])

View File

@ -234,7 +234,6 @@ describe TagUser do
end
describe "#notification_levels_for" do
let(:guardian) { Guardian.new(user) }
let!(:tag1) { Fabricate(:tag) }
let!(:tag2) { Fabricate(:tag) }
let!(:tag3) { Fabricate(:tag) }
@ -249,7 +248,7 @@ describe TagUser do
SiteSetting.default_tags_muted = tag4.name
end
it "every tag from the default_tags_* site settings get overridden to watching_first_post, except for muted" do
levels = TagUser.notification_levels_for(guardian)
levels = TagUser.notification_levels_for(user)
expect(levels[tag1.name]).to eq(TagUser.notification_levels[:regular])
expect(levels[tag2.name]).to eq(TagUser.notification_levels[:regular])
expect(levels[tag3.name]).to eq(TagUser.notification_levels[:regular])
@ -268,7 +267,7 @@ describe TagUser do
it "gets the tag_user notification levels for all tags the user is tracking and does not
include tags the user is not tracking at all" do
tag5 = Fabricate(:tag)
levels = TagUser.notification_levels_for(guardian)
levels = TagUser.notification_levels_for(user)
expect(levels[tag1.name]).to eq(TagUser.notification_levels[:watching])
expect(levels[tag2.name]).to eq(TagUser.notification_levels[:tracking])
expect(levels[tag3.name]).to eq(TagUser.notification_levels[:watching_first_post])