BigW Consortium Gitlab

snippets_controller.rb 2.39 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
    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)

27
      render 'index'
Dmitriy Zaporozhets committed
28
    else
29
      redirect_to(current_user ? dashboard_snippets_path : explore_snippets_path)
Dmitriy Zaporozhets committed
30
    end
gitlabhq committed
31 32
  end

Nihad Abbasov committed
33
  def new
Andrew8xx8 committed
34
    @snippet = PersonalSnippet.new
gitlabhq committed
35 36 37
  end

  def create
38 39
    @snippet = CreateSnippetService.new(nil, current_user,
                                        snippet_params).execute
gitlabhq committed
40

41
    respond_with @snippet.becomes(Snippet)
gitlabhq committed
42 43 44 45 46 47
  end

  def edit
  end

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

  def show
  end

  def destroy
57
    return access_denied! unless can?(current_user, :admin_personal_snippet, @snippet)
gitlabhq committed
58 59 60

    @snippet.destroy

61
    redirect_to snippets_path
gitlabhq committed
62
  end
63

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

73
  protected
74

75
  def snippet
76 77 78 79 80 81 82 83
    @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
84
  end
85

86
  def authorize_update_snippet!
87
    return render_404 unless can?(current_user, :update_personal_snippet, @snippet)
88 89 90
  end

  def authorize_admin_snippet!
91
    return render_404 unless can?(current_user, :admin_personal_snippet, @snippet)
92
  end
93

94
  def snippet_params
95 96
    params.require(:personal_snippet).permit(:title, :content, :file_name, :private, :visibility_level)
  end
gitlabhq committed
97
end