BigW Consortium Gitlab

snippets_controller.rb 2.58 KB
Newer Older
1
class SnippetsController < ApplicationController
2
  before_action :snippet, only: [:show, :edit, :destroy, :update, :raw]
gitlabhq committed
3

4
  # Allow read snippet
5
  before_action :authorize_read_snippet!, only: [:show, :raw]
6

7
  # Allow modify snippet
8
  before_action :authorize_update_snippet!, only: [:edit, :update]
9

10
  # Allow destroy snippet
11
  before_action :authorize_admin_snippet!, only: [:destroy]
gitlabhq committed
12

13
  skip_before_action :authenticate_user!, only: [:index, :user_index, :show, :raw]
14

15
  layout 'snippets'
gitlabhq committed
16 17 18
  respond_to :html

  def index
19 20 21 22 23 24 25 26 27 28 29
    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]).per(PER_PAGE)

30
      render 'index'
Dmitriy Zaporozhets committed
31
    else
32
      redirect_to(current_user ? dashboard_snippets_path : explore_snippets_path)
Dmitriy Zaporozhets committed
33
    end
gitlabhq committed
34 35
  end

Nihad Abbasov committed
36
  def new
Andrew8xx8 committed
37
    @snippet = PersonalSnippet.new
gitlabhq committed
38 39 40
  end

  def create
41 42
    @snippet = CreateSnippetService.new(nil, current_user,
                                        snippet_params).execute
gitlabhq committed
43

44
    respond_with @snippet.becomes(Snippet)
gitlabhq committed
45 46 47 48 49 50
  end

  def edit
  end

  def update
51 52 53
    UpdateSnippetService.new(nil, current_user, @snippet,
                             snippet_params).execute
    respond_with @snippet.becomes(Snippet)
gitlabhq committed
54 55 56 57 58 59
  end

  def show
  end

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

    @snippet.destroy

64
    redirect_to snippets_path
gitlabhq committed
65
  end
66

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

76
  protected
77

78
  def snippet
79 80 81 82 83 84
    @snippet ||= if current_user
                   PersonalSnippet.where("author_id = ? OR visibility_level IN (?)",
                     current_user.id,
                     [Snippet::PUBLIC, Snippet::INTERNAL]).
                     find(params[:id])
                 else
85
                   PersonalSnippet.find(params[:id])
86
                 end
87
  end
88

89
  def authorize_read_snippet!
90 91 92
    authenticate_user! unless can?(current_user, :read_personal_snippet, @snippet)
  end

93
  def authorize_update_snippet!
94
    return render_404 unless can?(current_user, :update_personal_snippet, @snippet)
95 96 97
  end

  def authorize_admin_snippet!
98
    return render_404 unless can?(current_user, :admin_personal_snippet, @snippet)
99
  end
100

101
  def snippet_params
102 103
    params.require(:personal_snippet).permit(:title, :content, :file_name, :private, :visibility_level)
  end
gitlabhq committed
104
end