discourse/app/serializers/basic_user_serializer.rb
Martin Brennan 38742bc208
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.
2021-05-14 09:45:14 +10:00

50 lines
1.2 KiB
Ruby

# frozen_string_literal: true
class BasicUserSerializer < ApplicationSerializer
attributes :id, :username, :name, :avatar_template
def name
Hash === user ? user[:name] : user.try(:name)
end
def include_name?
SiteSetting.enable_names?
end
def avatar_template
if Hash === object
User.avatar_template(user[:username], user[:uploaded_avatar_id])
else
user&.avatar_template
end
end
def user
object[:user] || object.try(:user) || object
end
def user_is_current_user
object.id == scope.user&.id
end
def categories_with_notification_level(lookup_level)
category_user_notification_levels.select do |id, level|
level == CategoryUser.notification_levels[lookup_level]
end.keys
end
def category_user_notification_levels
@category_user_notification_levels ||= CategoryUser.notification_levels_for(user)
end
def tags_with_notification_level(lookup_level)
tag_user_notification_levels.select do |id, level|
level == TagUser.notification_levels[lookup_level]
end.keys
end
def tag_user_notification_levels
@tag_user_notification_levels ||= TagUser.notification_levels_for(user)
end
end