A platform for community discussion. Free, open, simple.
FIX: respect profile-visibility controls on about page (#38527)
Natalie Tay5c591df9b844
diff --git a/app/models/about.rb b/app/models/about.rbindex a6ead14dd1f..e4049d5b3ac 100644--- a/app/models/about.rb+++ b/app/models/about.rb@@ -66,21 +66,33 @@ class Aboutdef moderators@moderators ||=- apply_excluded_groups(- User.where(moderator: true).human_users.order(last_seen_at: :desc),- ignore_groups: [Group::AUTO_GROUPS[:admins]],- )+ if guardian.public_can_see_profiles?+ apply_hidden_profile(+ apply_excluded_groups(+ User.where(moderator: true).human_users.order(last_seen_at: :desc),+ ignore_groups: [Group::AUTO_GROUPS[:admins]],+ ),+ )+ else+ []+ endenddef admins@admins ||=- DiscoursePluginRegistry.apply_modifier(- :about_admins,- apply_excluded_groups(- User.where(admin: true).human_users.order(last_seen_at: :desc),- ignore_groups: [Group::AUTO_GROUPS[:moderators]],- ),- )+ if guardian.public_can_see_profiles?+ DiscoursePluginRegistry.apply_modifier(+ :about_admins,+ apply_hidden_profile(+ apply_excluded_groups(+ User.where(admin: true).human_users.order(last_seen_at: :desc),+ ignore_groups: [Group::AUTO_GROUPS[:moderators]],+ ),+ ),+ )+ else+ []+ endenddef stats@@ -88,7 +100,9 @@ class Aboutenddef category_moderators- allowed_cats = Guardian.new(@user).allowed_category_ids+ return [] unless guardian.public_can_see_profiles?++ allowed_cats = guardian.allowed_category_idsreturn [] if allowed_cats.blank?cats_with_mods = Category.joins(:category_moderation_groups).distinct.pluck(:id)@@ -99,6 +113,8 @@ class Aboutper_cat_limit = category_mods_limit / category_ids.sizeper_cat_limit = 1 if per_cat_limit < 1+ filter_hidden_profiles = !guardian.is_staff? && SiteSetting.allow_users_to_hide_profile+results = DB.query(<<~SQL, category_ids:)WITH moderator_users AS (SELECT@@ -111,7 +127,9 @@ class AboutON cmg.group_id = gu.group_idINNER JOIN users uON gu.user_id = u.id+ #{"LEFT JOIN user_options uo ON uo.user_id = u.id" if filter_hidden_profiles}WHERE cmg.category_id IN (:category_ids)+ #{"AND uo.hide_profile IS NOT TRUE" if filter_hidden_profiles})SELECT id AS category_id, user_idsFROM categories@@ -143,13 +161,24 @@ class Aboutprivate+ def guardian+ @guardian ||= Guardian.new(@user)+ end++ def apply_hidden_profile(query)+ return query if guardian.is_staff?+ return query unless SiteSetting.allow_users_to_hide_profile++ query.left_joins(:user_option).where("user_options.hide_profile IS NOT TRUE")+ end+def apply_excluded_groups(query, ignore_groups: [])group_ids = SiteSetting.about_page_hidden_groups_map - ignore_groupsreturn query if group_ids.blank?query.joins(DB.sql_fragment(- "LEFT JOIN group_users ON group_id IN (:group_ids) AND user_id = users.id",+ "LEFT JOIN group_users ON group_users.group_id IN (:group_ids) AND group_users.user_id = users.id",group_ids:,),).where("group_users.id": nil)diff --git a/spec/requests/about_controller_spec.rb b/spec/requests/about_controller_spec.rbindex 73bda70105b..243903a0598 100644--- a/spec/requests/about_controller_spec.rb+++ b/spec/requests/about_controller_spec.rb@@ -63,5 +63,97 @@ RSpec.describe AboutController doexpect(response.status).to eq(200)expect(response.parsed_body["about"].keys).not_to include("stats")end++ context "with profile visibility controls" do+ fab!(:admin)+ fab!(:moderator)++ def user_ids_from(key)+ response.parsed_body["about"]["#{key}_ids"] || []+ end++ context "when hide_user_profiles_from_public is enabled" do+ before { SiteSetting.hide_user_profiles_from_public = true }++ it "does not expose admins and moderators to anonymous users" do+ get "/about.json"++ expect(response.status).to eq(200)+ expect(user_ids_from("admin")).to be_empty+ expect(user_ids_from("moderator")).to be_empty+ end++ it "exposes admins and moderators to logged in users" do+ sign_in(Fabricate(:user))+ get "/about.json"++ expect(response.status).to eq(200)+ expect(user_ids_from("admin")).to include(admin.id)+ expect(user_ids_from("moderator")).to include(moderator.id)+ end+ end++ context "when a staff member has hide_profile enabled" do+ before do+ SiteSetting.allow_users_to_hide_profile = true+ admin.user_option.update!(hide_profile: true)+ end++ it "excludes them from the about page for anonymous users" do+ get "/about.json"++ expect(response.status).to eq(200)+ expect(user_ids_from("admin")).not_to include(admin.id)+ end++ it "excludes them from the about page for regular users" do+ sign_in(Fabricate(:user))+ get "/about.json"++ expect(response.status).to eq(200)+ expect(user_ids_from("admin")).not_to include(admin.id)+ end++ it "still shows them to staff users" do+ sign_in(Fabricate(:admin))+ get "/about.json"++ expect(response.status).to eq(200)+ expect(user_ids_from("admin")).to include(admin.id)+ end+ end++ context "with category moderators" do+ fab!(:group, :public_group)+ fab!(:category_mod) { Fabricate(:user, last_seen_at: 1.day.ago) }+ fab!(:category)+ fab!(:category_moderation_group) do+ group.add(category_mod)+ Fabricate(:category_moderation_group, category: category, group: group)+ end++ it "does not expose category moderators to anonymous users when hide_user_profiles_from_public is enabled" do+ SiteSetting.hide_user_profiles_from_public = true+ get "/about.json"++ expect(response.status).to eq(200)+ expect(response.parsed_body["about"]["category_moderators"]).to be_empty+ end++ it "excludes category moderators with hide_profile enabled" do+ SiteSetting.allow_users_to_hide_profile = true+ category_mod.user_option.update!(hide_profile: true)++ get "/about.json"++ expect(response.status).to eq(200)+ all_mod_ids =+ response.parsed_body["about"]["category_moderators"].flat_map do |cm|+ cm["moderator_ids"]+ end+ expect(all_mod_ids).not_to include(category_mod.id)+ end+ end+ endendend