BigW Consortium Gitlab

snippets_controller.rb 2.59 KB
Newer Older
1
class SnippetsController < ApplicationController
2
  before_filter :snippet, only: [:show, :edit, :destroy, :update, :raw]
3 4

  # Allow modify snippet
5
  before_filter :authorize_modify_snippet!, only: [:edit, :update]
6 7

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

10 11
  before_filter :set_title

12
  skip_before_filter :authenticate_user!, only: [:index, :user_index, :show, :raw]
13

gitlabhq committed
14 15
  respond_to :html

16
  layout :determine_layout
17

gitlabhq committed
18
  def index
19
    @snippets = SnippetsFinder.new.execute(current_user, filter: :all).page(params[:page]).per(20)
20 21 22
  end

  def user_index
skv committed
23
    @user = User.find_by(username: params[:username])
24 25 26

    render_404 and return unless @user

27 28 29 30 31
    @snippets = SnippetsFinder.new.execute(current_user, {
      filter: :by_user,
      user: @user,
      scope: params[:scope]}).
    page(params[:page]).per(20)
Dmitriy Zaporozhets committed
32 33 34 35 36 37

    if @user == current_user
      render 'current_user_index'
    else
      render 'user_index'
    end
gitlabhq committed
38 39
  end

Nihad Abbasov committed
40
  def new
Andrew8xx8 committed
41
    @snippet = PersonalSnippet.new
gitlabhq committed
42 43 44
  end

  def create
45
    @snippet = PersonalSnippet.new(snippet_params)
gitlabhq committed
46 47
    @snippet.author = current_user

Andrew8xx8 committed
48
    if @snippet.save
49
      redirect_to snippet_path(@snippet)
gitlabhq committed
50
    else
51
      respond_with @snippet
gitlabhq committed
52 53 54 55 56 57 58
    end
  end

  def edit
  end

  def update
59
    if @snippet.update_attributes(snippet_params)
60
      redirect_to snippet_path(@snippet)
gitlabhq committed
61
    else
62
      respond_with @snippet
gitlabhq committed
63 64 65 66 67 68 69
    end
  end

  def show
  end

  def destroy
70
    return access_denied! unless can?(current_user, :admin_personal_snippet, @snippet)
gitlabhq committed
71 72 73

    @snippet.destroy

74
    redirect_to snippets_path
gitlabhq committed
75
  end
76

77
  def raw
78 79
    send_data(
      @snippet.content,
80
      type: 'text/plain; charset=utf-8',
81 82
      disposition: 'inline',
      filename: @snippet.file_name
83 84 85
    )
  end

86
  protected
87

88
  def snippet
89 90 91 92 93 94 95 96
    @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
97
  end
98 99

  def authorize_modify_snippet!
100
    return render_404 unless can?(current_user, :modify_personal_snippet, @snippet)
101 102 103
  end

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

  def set_title
    @title = 'Snippets'
  end
110 111

  def snippet_params
112 113 114 115 116
    params.require(:personal_snippet).permit(:title, :content, :file_name, :private, :visibility_level)
  end

  def determine_layout
    current_user ? 'navless' : 'public_users'
117
  end
gitlabhq committed
118
end