gitlab-org--gitlab-foss/app/finders/labels_finder.rb

194 lines
4.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-09-20 00:08:04 -04:00
class LabelsFinder < UnionFinder
prepend FinderWithCrossProjectAccess
include FinderMethods
2017-12-06 14:07:47 -05:00
include Gitlab::Utils::StrongMemoize
requires_cross_project_access unless: -> { project? }
2016-09-19 16:16:16 -04:00
def initialize(current_user, params = {})
@current_user = current_user
@params = params
end
# rubocop: disable CodeReuse/ActiveRecord
def execute(skip_authorization: false)
@skip_authorization = skip_authorization
items = find_union(label_ids, Label) || Label.none
2016-09-19 16:16:16 -04:00
items = with_title(items)
items = by_subscription(items)
items = by_search(items)
2016-09-19 16:16:16 -04:00
sort(items)
end
# rubocop: enable CodeReuse/ActiveRecord
2016-09-19 16:16:16 -04:00
private
attr_reader :current_user, :params, :skip_authorization
2016-09-19 16:16:16 -04:00
# rubocop: disable CodeReuse/ActiveRecord
2016-09-20 00:08:04 -04:00
def label_ids
2016-09-19 16:16:16 -04:00
label_ids = []
if project?
if project
if project.group.present?
labels_table = Label.arel_table
group_ids = group_ids_for(project.group)
label_ids << Label.where(
labels_table[:type].eq('GroupLabel').and(labels_table[:group_id].in(group_ids)).or(
labels_table[:type].eq('ProjectLabel').and(labels_table[:project_id].eq(project.id))
)
)
else
label_ids << project.labels
end
end
else
if group?
group = Group.find(params[:group_id])
label_ids << Label.where(group_id: group_ids_for(group))
end
label_ids << Label.where(group_id: projects.group_ids)
label_ids << Label.where(project_id: projects.select(:id)) unless only_group_labels?
end
label_ids
2016-09-19 16:16:16 -04:00
end
# rubocop: enable CodeReuse/ActiveRecord
2016-09-19 16:16:16 -04:00
# rubocop: disable CodeReuse/ActiveRecord
def sort(items)
if params[:sort]
items.order_by(params[:sort])
else
items.reorder(title: :asc)
end
end
# rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
2016-09-19 16:16:16 -04:00
def with_title(items)
return items if title.nil?
return items.none if title.blank?
items.where(title: title)
2016-09-19 16:16:16 -04:00
end
# rubocop: enable CodeReuse/ActiveRecord
2016-09-19 16:16:16 -04:00
def by_search(labels)
return labels unless search?
labels.search(params[:search])
end
def by_subscription(labels)
labels.optionally_subscribed_by(subscriber_id)
end
def subscriber_id
current_user&.id if subscribed?
end
def subscribed?
params[:subscribed] == 'true'
end
# Gets redacted array of group ids
# which can include the ancestors and descendants of the requested group.
def group_ids_for(group)
strong_memoize(:group_ids) do
groups = groups_to_include(group)
groups_user_can_read_labels(groups).map(&:id)
2017-12-06 14:07:47 -05:00
end
end
def groups_to_include(group)
groups = [group]
groups += group.ancestors if include_ancestor_groups?
groups += group.descendants if include_descendant_groups?
groups
end
def include_ancestor_groups?
params[:include_ancestor_groups]
end
def include_descendant_groups?
params[:include_descendant_groups]
end
def group?
params[:group_id].present?
2016-09-19 16:16:16 -04:00
end
def project?
params[:project].present? || params[:project_id].present?
2016-09-19 16:16:16 -04:00
end
def projects?
params[:project_ids]
end
2017-12-06 14:07:47 -05:00
def only_group_labels?
params[:only_group_labels]
end
def search?
params[:search].present?
end
2016-09-19 16:16:16 -04:00
def title
params[:title] || params[:name]
2016-09-19 16:16:16 -04:00
end
def project
return @project if defined?(@project)
if project?
@project = params[:project] || Project.find(params[:project_id])
@project = nil unless authorized_to_read_labels?(@project)
else
@project = nil
end
@project
end
# rubocop: disable CodeReuse/ActiveRecord
2016-09-19 16:16:16 -04:00
def projects
return @projects if defined?(@projects)
@projects = if skip_authorization
Project.all
else
ProjectsFinder.new(params: { non_archived: true }, current_user: current_user).execute # rubocop: disable CodeReuse/Finder
end
@projects = @projects.in_namespace(params[:group_id]) if group?
@projects = @projects.where(id: params[:project_ids]) if projects?
@projects = @projects.reorder(nil)
2016-09-19 16:16:16 -04:00
@projects
end
# rubocop: enable CodeReuse/ActiveRecord
2017-12-06 14:07:47 -05:00
def authorized_to_read_labels?(label_parent)
return true if skip_authorization
2017-12-06 14:07:47 -05:00
Ability.allowed?(current_user, :read_label, label_parent)
end
def groups_user_can_read_labels(groups)
DeclarativePolicy.user_scope do
groups.select { |group| authorized_to_read_labels?(group) }
end
end
2016-09-19 16:16:16 -04:00
end