debian-mirror-gitlab/app/finders/labels_finder.rb

201 lines
4.7 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2016-11-03 12:29:30 +05:30
class LabelsFinder < UnionFinder
2018-03-27 19:54:05 +05:30
prepend FinderWithCrossProjectAccess
include FinderMethods
2018-03-17 18:26:18 +05:30
include Gitlab::Utils::StrongMemoize
2018-03-27 19:54:05 +05:30
requires_cross_project_access unless: -> { project? }
2016-11-03 12:29:30 +05:30
def initialize(current_user, params = {})
@current_user = current_user
@params = params
end
def execute(skip_authorization: false)
@skip_authorization = skip_authorization
2016-11-24 13:41:30 +05:30
items = find_union(label_ids, Label) || Label.none
2016-11-03 12:29:30 +05:30
items = with_title(items)
2018-12-05 23:21:45 +05:30
items = by_subscription(items)
2018-11-18 11:00:15 +05:30
items = by_search(items)
2016-11-03 12:29:30 +05:30
sort(items)
end
private
attr_reader :current_user, :params, :skip_authorization
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-11-03 12:29:30 +05:30
def label_ids
label_ids = []
2016-11-24 13:41:30 +05:30
if project?
if project
2017-08-17 22:00:37 +05:30
if project.group.present?
labels_table = Label.arel_table
2018-05-09 12:01:36 +05:30
group_ids = group_ids_for(project.group)
2017-08-17 22:00:37 +05:30
label_ids << Label.where(
2018-05-09 12:01:36 +05:30
labels_table[:type].eq('GroupLabel').and(labels_table[:group_id].in(group_ids)).or(
2017-08-17 22:00:37 +05:30
labels_table[:type].eq('ProjectLabel').and(labels_table[:project_id].eq(project.id))
)
)
else
label_ids << project.labels
end
2016-11-24 13:41:30 +05:30
end
2016-11-03 12:29:30 +05:30
else
2018-05-09 12:01:36 +05:30
if group?
label_ids << Label.where(group_id: group_ids_for(group))
end
2016-11-03 12:29:30 +05:30
label_ids << Label.where(group_id: projects.group_ids)
2019-10-31 01:37:42 +05:30
label_ids << Label.where(project_id: ids_user_can_read_labels(projects)) unless only_group_labels?
2016-11-03 12:29:30 +05:30
end
label_ids
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-11-03 12:29:30 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-11-03 12:29:30 +05:30
def sort(items)
2018-12-05 23:21:45 +05:30
if params[:sort]
items.order_by(params[:sort])
else
items.reorder(title: :asc)
end
2016-11-03 12:29:30 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-11-03 12:29:30 +05:30
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-11-03 12:29:30 +05:30
def with_title(items)
return items if title.nil?
return items.none if title.blank?
items.where(title: title)
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-11-03 12:29:30 +05:30
2018-11-18 11:00:15 +05:30
def by_search(labels)
return labels unless search?
labels.search(params[:search])
end
2018-12-05 23:21:45 +05:30
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
2018-05-09 12:01:36 +05:30
# Gets redacted array of group ids
# which can include the ancestors and descendants of the requested group.
def group_ids_for(group)
2018-03-27 19:54:05 +05:30
strong_memoize(:group_ids) do
2018-05-09 12:01:36 +05:30
groups = groups_to_include(group)
groups_user_can_read_labels(groups).map(&:id)
2018-03-17 18:26:18 +05:30
end
end
2018-05-09 12:01:36 +05:30
def groups_to_include(group)
2018-03-27 19:54:05 +05:30
groups = [group]
2018-05-09 12:01:36 +05:30
groups += group.ancestors if include_ancestor_groups?
groups += group.descendants if include_descendant_groups?
2018-03-27 19:54:05 +05:30
groups
end
2018-05-09 12:01:36 +05:30
def include_ancestor_groups?
params[:include_ancestor_groups]
end
def include_descendant_groups?
params[:include_descendant_groups]
end
2016-11-24 13:41:30 +05:30
def group?
2020-06-23 00:09:42 +05:30
params[:group].present? || params[:group_id].present?
end
def group
strong_memoize(:group) { params[:group].presence || Group.find(params[:group_id]) }
2016-11-03 12:29:30 +05:30
end
2016-11-24 13:41:30 +05:30
def project?
2018-12-05 23:21:45 +05:30
params[:project].present? || params[:project_id].present?
2016-11-03 12:29:30 +05:30
end
2016-11-24 13:41:30 +05:30
def projects?
2018-03-17 18:26:18 +05:30
params[:project_ids]
end
def only_group_labels?
params[:only_group_labels]
2016-11-03 12:29:30 +05:30
end
2018-11-18 11:00:15 +05:30
def search?
params[:search].present?
end
2016-11-03 12:29:30 +05:30
def title
params[:title] || params[:name]
end
def project
return @project if defined?(@project)
2016-11-24 13:41:30 +05:30
if project?
2018-12-05 23:21:45 +05:30
@project = params[:project] || Project.find(params[:project_id])
2016-11-24 13:41:30 +05:30
@project = nil unless authorized_to_read_labels?(@project)
2016-11-03 12:29:30 +05:30
else
@project = nil
end
@project
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2016-11-03 12:29:30 +05:30
def projects
return @projects if defined?(@projects)
2017-09-10 17:25:29 +05:30
@projects = if skip_authorization
Project.all
else
2018-12-05 23:21:45 +05:30
ProjectsFinder.new(params: { non_archived: true }, current_user: current_user).execute # rubocop: disable CodeReuse/Finder
2017-09-10 17:25:29 +05:30
end
2020-06-23 00:09:42 +05:30
@projects = @projects.in_namespace(group.id) if group?
2016-11-24 13:41:30 +05:30
@projects = @projects.where(id: params[:project_ids]) if projects?
2016-11-03 12:29:30 +05:30
@projects = @projects.reorder(nil)
@projects
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2016-11-03 12:29:30 +05:30
2018-03-17 18:26:18 +05:30
def authorized_to_read_labels?(label_parent)
2016-11-24 13:41:30 +05:30
return true if skip_authorization
2018-03-17 18:26:18 +05:30
Ability.allowed?(current_user, :read_label, label_parent)
2016-11-03 12:29:30 +05:30
end
2018-03-27 19:54:05 +05:30
def groups_user_can_read_labels(groups)
DeclarativePolicy.user_scope do
groups.select { |group| authorized_to_read_labels?(group) }
end
end
2019-10-31 01:37:42 +05:30
# rubocop: disable CodeReuse/ActiveRecord
def ids_user_can_read_labels(projects)
Project.where(id: projects.select(:id)).ids_with_issuables_available_for(current_user)
end
# rubocop: enable CodeReuse/ActiveRecord
2016-11-03 12:29:30 +05:30
end