2016-09-20 00:08:04 -04:00
|
|
|
class LabelsFinder < UnionFinder
|
2017-12-11 09:21:06 -05:00
|
|
|
prepend FinderWithCrossProjectAccess
|
|
|
|
include FinderMethods
|
2017-12-06 14:07:47 -05:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
|
|
|
|
2017-12-11 09:21:06 -05:00
|
|
|
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
|
|
|
|
|
2016-10-28 05:31:45 -04:00
|
|
|
def execute(skip_authorization: false)
|
|
|
|
@skip_authorization = skip_authorization
|
2016-11-16 04:51:47 -05:00
|
|
|
items = find_union(label_ids, Label) || Label.none
|
2016-09-19 16:16:16 -04:00
|
|
|
items = with_title(items)
|
|
|
|
sort(items)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-10-28 05:31:45 -04:00
|
|
|
attr_reader :current_user, :params, :skip_authorization
|
2016-09-19 16:16:16 -04:00
|
|
|
|
2016-09-20 00:08:04 -04:00
|
|
|
def label_ids
|
2016-09-19 16:16:16 -04:00
|
|
|
label_ids = []
|
2016-09-29 14:50:14 -04:00
|
|
|
|
2016-11-16 04:51:47 -05:00
|
|
|
if project?
|
|
|
|
if project
|
2017-03-26 17:43:35 -04:00
|
|
|
if project.group.present?
|
|
|
|
labels_table = Label.arel_table
|
|
|
|
|
|
|
|
label_ids << Label.where(
|
|
|
|
labels_table[:type].eq('GroupLabel').and(labels_table[:group_id].eq(project.group.id)).or(
|
|
|
|
labels_table[:type].eq('ProjectLabel').and(labels_table[:project_id].eq(project.id))
|
|
|
|
)
|
|
|
|
)
|
|
|
|
else
|
|
|
|
label_ids << project.labels
|
|
|
|
end
|
2016-11-16 04:51:47 -05:00
|
|
|
end
|
2017-12-06 14:07:47 -05:00
|
|
|
elsif only_group_labels?
|
2018-02-26 11:23:19 -05:00
|
|
|
label_ids << Label.where(group_id: group_ids)
|
2016-09-29 14:50:14 -04:00
|
|
|
else
|
|
|
|
label_ids << Label.where(group_id: projects.group_ids)
|
|
|
|
label_ids << Label.where(project_id: projects.select(:id))
|
|
|
|
end
|
|
|
|
|
|
|
|
label_ids
|
2016-09-19 16:16:16 -04:00
|
|
|
end
|
|
|
|
|
2016-09-20 10:55:31 -04:00
|
|
|
def sort(items)
|
2016-10-19 09:59:31 -04:00
|
|
|
items.reorder(title: :asc)
|
2016-09-20 10:55:31 -04:00
|
|
|
end
|
|
|
|
|
2016-09-19 16:16:16 -04:00
|
|
|
def with_title(items)
|
2016-10-25 02:04:38 -04:00
|
|
|
return items if title.nil?
|
|
|
|
return items.none if title.blank?
|
|
|
|
|
|
|
|
items.where(title: title)
|
2016-09-19 16:16:16 -04:00
|
|
|
end
|
|
|
|
|
2018-02-26 11:23:19 -05:00
|
|
|
def group_ids
|
|
|
|
strong_memoize(:group_ids) do
|
2018-03-02 03:25:25 -05:00
|
|
|
groups_user_can_read_labels(groups_to_include).map(&:id)
|
2017-12-06 14:07:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-02 03:25:25 -05:00
|
|
|
def groups_to_include
|
|
|
|
group = Group.find(params[:group_id])
|
|
|
|
groups = [group]
|
|
|
|
|
|
|
|
groups += group.ancestors if params[:include_ancestor_groups].present?
|
|
|
|
groups += group.descendants if params[:include_descendant_groups].present?
|
|
|
|
|
|
|
|
groups
|
|
|
|
end
|
|
|
|
|
2016-11-16 04:51:47 -05:00
|
|
|
def group?
|
|
|
|
params[:group_id].present?
|
2016-09-19 16:16:16 -04:00
|
|
|
end
|
|
|
|
|
2016-11-16 04:51:47 -05:00
|
|
|
def project?
|
|
|
|
params[:project_id].present?
|
2016-09-19 16:16:16 -04:00
|
|
|
end
|
|
|
|
|
2016-11-16 04:51:47 -05:00
|
|
|
def projects?
|
2018-01-17 09:28:36 -05:00
|
|
|
params[:project_ids]
|
2016-09-29 14:50:14 -04:00
|
|
|
end
|
|
|
|
|
2017-12-06 14:07:47 -05:00
|
|
|
def only_group_labels?
|
|
|
|
params[:only_group_labels]
|
|
|
|
end
|
|
|
|
|
2016-09-19 16:16:16 -04:00
|
|
|
def title
|
2016-10-25 02:04:38 -04:00
|
|
|
params[:title] || params[:name]
|
2016-09-19 16:16:16 -04:00
|
|
|
end
|
|
|
|
|
2016-09-29 14:50:14 -04:00
|
|
|
def project
|
|
|
|
return @project if defined?(@project)
|
|
|
|
|
2016-11-16 04:51:47 -05:00
|
|
|
if project?
|
|
|
|
@project = Project.find(params[:project_id])
|
|
|
|
@project = nil unless authorized_to_read_labels?(@project)
|
2016-09-29 14:50:14 -04:00
|
|
|
else
|
|
|
|
@project = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
@project
|
|
|
|
end
|
|
|
|
|
2016-09-19 16:16:16 -04:00
|
|
|
def projects
|
|
|
|
return @projects if defined?(@projects)
|
|
|
|
|
2017-06-29 01:23:38 -04:00
|
|
|
@projects = if skip_authorization
|
|
|
|
Project.all
|
|
|
|
else
|
|
|
|
ProjectsFinder.new(params: { non_archived: true }, current_user: current_user).execute
|
|
|
|
end
|
|
|
|
|
2016-11-16 04:51:47 -05:00
|
|
|
@projects = @projects.in_namespace(params[:group_id]) if group?
|
|
|
|
@projects = @projects.where(id: params[:project_ids]) if projects?
|
2016-09-20 10:55:31 -04:00
|
|
|
@projects = @projects.reorder(nil)
|
2016-09-19 16:16:16 -04:00
|
|
|
|
|
|
|
@projects
|
|
|
|
end
|
2016-09-29 14:50:14 -04:00
|
|
|
|
2017-12-06 14:07:47 -05:00
|
|
|
def authorized_to_read_labels?(label_parent)
|
2016-11-16 04:51:47 -05:00
|
|
|
return true if skip_authorization
|
|
|
|
|
2017-12-06 14:07:47 -05:00
|
|
|
Ability.allowed?(current_user, :read_label, label_parent)
|
2016-09-29 14:50:14 -04:00
|
|
|
end
|
2018-02-26 11:23:19 -05:00
|
|
|
|
|
|
|
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
|