BigW Consortium Gitlab

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

4
  # Allow modify snippet
5
  before_action :authorize_update_snippet!, only: [:edit, :update]
6

7
  # Allow destroy snippet
8
  before_action :authorize_admin_snippet!, only: [:destroy]
gitlabhq committed
9

10
  skip_before_action :authenticate_user!, only: [:index, :user_index, :show, :raw]
11

12
  layout 'snippets'
gitlabhq committed
13 14 15
  respond_to :html

  def index
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    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)

      if @user == current_user
        render 'current_user_index'
      else
        render 'user_index'
      end
Dmitriy Zaporozhets committed
32
    else
33
      @snippets = SnippetsFinder.new.execute(current_user, filter: :all).page(params[:page]).per(PER_PAGE)
Dmitriy Zaporozhets committed
34
    end
gitlabhq committed
35 36
  end

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

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

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

  def edit
  end

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

  def show
  end

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

    @snippet.destroy

65
    redirect_to snippets_path
gitlabhq committed
66
  end
67

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

77
  protected
78

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

90
  def authorize_update_snippet!
91
    return render_404 unless can?(current_user, :update_personal_snippet, @snippet)
92 93 94
  end

  def authorize_admin_snippet!
95
    return render_404 unless can?(current_user, :admin_personal_snippet, @snippet)
96
  end
97

98
  def snippet_params
99 100
    params.require(:personal_snippet).permit(:title, :content, :file_name, :private, :visibility_level)
  end
gitlabhq committed
101
end