DEV: Introduce TopicGuardian#can_see_topic_ids method (#18692)

Before this commit, there was no way for us to efficiently check an
array of topics for which a user can see. Therefore, this commit
introduces the `TopicGuardian#can_see_topic_ids` method which accepts an
array of `Topic#id`s and filters out the ids which the user is not
allowed to see. The `TopicGuardian#can_see_topic_ids` method is meant to
maintain feature parity with `TopicGuardian#can_see_topic?` at all
times so a consistency check has been added in our tests to ensure that
`TopicGuardian#can_see_topic_ids` returns the same result as
`TopicGuardian#can_see_topic?`. In the near future, the plan is for us
to switch to `TopicGuardian#can_see_topic_ids` completely but I'm not
doing that in this commit as we have to be careful with the performance
impact of such a change.

This method is currently not being used in the current commit but will
be relied on in a subsequent commit.
This commit is contained in:
Alan Guo Xiang Tan
2022-10-27 06:13:21 +08:00
committed by GitHub
parent d4583357cb
commit a473e352de
7 changed files with 222 additions and 18 deletions

View File

@ -116,21 +116,18 @@ class Guardian
end
def is_category_group_moderator?(category)
return false if !SiteSetting.enable_category_group_moderation?
return false if !category
return false if !authenticated?
return false if !category_group_moderation_allowed?
reviewable_by_group_id = category.reviewable_by_group_id
return false if reviewable_by_group_id.blank?
@is_group_member ||= {}
@category_group_moderator_groups ||= {}
if @is_group_member.key?(reviewable_by_group_id)
@is_group_member[reviewable_by_group_id]
if @category_group_moderator_groups.key?(reviewable_by_group_id)
@category_group_moderator_groups[reviewable_by_group_id]
else
@is_group_member[reviewable_by_group_id] = begin
GroupUser.where(group_id: reviewable_by_group_id, user_id: @user.id).exists?
end
@category_group_moderator_groups[reviewable_by_group_id] = category_group_moderator_scope.exists?("categories.id": category.id)
end
end
@ -622,4 +619,16 @@ class Guardian
end
end
protected
def category_group_moderation_allowed?
authenticated? && SiteSetting.enable_category_group_moderation
end
def category_group_moderator_scope
Category
.joins("INNER JOIN group_users ON group_users.group_id = categories.reviewable_by_group_id")
.where("group_users.user_id = ?", user.id)
end
end