From b3904eab456f1574431761d18a5e27fc648b2c10 Mon Sep 17 00:00:00 2001 From: Martin Brennan Date: Mon, 22 Jan 2024 14:40:29 +1000 Subject: [PATCH] FIX: User group check should return true for system user for auto groups (#25357) This is a temporary fix to address an issue where the system user is losing its automatic groups when the server is running. If any auto groups are provided, and the user is a system user, then we return true. The system user is admin, moderator, and TL4, so they usually have all auto groups. We can remove this when we get to the bottom of why the auto groups are being deleted. --- app/models/user.rb | 4 +++- spec/models/user_spec.rb | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/models/user.rb b/app/models/user.rb index ccb048f8a33..6f38448089f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -516,7 +516,9 @@ class User < ActiveRecord::Base end def in_any_groups?(group_ids) - group_ids.include?(Group::AUTO_GROUPS[:everyone]) || (group_ids & belonging_to_group_ids).any? + group_ids.include?(Group::AUTO_GROUPS[:everyone]) || + (is_system_user? && (Group.auto_groups_between(:admins, :trust_level_4) & group_ids).any?) || + (group_ids & belonging_to_group_ids).any? end def belonging_to_group_ids diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e2288f10604..fd2c2b8667a 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -17,6 +17,29 @@ RSpec.describe User do ) end + describe ".in_any_groups?" do + fab!(:group) + + it "returns true if any of the group IDs are the 'everyone' auto group" do + expect(user.in_any_groups?([group.id, Group::AUTO_GROUPS[:everyone]])).to eq(true) + end + + it "returns true if the user is in the group" do + expect(user.in_any_groups?([group.id])).to eq(false) + group.add(user) + user.reload + expect(user.in_any_groups?([group.id])).to eq(true) + end + + it "always returns true for system user for automated groups" do + GroupUser.where(user_id: Discourse::SYSTEM_USER_ID).delete_all + Discourse.system_user.reload + expect(Discourse.system_user.in_any_groups?([group.id])).to eq(false) + expect(Discourse.system_user.in_any_groups?([Group::AUTO_GROUPS[:trust_level_4]])).to eq(true) + expect(Discourse.system_user.in_any_groups?([Group::AUTO_GROUPS[:admins]])).to eq(true) + end + end + describe "Associations" do it "should delete sidebar_section_links when a user is destroyed" do Fabricate(:category_sidebar_section_link, user: user)