2014-02-25 12:15:08 -05:00
|
|
|
class ProjectsFinder
|
2015-11-18 06:21:06 -05:00
|
|
|
# Returns all projects, optionally including group projects a user has access
|
|
|
|
# to.
|
|
|
|
#
|
|
|
|
# ## Examples
|
|
|
|
#
|
|
|
|
# Retrieving all public projects:
|
|
|
|
#
|
|
|
|
# ProjectsFinder.new.execute
|
|
|
|
#
|
|
|
|
# Retrieving all public/internal projects and those the given user has access
|
|
|
|
# to:
|
|
|
|
#
|
|
|
|
# ProjectsFinder.new.execute(some_user)
|
|
|
|
#
|
|
|
|
# Retrieving all public/internal projects as well as the group's projects the
|
|
|
|
# user has access to:
|
|
|
|
#
|
|
|
|
# ProjectsFinder.new.execute(some_user, group: some_group)
|
|
|
|
#
|
|
|
|
# Returns an ActiveRecord::Relation.
|
|
|
|
def execute(current_user = nil, options = {})
|
2014-02-25 12:15:08 -05:00
|
|
|
group = options[:group]
|
|
|
|
|
|
|
|
if group
|
2015-11-20 09:48:05 -05:00
|
|
|
segments = group_projects(current_user, group)
|
2014-02-25 12:15:08 -05:00
|
|
|
else
|
2015-11-20 09:48:05 -05:00
|
|
|
segments = all_projects(current_user)
|
2015-11-18 06:21:06 -05:00
|
|
|
end
|
|
|
|
|
2015-11-20 09:48:05 -05:00
|
|
|
if segments.length > 1
|
|
|
|
union = Gitlab::SQL::Union.new(segments.map { |s| s.select(:id) })
|
2015-11-18 06:21:06 -05:00
|
|
|
|
|
|
|
Project.where("projects.id IN (#{union.to_sql})")
|
|
|
|
else
|
2015-11-20 09:48:05 -05:00
|
|
|
segments.first
|
2014-02-25 12:15:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def group_projects(current_user, group)
|
|
|
|
if current_user
|
2015-11-18 06:21:06 -05:00
|
|
|
[
|
|
|
|
group_projects_for_user(current_user, group),
|
|
|
|
group.projects.public_and_internal_only
|
|
|
|
]
|
2014-02-25 12:15:08 -05:00
|
|
|
else
|
2015-11-18 06:21:06 -05:00
|
|
|
[group.projects.public_only]
|
2014-02-25 12:15:08 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-06-05 13:37:35 -04:00
|
|
|
def all_projects(current_user)
|
|
|
|
if current_user
|
2015-11-18 06:21:06 -05:00
|
|
|
[current_user.authorized_projects, public_and_internal_projects]
|
2014-06-05 13:37:35 -04:00
|
|
|
else
|
2015-11-18 06:21:06 -05:00
|
|
|
[Project.public_only]
|
2014-06-05 13:37:35 -04:00
|
|
|
end
|
2014-02-25 12:15:08 -05:00
|
|
|
end
|
2015-11-18 06:21:06 -05:00
|
|
|
|
|
|
|
def group_projects_for_user(current_user, group)
|
|
|
|
if group.users.include?(current_user)
|
|
|
|
group.projects
|
|
|
|
else
|
|
|
|
group.projects.visible_to_user(current_user)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_projects
|
|
|
|
Project.unscoped.public_only
|
|
|
|
end
|
|
|
|
|
|
|
|
def public_and_internal_projects
|
|
|
|
Project.unscoped.public_and_internal_only
|
|
|
|
end
|
2014-02-25 12:15:08 -05:00
|
|
|
end
|