A platform for community discussion. Free, open, simple.
FEATURE: Restrict profile visibility of low-trust users (#29981)
Osama Sayegh976aca68f66e
diff --git a/lib/guardian/user_guardian.rb b/lib/guardian/user_guardian.rbindex 755f1cfbbd9..9c25c04367c 100644--- a/lib/guardian/user_guardian.rb+++ b/lib/guardian/user_guardian.rb@@ -128,12 +128,21 @@ module UserGuardiandef can_see_profile?(user)return false if user.blank?- return true if !SiteSetting.allow_users_to_hide_profile?+ return true if is_me?(user) || is_staff?- # If a user has hidden their profile, restrict it to them and staff- return is_me?(user) || is_staff? if user.user_option.try(:hide_profile?)+ profile_hidden = SiteSetting.allow_users_to_hide_profile && user.user_option&.hide_profile?- true+ return true if user.staff? && !profile_hidden++ if user.user_stat.blank? || user.user_stat.post_count == 0+ return false if anonymous? || [email protected]_trust_level?(TrustLevel[2])+ end++ if anonymous? || [email protected]_trust_level?(TrustLevel[1])+ return user.has_trust_level?(TrustLevel[1]) && !profile_hidden+ end++ !profile_hiddenenddef can_see_user_actions?(user, action_types)diff --git a/plugins/chat/spec/system/user_profile_spec.rb b/plugins/chat/spec/system/user_profile_spec.rbindex d3d5083e946..0ae52c36af7 100644--- a/plugins/chat/spec/system/user_profile_spec.rb+++ b/plugins/chat/spec/system/user_profile_spec.rb@@ -4,7 +4,10 @@ RSpec.describe "User profile", type: :system dofab!(:current_user) { Fabricate(:user) }fab!(:user)- before { chat_system_bootstrap }+ before do+ user.user_stat.update!(post_count: 1)+ chat_system_bootstrap+ endshared_examples "not showing chat button" doit "has no chat button" dodiff --git a/spec/lib/guardian/user_guardian_spec.rb b/spec/lib/guardian/user_guardian_spec.rbindex cdb3e0550bb..f5eb487b17d 100644--- a/spec/lib/guardian/user_guardian_spec.rb+++ b/spec/lib/guardian/user_guardian_spec.rb@@ -98,45 +98,176 @@ RSpec.describe UserGuardian doenddescribe "#can_see_profile?" do- it "is false for no user" do- expect(Guardian.new.can_see_profile?(nil)).to eq(false)- end+ fab!(:tl0_user) { Fabricate(:user, trust_level: 0) }+ fab!(:tl1_user) { Fabricate(:user, trust_level: 1) }+ fab!(:tl2_user) { Fabricate(:user, trust_level: 2) }++ before { tl2_user.user_stat.update!(post_count: 1) }++ context "when viewing the profile of a user with 0 posts" do+ before { user.user_stat.update!(post_count: 0) }++ it "they can view their own profile" do+ expect(Guardian.new(user).can_see_profile?(user)).to eq(true)+ end++ it "an anonymous user cannot view the user's profile" do+ expect(Guardian.new.can_see_profile?(user)).to eq(false)+ end++ it "a TL0 user cannot view the user's profile" do+ expect(Guardian.new(tl0_user).can_see_profile?(user)).to eq(false)+ end++ it "a TL1 user cannot view the user's profile" do+ expect(Guardian.new(tl1_user).can_see_profile?(user)).to eq(false)+ end++ it "a TL2 user can view the user's profile" do+ expect(Guardian.new(tl2_user).can_see_profile?(user)).to eq(true)+ end++ it "a moderator can view the user's profile" do+ expect(Guardian.new(moderator).can_see_profile?(user)).to eq(true)+ end- it "is true for a user whose profile is public" do- expect(Guardian.new.can_see_profile?(user)).to eq(true)+ it "an admin can view the user's profile" do+ expect(Guardian.new(admin).can_see_profile?(user)).to eq(true)+ end++ context "when the profile is hidden" do+ before do+ SiteSetting.allow_users_to_hide_profile = true+ user.user_option.update!(hide_profile: true)+ end++ it "they can view their own profile" do+ expect(Guardian.new(user).can_see_profile?(user)).to eq(true)+ end++ it "a TL2 user cannot view the user's profile" do+ expect(Guardian.new(tl2_user).can_see_profile?(user)).to eq(false)+ end++ it "a moderator can view the user's profile" do+ expect(Guardian.new(moderator).can_see_profile?(user)).to eq(true)+ end++ it "an admin can view the user's profile" do+ expect(Guardian.new(admin).can_see_profile?(user)).to eq(true)+ end+ endend- context "with hidden profile" do- # Mixing Fabricate.build() and Fabricate() could cause ID clashes, so override :user- fab!(:user)+ context "when viewing the profile of a TL0 user with more than 0 posts" do+ before { tl0_user.user_stat.update!(post_count: 1) }++ it "they can view their own profile" do+ expect(Guardian.new(tl0_user).can_see_profile?(tl0_user)).to eq(true)+ end++ it "an anonymous user cannot view the user's profile" do+ expect(Guardian.new.can_see_profile?(tl0_user)).to eq(false)+ end++ it "a TL0 user cannot view the user's profile" do+ expect(Guardian.new(Fabricate(:user, trust_level: 0)).can_see_profile?(tl0_user)).to eq(+ false,+ )+ end- let(:hidden_user) do- result = Fabricate(:user)- result.user_option.update_column(:hide_profile, true)- result+ it "a TL1 user can view the user's profile" do+ expect(Guardian.new(tl1_user).can_see_profile?(tl0_user)).to eq(true)end- it "is false for another user" do- expect(Guardian.new(user).can_see_profile?(hidden_user)).to eq(false)+ it "a TL2 user can view the user's profile" do+ expect(Guardian.new(tl2_user).can_see_profile?(tl0_user)).to eq(true)end- it "is false for an anonymous user" do- expect(Guardian.new.can_see_profile?(hidden_user)).to eq(false)+ it "a moderator user can view the user's profile" do+ expect(Guardian.new(moderator).can_see_profile?(tl0_user)).to eq(true)end- it "is true for the user themselves" do- expect(Guardian.new(hidden_user).can_see_profile?(hidden_user)).to eq(true)+ it "an admin user can view the user's profile" do+ expect(Guardian.new(admin).can_see_profile?(tl0_user)).to eq(true)end- it "is true for a staff user" do- expect(Guardian.new(admin).can_see_profile?(hidden_user)).to eq(true)+ context "when the profile is hidden" do+ before do+ SiteSetting.allow_users_to_hide_profile = true+ tl0_user.user_option.update!(hide_profile: true)+ end++ it "they can view their own profile" do+ expect(Guardian.new(tl0_user).can_see_profile?(tl0_user)).to eq(true)+ end++ it "a TL1 user cannot view the user's profile" do+ expect(Guardian.new(tl1_user).can_see_profile?(tl0_user)).to eq(false)+ end++ it "a TL2 user cannot view the user's profile" do+ expect(Guardian.new(tl2_user).can_see_profile?(tl0_user)).to eq(false)+ end++ it "a moderator user can view the user's profile" do+ expect(Guardian.new(moderator).can_see_profile?(tl0_user)).to eq(true)+ end++ it "an admin user can view the user's profile" do+ expect(Guardian.new(admin).can_see_profile?(tl0_user)).to eq(true)+ endend+ end++ context "when the allow_users_to_hide_profile setting is false" do+ before { SiteSetting.allow_users_to_hide_profile = false }- it "is true if hiding profiles is disabled" do- SiteSetting.allow_users_to_hide_profile = false- expect(Guardian.new(user).can_see_profile?(hidden_user)).to eq(true)+ it "doesn't hide the profile even if the hide_profile user option is true" do+ tl2_user.user_option.update!(hide_profile: true)++ expect(Guardian.new(tl0_user).can_see_profile?(tl2_user)).to eq(true)+ expect(Guardian.new(tl1_user).can_see_profile?(tl2_user)).to eq(true)+ expect(Guardian.new(admin).can_see_profile?(tl2_user)).to eq(true)+ expect(Guardian.new(moderator).can_see_profile?(tl2_user)).to eq(true)endend++ context "when the allow_users_to_hide_profile setting is true" do+ before { SiteSetting.allow_users_to_hide_profile = true }++ it "doesn't allow non-staff users to view the user's profile if the hide_profile user option is true" do+ tl2_user.user_option.update!(hide_profile: true)++ expect(Guardian.new(tl0_user).can_see_profile?(tl2_user)).to eq(false)+ expect(Guardian.new(tl1_user).can_see_profile?(tl2_user)).to eq(false)++ expect(Guardian.new(admin).can_see_profile?(tl2_user)).to eq(true)+ expect(Guardian.new(moderator).can_see_profile?(tl2_user)).to eq(true)+ end++ it "allows everyone to view the user's profile if the hide_profile user option is false" do+ tl2_user.user_option.update!(hide_profile: false)++ expect(Guardian.new(tl0_user).can_see_profile?(tl2_user)).to eq(true)+ expect(Guardian.new(tl1_user).can_see_profile?(tl2_user)).to eq(true)++ expect(Guardian.new(admin).can_see_profile?(tl2_user)).to eq(true)+ expect(Guardian.new(moderator).can_see_profile?(tl2_user)).to eq(true)+ end+ end++ it "is false for no user" do+ expect(Guardian.new.can_see_profile?(nil)).to eq(false)+ end++ it "is true for staff users even when they have no posts" do+ admin.user_stat.update!(post_count: 0)+ moderator.user_stat.update!(post_count: 0)++ expect(Guardian.new.can_see_profile?(admin)).to eq(true)+ expect(Guardian.new.can_see_profile?(moderator)).to eq(true)+ endenddescribe "#can_see_user_actions?" dodiff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rbindex 3aa6b61c30e..93c0b69c3a6 100644--- a/spec/requests/list_controller_spec.rb+++ b/spec/requests/list_controller_spec.rb@@ -905,7 +905,10 @@ RSpec.describe ListController dofab!(:topic2) { Fabricate(:topic, user: user) }fab!(:user2) { Fabricate(:user) }- before { sign_in(user2) }+ before do+ user.user_stat.update!(post_count: 1)+ sign_in(user2)+ endit "should respond with a list" doget "/topics/created-by/#{user.username}.json"diff --git a/spec/requests/posts_controller_spec.rb b/spec/requests/posts_controller_spec.rbindex b885e3920f6..439b3a1a39c 100644--- a/spec/requests/posts_controller_spec.rb+++ b/spec/requests/posts_controller_spec.rb@@ -2578,6 +2578,8 @@ RSpec.describe PostsController doenddescribe "#user_posts_feed" do+ before { user.user_stat.update!(post_count: 1) }+it "returns public posts rss feed" dopublic_postprivate_postdiff --git a/spec/requests/user_actions_controller_spec.rb b/spec/requests/user_actions_controller_spec.rbindex 6723995f8e7..abb0f5cec77 100644--- a/spec/requests/user_actions_controller_spec.rb+++ b/spec/requests/user_actions_controller_spec.rb@@ -19,7 +19,10 @@ RSpec.describe UserActionsController dolet(:actions) { response.parsed_body["user_actions"] }let(:post) { create_post }- before { UserActionManager.enable }+ before do+ UserActionManager.enable+ post.user.user_stat.update!(post_count: 1)+ endit "renders list correctly" douser_actionsdiff --git a/spec/requests/user_badges_controller_spec.rb b/spec/requests/user_badges_controller_spec.rbindex a4224239234..8f5a9dea442 100644--- a/spec/requests/user_badges_controller_spec.rb+++ b/spec/requests/user_badges_controller_spec.rb@@ -5,6 +5,8 @@ RSpec.describe UserBadgesController dofab!(:admin)fab!(:badge)+ before { user.user_stat.update!(post_count: 1) }+describe "#index" dofab!(:badge) { Fabricate(:badge, target_posts: true, show_posts: false) }diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rbindex 9a6350198ae..8f2aac569b4 100644--- a/spec/requests/users_controller_spec.rb+++ b/spec/requests/users_controller_spec.rb@@ -4252,6 +4252,12 @@ RSpec.describe UsersController doenddescribe "#summary" do+ before do+ user.user_stat.update!(post_count: 1)+ user1.user_stat.update!(post_count: 1)+ user_deferred.user_stat.update!(post_count: 1)+ end+it "generates summary info" docreate_post(user: user)@@ -4261,7 +4267,7 @@ RSpec.describe UsersController dojson = response.parsed_bodyexpect(json["user_summary"]["topic_count"]).to eq(1)- expect(json["user_summary"]["post_count"]).to eq(0)+ expect(json["user_summary"]["post_count"]).to eq(1)endcontext "when `hide_user_profiles_from_public` site setting is enabled" do@@ -4938,6 +4944,8 @@ RSpec.describe UsersController dofab!(:user) { Discourse.system_user }fab!(:user2) { Fabricate(:user) }+ before { user2.user_stat.update!(post_count: 1) }+it "returns success" doget "/user-cards.json?user_ids=#{user.id},#{user2.id}"expect(response.status).to eq(200)diff --git a/spec/serializers/user_serializer_spec.rb b/spec/serializers/user_serializer_spec.rbindex 9260e9396e1..e96d1ade673 100644--- a/spec/serializers/user_serializer_spec.rb+++ b/spec/serializers/user_serializer_spec.rb@@ -3,6 +3,8 @@RSpec.describe UserSerializer dofab!(:user) { Fabricate(:user, trust_level: 0) }+ before { user.user_stat.update!(post_count: 1) }+context "with a TL0 user seen as anonymous" dolet(:serializer) { UserSerializer.new(user, scope: Guardian.new, root: false) }let(:json) { serializer.as_json }