Use Issue.visible_to_user in Notes.search to avoid query duplication

parent b56c4567
...@@ -51,7 +51,7 @@ class Issue < ActiveRecord::Base ...@@ -51,7 +51,7 @@ class Issue < ActiveRecord::Base
end end
def self.visible_to_user(user) def self.visible_to_user(user)
return where(confidential: false) if user.blank? return where('issues.confidential IS NULL OR issues.confidential IS FALSE') if user.blank?
return all if user.admin? return all if user.admin?
where(' where('
......
...@@ -88,22 +88,9 @@ class Note < ActiveRecord::Base ...@@ -88,22 +88,9 @@ class Note < ActiveRecord::Base
table = arel_table table = arel_table
pattern = "%#{query}%" pattern = "%#{query}%"
found_notes = joins('LEFT JOIN issues ON issues.id = noteable_id'). Note.joins('LEFT JOIN issues ON issues.id = noteable_id').
where(table[:note].matches(pattern)) where(table[:note].matches(pattern)).
merge(Issue.visible_to_user(as_user))
if as_user
found_notes.where('
issues.confidential IS NULL
OR issues.confidential IS FALSE
OR (issues.confidential IS TRUE
AND (issues.author_id = :user_id
OR issues.assignee_id = :user_id
OR issues.project_id IN(:project_ids)))',
user_id: as_user.id,
project_ids: as_user.authorized_projects(Gitlab::Access::REPORTER).select(:id))
else
found_notes.where('issues.confidential IS NULL OR issues.confidential IS FALSE')
end
end end
end end
......
...@@ -825,19 +825,16 @@ class User < ActiveRecord::Base ...@@ -825,19 +825,16 @@ class User < ActiveRecord::Base
private private
def projects_union(min_access_level = nil) def projects_union(min_access_level = nil)
relations = if min_access_level relations = [personal_projects.select(:id),
scope = { access_level: Gitlab::Access.values.select { |access| access >= min_access_level } } groups_projects.select(:id),
projects.select(:id),
[personal_projects.select(:id), groups.joins(:shared_projects).select(:project_id)]
groups_projects.where(members: scope).select(:id),
projects.where(members: scope).select(:id),
groups.joins(:shared_projects).where(members: scope).select(:project_id)] if min_access_level
else scope = { access_level: Gitlab::Access.values.select { |access| access >= min_access_level } }
[personal_projects.select(:id), relations = [relations.shift] + relations.map { |relation| relation.where(members: scope) }
groups_projects.select(:id), end
projects.select(:id),
groups.joins(:shared_projects).select(:project_id)]
end
Gitlab::SQL::Union.new(relations) Gitlab::SQL::Union.new(relations)
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment