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

100 lines
2.6 KiB
Ruby
Raw Normal View History

2013-03-23 15:13:51 -04:00
class Projects::SnippetsController < Projects::ApplicationController
before_action :module_enabled
before_action :snippet, only: [:show, :edit, :destroy, :update, :raw]
2013-03-23 14:14:37 -04:00
# Allow read any snippet
before_action :authorize_read_project_snippet!
2013-03-23 14:14:37 -04:00
# Allow write(create) snippet
before_action :authorize_create_project_snippet!, only: [:new, :create]
2013-03-23 14:14:37 -04:00
# Allow modify snippet
before_action :authorize_update_project_snippet!, only: [:edit, :update]
2013-03-23 14:14:37 -04:00
# Allow destroy snippet
before_action :authorize_admin_project_snippet!, only: [:destroy]
2013-03-23 14:14:37 -04:00
respond_to :html
def index
2014-10-08 09:44:25 -04:00
@snippets = SnippetsFinder.new.execute(current_user, {
filter: :by_project,
project: @project
})
2015-10-18 07:03:26 -04:00
@snippets = @snippets.page(params[:page]).per(PER_PAGE)
2013-03-23 14:14:37 -04:00
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
@snippet = CreateSnippetService.new(@project, current_user,
snippet_params).execute
if @snippet.valid?
respond_with(@snippet,
location: namespace_project_snippet_path(@project.namespace,
@project, @snippet))
else
render :new
end
2013-03-23 14:14:37 -04:00
end
def edit
end
def update
UpdateSnippetService.new(project, current_user, @snippet,
snippet_params).execute
respond_with(@snippet,
location: namespace_project_snippet_path(@project.namespace,
@project, @snippet))
2013-03-23 14:14:37 -04:00
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 namespace_project_snippets_path(@project.namespace, @project)
2013-03-23 14:14:37 -04:00
end
def raw
send_data(
@snippet.content,
type: 'text/plain; charset=utf-8',
2013-03-23 14:14:37 -04:00
disposition: 'inline',
filename: @snippet.sanitized_file_name
2013-03-23 14:14:37 -04:00
)
end
protected
def snippet
@snippet ||= @project.snippets.find(params[:id])
end
def authorize_update_project_snippet!
return render_404 unless can?(current_user, :update_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
def snippet_params
2014-10-08 09:44:25 -04:00
params.require(:project_snippet).permit(:title, :content, :file_name, :private, :visibility_level)
end
2013-03-23 14:14:37 -04:00
end