snippets_controller.rb 2.47 KB
Newer Older
1
class SnippetsController < ApplicationController
2
  before_filter :snippet, only: [:show, :edit, :destroy, :update, :raw]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
3 4

  # Allow modify snippet
5
  before_filter :authorize_modify_snippet!, only: [:edit, :update]
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
6 7

  # Allow destroy snippet
8
  before_filter :authorize_admin_snippet!, only: [:destroy]
gitlabhq's avatar
gitlabhq committed
9

10 11
  before_filter :set_title

gitlabhq's avatar
gitlabhq committed
12 13
  respond_to :html

14 15
  layout 'navless'

gitlabhq's avatar
gitlabhq committed
16
  def index
17
    @snippets = Snippet.are_internal.fresh.non_expired.page(params[:page]).per(20)
18 19 20
  end

  def user_index
skv's avatar
skv committed
21
    @user = User.find_by(username: params[:username])
22 23 24

    render_404 and return unless @user

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
25 26 27 28
    @snippets = @user.snippets.fresh.non_expired

    if @user == current_user
      @snippets = case params[:scope]
29 30
                  when 'are_internal' then
                    @snippets.are_internal
31 32
                  when 'are_private' then
                    @snippets.are_private
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
33 34 35 36
                  else
                    @snippets
                  end
    else
37
      @snippets = @snippets.are_internal
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
38
    end
39 40

    @snippets = @snippets.page(params[:page]).per(20)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
41 42 43 44 45 46

    if @user == current_user
      render 'current_user_index'
    else
      render 'user_index'
    end
gitlabhq's avatar
gitlabhq committed
47 48
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
49
  def new
Andrew8xx8's avatar
Andrew8xx8 committed
50
    @snippet = PersonalSnippet.new
gitlabhq's avatar
gitlabhq committed
51 52 53
  end

  def create
54
    @snippet = PersonalSnippet.new(snippet_params)
gitlabhq's avatar
gitlabhq committed
55 56
    @snippet.author = current_user

Andrew8xx8's avatar
Andrew8xx8 committed
57
    if @snippet.save
58
      redirect_to snippet_path(@snippet)
gitlabhq's avatar
gitlabhq committed
59
    else
60
      respond_with @snippet
gitlabhq's avatar
gitlabhq committed
61 62 63 64 65 66 67
    end
  end

  def edit
  end

  def update
68
    if @snippet.update_attributes(snippet_params)
69
      redirect_to snippet_path(@snippet)
gitlabhq's avatar
gitlabhq committed
70
    else
71
      respond_with @snippet
gitlabhq's avatar
gitlabhq committed
72 73 74 75 76 77 78
    end
  end

  def show
  end

  def destroy
79
    return access_denied! unless can?(current_user, :admin_personal_snippet, @snippet)
gitlabhq's avatar
gitlabhq committed
80 81 82

    @snippet.destroy

83
    redirect_to snippets_path
gitlabhq's avatar
gitlabhq committed
84
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
85

86
  def raw
87 88
    send_data(
      @snippet.content,
89
      type: 'text/plain; charset=utf-8',
90 91
      disposition: 'inline',
      filename: @snippet.file_name
92 93 94
    )
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
95
  protected
96

97
  def snippet
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
98
    @snippet ||= PersonalSnippet.where('author_id = :user_id or private is false', user_id: current_user.id).find(params[:id])
99
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
100 101

  def authorize_modify_snippet!
102
    return render_404 unless can?(current_user, :modify_personal_snippet, @snippet)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
103 104 105
  end

  def authorize_admin_snippet!
106
    return render_404 unless can?(current_user, :admin_personal_snippet, @snippet)
107
  end
108 109 110 111

  def set_title
    @title = 'Snippets'
  end
112 113 114 115

  def snippet_params
    params.require(:personal_snippet).permit(:title, :content, :file_name, :private)
  end
gitlabhq's avatar
gitlabhq committed
116
end