Commit 5f243a63 authored by Thong Kuah's avatar Thong Kuah

Refactor to reduce branches

This also DRYs up the two calls to current_user.authorized_projects.

Clarify code comment
parent 5c93bdbe
...@@ -79,18 +79,30 @@ class ProjectsFinder < UnionFinder ...@@ -79,18 +79,30 @@ class ProjectsFinder < UnionFinder
elsif min_access_level? elsif min_access_level?
current_user.authorized_projects(params[:min_access_level]) current_user.authorized_projects(params[:min_access_level])
else else
if private_only? if private_only? || impossible_visibility_level?
current_user.authorized_projects current_user.authorized_projects
else else
default_visibility_levels = Gitlab::VisibilityLevel.levels_for_user(current_user) Project.public_or_visible_to_user(current_user)
end
end
end
# Builds a collection for an anonymous user.
def collection_without_user
if private_only? || owned_projects? || min_access_level?
Project.none
else
Project.public_to_user
end
end
# This is an optimization - surprisingly PostgreSQL does not optimize # This is an optimization - surprisingly PostgreSQL does not optimize
# for this. # for this.
# #
# If the default visiblity level and desired visiblity level filter # If the default visiblity level and desired visiblity level filter cancels
# cancels each other out, don't use the SQL clause for visibility level. # each other out, don't use the SQL clause for visibility level in
# In fact, this then becames equivalent to just authorized projects for # `Project.public_or_visible_to_user`. In fact, this then becames equivalent
# the user. # to just authorized projects for the user.
# #
# E.g. # E.g.
# (EXISTS(<authorized_projects>) OR projects.visibility_level IN (10,20)) # (EXISTS(<authorized_projects>) OR projects.visibility_level IN (10,20))
...@@ -100,22 +112,12 @@ class ProjectsFinder < UnionFinder ...@@ -100,22 +112,12 @@ class ProjectsFinder < UnionFinder
# EXISTS(<authorized_projects>) AND "projects"."visibility_level" = 0 # EXISTS(<authorized_projects>) AND "projects"."visibility_level" = 0
# #
# See https://gitlab.com/gitlab-org/gitlab/issues/37007 # See https://gitlab.com/gitlab-org/gitlab/issues/37007
if params[:visibility_level].present? && !default_visibility_levels.include?(params[:visibility_level]) def impossible_visibility_level?
current_user.authorized_projects return unless params[:visibility_level].present?
else
Project.public_or_visible_to_user(current_user)
end
end
end
end
# Builds a collection for an anonymous user. public_visibility_levels = Gitlab::VisibilityLevel.levels_for_user(current_user)
def collection_without_user
if private_only? || owned_projects? || min_access_level? !public_visibility_levels.include?(params[:visibility_level])
Project.none
else
Project.public_to_user
end
end end
def owned_projects? def owned_projects?
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment