2018-01-18 11:07:06 -05:00
|
|
|
# Snippets Finder
|
|
|
|
#
|
|
|
|
# Used to filter Snippets collections by a set of params
|
|
|
|
#
|
|
|
|
# Arguments.
|
|
|
|
#
|
|
|
|
# current_user - The current user, nil also can be used.
|
|
|
|
# params:
|
|
|
|
# visibility (integer) - Individual snippet visibility: Public(20), internal(10) or private(0).
|
|
|
|
# project (Project) - Project related.
|
|
|
|
# author (User) - Author related.
|
|
|
|
#
|
|
|
|
# params are optional
|
2017-04-28 18:06:27 -04:00
|
|
|
class SnippetsFinder < UnionFinder
|
2018-01-18 11:07:06 -05:00
|
|
|
include Gitlab::Allowable
|
|
|
|
attr_accessor :current_user, :params, :project
|
2017-04-28 18:06:27 -04:00
|
|
|
|
|
|
|
def initialize(current_user, params = {})
|
|
|
|
@current_user = current_user
|
|
|
|
@params = params
|
2018-01-18 11:07:06 -05:00
|
|
|
@project = params[:project]
|
2017-04-28 18:06:27 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
items = init_collection
|
|
|
|
items = by_author(items)
|
|
|
|
items = by_visibility(items)
|
|
|
|
|
|
|
|
items.fresh
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
def init_collection
|
2018-01-18 11:07:06 -05:00
|
|
|
if project.present?
|
|
|
|
authorized_snippets_from_project
|
|
|
|
else
|
|
|
|
authorized_snippets
|
|
|
|
end
|
|
|
|
end
|
2017-04-28 18:06:27 -04:00
|
|
|
|
2018-01-18 11:07:06 -05:00
|
|
|
def authorized_snippets_from_project
|
|
|
|
if can?(current_user, :read_project_snippet, project)
|
|
|
|
if project.team.member?(current_user)
|
|
|
|
project.snippets
|
|
|
|
else
|
|
|
|
project.snippets.public_to_user(current_user)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Snippet.none
|
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
2018-01-18 11:07:06 -05:00
|
|
|
def authorized_snippets
|
|
|
|
Snippet.where(feature_available_projects.or(not_project_related)).public_or_visible_to_user(current_user)
|
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
|
2018-01-18 11:07:06 -05:00
|
|
|
def feature_available_projects
|
|
|
|
projects = Project.public_or_visible_to_user(current_user)
|
|
|
|
.with_feature_available_for_user(:snippets, current_user).select(:id)
|
|
|
|
arel_query = Arel::Nodes::SqlLiteral.new(projects.to_sql)
|
|
|
|
table[:project_id].in(arel_query)
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|
|
|
|
|
2018-01-18 11:07:06 -05:00
|
|
|
def not_project_related
|
|
|
|
table[:project_id].eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
def table
|
|
|
|
Snippet.arel_table
|
2017-04-28 18:06:27 -04:00
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
|
2017-04-28 18:06:27 -04:00
|
|
|
def by_visibility(items)
|
|
|
|
visibility = params[:visibility] || visibility_from_scope
|
|
|
|
|
|
|
|
return items unless visibility
|
|
|
|
|
|
|
|
items.where(visibility_level: visibility)
|
|
|
|
end
|
|
|
|
|
|
|
|
def by_author(items)
|
|
|
|
return items unless params[:author]
|
|
|
|
|
|
|
|
items.where(author_id: params[:author].id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def visibility_from_scope
|
|
|
|
case params[:scope].to_s
|
2016-12-09 15:40:48 -05:00
|
|
|
when 'are_private'
|
2017-04-28 18:06:27 -04:00
|
|
|
Snippet::PRIVATE
|
2016-12-09 15:40:48 -05:00
|
|
|
when 'are_internal'
|
2017-04-28 18:06:27 -04:00
|
|
|
Snippet::INTERNAL
|
2016-12-09 15:40:48 -05:00
|
|
|
when 'are_public'
|
2017-04-28 18:06:27 -04:00
|
|
|
Snippet::PUBLIC
|
2016-12-09 15:40:48 -05:00
|
|
|
else
|
2017-04-28 18:06:27 -04:00
|
|
|
nil
|
2016-12-09 15:40:48 -05:00
|
|
|
end
|
|
|
|
end
|
2014-10-08 09:44:25 -04:00
|
|
|
end
|