2018-09-11 19:08:34 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-20 04:08:04 +00:00
|
|
|
class LabelsFinder < UnionFinder
|
2017-12-11 14:21:06 +00:00
|
|
|
prepend FinderWithCrossProjectAccess
|
2021-04-08 18:09:32 +00:00
|
|
|
include FinderWithGroupHierarchy
|
2017-12-11 14:21:06 +00:00
|
|
|
include FinderMethods
|
2017-12-06 19:07:47 +00:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2017-12-11 14:21:06 +00:00
|
|
|
requires_cross_project_access unless: -> { project? }
|
|
|
|
|
2016-09-19 20:16:16 +00:00
|
|
|
def initialize(current_user, params = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
|
|
|
end
|
|
|
|
|
2016-10-28 09:31:45 +00:00
|
|
|
def execute(skip_authorization: false)
|
|
|
|
@skip_authorization = skip_authorization
|
2021-04-08 18:09:32 +00:00
|
|
|
items = find_union(item_ids, Label) || Label.none
|
2016-09-19 20:16:16 +00:00
|
|
|
items = with_title(items)
|
2018-09-27 14:19:28 +00:00
|
|
|
items = by_subscription(items)
|
2018-07-20 14:19:58 +00:00
|
|
|
items = by_search(items)
|
2016-09-19 20:16:16 +00:00
|
|
|
sort(items)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-10-28 09:31:45 +00:00
|
|
|
attr_reader :current_user, :params, :skip_authorization
|
2016-09-19 20:16:16 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2021-04-08 18:09:32 +00:00
|
|
|
def item_ids
|
|
|
|
item_ids = []
|
2016-09-29 18:50:14 +00:00
|
|
|
|
2016-11-16 09:51:47 +00:00
|
|
|
if project?
|
|
|
|
if project
|
2017-03-26 21:43:35 +00:00
|
|
|
if project.group.present?
|
|
|
|
labels_table = Label.arel_table
|
2018-04-04 15:40:29 +00:00
|
|
|
group_ids = group_ids_for(project.group)
|
2017-03-26 21:43:35 +00:00
|
|
|
|
2021-04-08 18:09:32 +00:00
|
|
|
item_ids << Label.where(
|
2018-04-04 15:40:29 +00:00
|
|
|
labels_table[:type].eq('GroupLabel').and(labels_table[:group_id].in(group_ids)).or(
|
2017-03-26 21:43:35 +00:00
|
|
|
labels_table[:type].eq('ProjectLabel').and(labels_table[:project_id].eq(project.id))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else
|
2021-04-08 18:09:32 +00:00
|
|
|
item_ids << project.labels
|
2017-03-26 21:43:35 +00:00
|
|
|
end
|
2016-11-16 09:51:47 +00:00
|
|
|
end
|
2016-09-29 18:50:14 +00:00
|
|
|
else
|
2018-04-04 15:40:29 +00:00
|
|
|
if group?
|
2021-04-08 18:09:32 +00:00
|
|
|
item_ids << Label.where(group_id: group_ids_for(group))
|
2018-04-04 15:40:29 +00:00
|
|
|
end
|
|
|
|
|
2021-04-08 18:09:32 +00:00
|
|
|
item_ids << Label.where(group_id: projects.group_ids)
|
|
|
|
item_ids << Label.where(project_id: ids_user_can_read_labels(projects)) unless only_group_labels?
|
2016-09-29 18:50:14 +00:00
|
|
|
end
|
|
|
|
|
2021-04-08 18:09:32 +00:00
|
|
|
item_ids
|
2016-09-19 20:16:16 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-09-19 20:16:16 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-09-20 14:55:31 +00:00
|
|
|
def sort(items)
|
2018-09-10 11:38:29 +00:00
|
|
|
if params[:sort]
|
|
|
|
items.order_by(params[:sort])
|
|
|
|
else
|
|
|
|
items.reorder(title: :asc)
|
|
|
|
end
|
2016-09-20 14:55:31 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-09-20 14:55:31 +00:00
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-09-19 20:16:16 +00:00
|
|
|
def with_title(items)
|
2016-10-25 06:04:38 +00:00
|
|
|
return items if title.nil?
|
|
|
|
return items.none if title.blank?
|
|
|
|
|
|
|
|
items.where(title: title)
|
2016-09-19 20:16:16 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-09-19 20:16:16 +00:00
|
|
|
|
2018-07-20 14:19:58 +00:00
|
|
|
def by_search(labels)
|
2018-07-28 08:32:50 +00:00
|
|
|
return labels unless search?
|
2018-07-20 14:19:58 +00:00
|
|
|
|
|
|
|
labels.search(params[:search])
|
|
|
|
end
|
|
|
|
|
2018-09-27 14:19:28 +00:00
|
|
|
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
|
|
|
|
|
2016-11-16 09:51:47 +00:00
|
|
|
def projects?
|
2018-01-17 14:28:36 +00:00
|
|
|
params[:project_ids]
|
2016-09-29 18:50:14 +00:00
|
|
|
end
|
|
|
|
|
2017-12-06 19:07:47 +00:00
|
|
|
def only_group_labels?
|
|
|
|
params[:only_group_labels]
|
|
|
|
end
|
|
|
|
|
2018-07-28 08:32:50 +00:00
|
|
|
def search?
|
|
|
|
params[:search].present?
|
|
|
|
end
|
|
|
|
|
2016-09-19 20:16:16 +00:00
|
|
|
def title
|
2016-10-25 06:04:38 +00:00
|
|
|
params[:title] || params[:name]
|
2016-09-19 20:16:16 +00:00
|
|
|
end
|
|
|
|
|
2021-04-08 18:09:32 +00:00
|
|
|
def project?
|
|
|
|
params[:project].present? || params[:project_id].present?
|
|
|
|
end
|
|
|
|
|
2016-09-29 18:50:14 +00:00
|
|
|
def project
|
|
|
|
return @project if defined?(@project)
|
|
|
|
|
2016-11-16 09:51:47 +00:00
|
|
|
if project?
|
2018-10-03 02:56:51 +00:00
|
|
|
@project = params[:project] || Project.find(params[:project_id])
|
2021-04-08 18:09:32 +00:00
|
|
|
@project = nil unless authorized_to_read_item?(@project)
|
2016-09-29 18:50:14 +00:00
|
|
|
else
|
|
|
|
@project = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
@project
|
|
|
|
end
|
|
|
|
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2016-09-19 20:16:16 +00:00
|
|
|
def projects
|
|
|
|
return @projects if defined?(@projects)
|
|
|
|
|
2017-06-29 05:23:38 +00:00
|
|
|
@projects = if skip_authorization
|
|
|
|
Project.all
|
|
|
|
else
|
2018-08-27 15:31:01 +00:00
|
|
|
ProjectsFinder.new(params: { non_archived: true }, current_user: current_user).execute # rubocop: disable CodeReuse/Finder
|
2017-06-29 05:23:38 +00:00
|
|
|
end
|
|
|
|
|
2020-09-03 12:08:47 +00:00
|
|
|
if group?
|
2021-02-15 12:09:29 +00:00
|
|
|
@projects = if params[:include_descendant_groups]
|
2020-09-03 12:08:47 +00:00
|
|
|
@projects.in_namespace(group.self_and_descendants.select(:id))
|
|
|
|
else
|
|
|
|
@projects.in_namespace(group.id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-11-16 09:51:47 +00:00
|
|
|
@projects = @projects.where(id: params[:project_ids]) if projects?
|
2016-09-20 14:55:31 +00:00
|
|
|
@projects = @projects.reorder(nil)
|
2016-09-19 20:16:16 +00:00
|
|
|
|
|
|
|
@projects
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2016-09-29 18:50:14 +00:00
|
|
|
|
2021-04-08 18:09:32 +00:00
|
|
|
def read_permission
|
|
|
|
:read_label
|
2018-02-26 16:23:19 +00:00
|
|
|
end
|
2019-09-12 16:14:43 +00:00
|
|
|
|
|
|
|
# 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-09-19 20:16:16 +00:00
|
|
|
end
|