gitlab-org--gitlab-foss/app/controllers/projects/snippets_controller.rb

90 lines
2 KiB
Ruby
Raw Normal View History

2013-03-23 15:13:51 -04:00
class Projects::SnippetsController < Projects::ApplicationController
2013-03-23 14:14:37 -04:00
before_filter :module_enabled
before_filter :snippet, only: [:show, :edit, :destroy, :update, :raw]
# Allow read any snippet
2013-03-25 06:22:14 -04:00
before_filter :authorize_read_project_snippet!
2013-03-23 14:14:37 -04:00
# Allow write(create) snippet
2013-03-25 06:22:14 -04:00
before_filter :authorize_write_project_snippet!, only: [:new, :create]
2013-03-23 14:14:37 -04:00
# Allow modify snippet
2013-03-25 06:22:14 -04:00
before_filter :authorize_modify_project_snippet!, only: [:edit, :update]
2013-03-23 14:14:37 -04:00
# Allow destroy snippet
2013-03-25 06:22:14 -04:00
before_filter :authorize_admin_project_snippet!, only: [:destroy]
2013-03-23 14:14:37 -04:00
respond_to :html
def index
@snippets = @project.snippets.fresh.non_expired
end
def new
2013-03-25 03:20:14 -04:00
@snippet = @project.snippets.build
2013-03-23 14:14:37 -04:00
end
def create
2013-03-25 03:20:14 -04:00
@snippet = @project.snippets.build(params[:project_snippet])
2013-03-23 14:14:37 -04:00
@snippet.author = current_user
2013-03-25 08:28:39 -04:00
if @snippet.save
redirect_to project_snippet_path(@project, @snippet)
2013-03-23 14:14:37 -04:00
else
respond_with(@snippet)
end
end
def edit
end
def update
2013-03-25 08:28:39 -04:00
if @snippet.update_attributes(params[:project_snippet])
redirect_to project_snippet_path(@project, @snippet)
2013-03-23 14:14:37 -04:00
else
respond_with(@snippet)
end
end
def show
@note = @project.notes.new(noteable: @snippet)
@notes = @snippet.notes.fresh
@noteable = @snippet
2013-03-23 14:14:37 -04:00
end
def destroy
return access_denied! unless can?(current_user, :admin_project_snippet, @snippet)
2013-03-23 14:14:37 -04:00
@snippet.destroy
redirect_to project_snippets_path(@project)
end
def raw
send_data(
@snippet.content,
type: "text/plain",
disposition: 'inline',
filename: @snippet.file_name
)
end
protected
def snippet
@snippet ||= @project.snippets.find(params[:id])
end
2013-03-25 06:22:14 -04:00
def authorize_modify_project_snippet!
return render_404 unless can?(current_user, :modify_project_snippet, @snippet)
2013-03-23 14:14:37 -04:00
end
2013-03-25 06:22:14 -04:00
def authorize_admin_project_snippet!
return render_404 unless can?(current_user, :admin_project_snippet, @snippet)
2013-03-23 14:14:37 -04:00
end
def module_enabled
return render_404 unless @project.snippets_enabled
end
end