BigW Consortium Gitlab

snippets_controller.rb 1.79 KB
Newer Older
gitlabhq committed
1
class SnippetsController < ApplicationController
Nihad Abbasov committed
2
  before_filter :project
3
  before_filter :snippet, only: [:show, :edit, :destroy, :update, :raw]
gitlabhq committed
4
  layout "project"
gitlabhq committed
5 6 7

  # Authorize
  before_filter :add_project_abilities
8 9

  # Allow read any snippet
gitlabhq committed
10
  before_filter :authorize_read_snippet!
11 12

  # Allow write(create) snippet
13
  before_filter :authorize_write_snippet!, only: [:new, :create]
14 15

  # Allow modify snippet
16
  before_filter :authorize_modify_snippet!, only: [:edit, :update]
17 18

  # Allow destroy snippet
19
  before_filter :authorize_admin_snippet!, only: [:destroy]
gitlabhq committed
20 21 22 23 24 25 26

  respond_to :html

  def index
    @snippets = @project.snippets
  end

Nihad Abbasov committed
27
  def new
gitlabhq committed
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
    @snippet = @project.snippets.new
  end

  def create
    @snippet = @project.snippets.new(params[:snippet])
    @snippet.author = current_user
    @snippet.save

    if @snippet.valid?
      redirect_to [@project, @snippet]
    else
      respond_with(@snippet)
    end
  end

  def edit
  end

  def update
    @snippet.update_attributes(params[:snippet])

    if @snippet.valid?
      redirect_to [@project, @snippet]
    else
      respond_with(@snippet)
    end
  end

  def show
57
    @note = @project.notes.new(noteable: @snippet)
gitlabhq committed
58 59 60
  end

  def destroy
gitlabhq committed
61
    return access_denied! unless can?(current_user, :admin_snippet, @snippet)
gitlabhq committed
62 63 64

    @snippet.destroy

gitlabhq committed
65
    redirect_to project_snippets_path(@project)
gitlabhq committed
66
  end
67

68 69 70
  def raw 
    send_data(
      @snippet.content,
71 72 73
      type: "text/plain",
      disposition: 'inline',
      filename: @snippet.file_name
74 75 76
    )
  end

77
  protected
78

79 80 81
  def snippet
    @snippet ||= @project.snippets.find(params[:id])
  end
82 83

  def authorize_modify_snippet!
84
    return render_404 unless can?(current_user, :modify_snippet, @snippet)
85 86 87
  end

  def authorize_admin_snippet!
88
    return render_404 unless can?(current_user, :admin_snippet, @snippet)
89
  end
gitlabhq committed
90
end