BigW Consortium Gitlab

snippets_finder.rb 1.26 KB
Newer Older
1 2 3 4 5 6
class SnippetsFinder
  def execute(current_user, params = {})
    filter = params[:filter]

    case filter
    when :all then
7
      snippets(current_user).fresh
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    when :by_user then
      by_user(current_user, params[:user], params[:scope])
    when :by_project
      by_project(current_user, params[:project])
    end
  end

  private

  def snippets(current_user)
    if current_user
      Snippet.public_and_internal
    else
      # Not authenticated
      #
      # Return only:
      #   public snippets
      Snippet.are_public
    end
  end

  def by_user(current_user, user, scope)
30
    snippets = user.snippets.fresh
31

32 33
    return snippets.are_public unless current_user

34
    if user == current_user
Valery Sizov committed
35 36 37 38 39 40 41 42
      case scope
      when 'are_internal' then
        snippets.are_internal
      when 'are_private' then
        snippets.are_private
      when 'are_public' then
        snippets.are_public
      else
43
        snippets
Valery Sizov committed
44
      end
45
    else
Valery Sizov committed
46
      snippets.public_and_internal
47 48 49 50
    end
  end

  def by_project(current_user, project)
51
    snippets = project.snippets.fresh
52 53

    if current_user
54
      if project.team.member?(current_user) || current_user.admin?
55 56 57 58 59 60 61 62 63
        snippets
      else
        snippets.public_and_internal
      end
    else
      snippets.are_public
    end
  end
end