snippets_controller.rb 2.61 KB
Newer Older
1
class SnippetsController < ApplicationController
2 3
  include ToggleAwardEmoji

4
  before_action :snippet, only: [:show, :edit, :destroy, :update, :raw]
gitlabhq's avatar
gitlabhq committed
5

6
  # Allow read snippet
7
  before_action :authorize_read_snippet!, only: [:show, :raw]
8

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
9
  # Allow modify snippet
10
  before_action :authorize_update_snippet!, only: [:edit, :update]
11

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
12
  # Allow destroy snippet
13
  before_action :authorize_admin_snippet!, only: [:destroy]
gitlabhq's avatar
gitlabhq committed
14

Long Nguyen's avatar
Long Nguyen committed
15
  skip_before_action :authenticate_user!, only: [:index, :show, :raw]
16

17
  layout 'snippets'
gitlabhq's avatar
gitlabhq committed
18 19
  respond_to :html

Long Nguyen's avatar
Long Nguyen committed
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
  def index
    if params[:username].present?
      @user = User.find_by(username: params[:username])

      render_404 and return unless @user

      @snippets = SnippetsFinder.new.execute(current_user, {
        filter: :by_user,
        user: @user,
        scope: params[:scope] }).
      page(params[:page])

      render 'index'
    else
      redirect_to(current_user ? dashboard_snippets_path : explore_snippets_path)
    end
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
38
  def new
Andrew8xx8's avatar
Andrew8xx8 committed
39
    @snippet = PersonalSnippet.new
gitlabhq's avatar
gitlabhq committed
40 41 42
  end

  def create
43 44
    @snippet = CreateSnippetService.new(nil, current_user,
                                        snippet_params).execute
gitlabhq's avatar
gitlabhq committed
45

46
    respond_with @snippet.becomes(Snippet)
gitlabhq's avatar
gitlabhq committed
47 48 49 50 51 52
  end

  def edit
  end

  def update
53 54 55
    UpdateSnippetService.new(nil, current_user, @snippet,
                             snippet_params).execute
    respond_with @snippet.becomes(Snippet)
gitlabhq's avatar
gitlabhq committed
56 57 58 59 60 61
  end

  def show
  end

  def destroy
62
    return access_denied! unless can?(current_user, :admin_personal_snippet, @snippet)
gitlabhq's avatar
gitlabhq committed
63 64 65

    @snippet.destroy

Long Nguyen's avatar
Long Nguyen committed
66
    redirect_to snippets_path
gitlabhq's avatar
gitlabhq committed
67
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
68

69
  def raw
70 71
    send_data(
      @snippet.content,
72
      type: 'text/plain; charset=utf-8',
73
      disposition: 'inline',
74
      filename: @snippet.sanitized_file_name
75 76 77
    )
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
78
  protected
79

80
  def snippet
81 82 83 84 85 86
    @snippet ||= if current_user
                   PersonalSnippet.where("author_id = ? OR visibility_level IN (?)",
                     current_user.id,
                     [Snippet::PUBLIC, Snippet::INTERNAL]).
                     find(params[:id])
                 else
87
                   PersonalSnippet.find(params[:id])
88
                 end
89
  end
90
  alias_method :awardable, :snippet
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
91

92
  def authorize_read_snippet!
93 94 95
    authenticate_user! unless can?(current_user, :read_personal_snippet, @snippet)
  end

96
  def authorize_update_snippet!
97
    return render_404 unless can?(current_user, :update_personal_snippet, @snippet)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
98 99 100
  end

  def authorize_admin_snippet!
101
    return render_404 unless can?(current_user, :admin_personal_snippet, @snippet)
102
  end
103

104
  def snippet_params
105 106
    params.require(:personal_snippet).permit(:title, :content, :file_name, :private, :visibility_level)
  end
gitlabhq's avatar
gitlabhq committed
107
end