summaryrefslogtreecommitdiff
path: root/app/finders/snippets_finder.rb
blob: 01cbf91c658b8a9a5d7a2d2652dce5eba14925ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class SnippetsFinder
  def execute(current_user, params = {})
    filter = params[:filter]

    case filter
    when :all then
      snippets(current_user).fresh
    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)
    snippets = user.snippets.fresh

    return snippets.are_public unless current_user

    if user == current_user
      case scope
      when 'are_internal' then
        snippets.are_internal
      when 'are_private' then
        snippets.are_private
      when 'are_public' then
        snippets.are_public
      else
        snippets
      end
    else
      snippets.public_and_internal
    end
  end

  def by_project(current_user, project)
    snippets = project.snippets.fresh

    if current_user
      if project.team.member?(current_user.id) || current_user.admin?
        snippets
      else
        snippets.public_and_internal
      end
    else
      snippets.are_public
    end
  end
end