debian-mirror-gitlab/app/controllers/projects/snippets_controller.rb

100 lines
2.6 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
class Projects::SnippetsController < Projects::ApplicationController
2015-09-11 14:41:01 +05:30
before_action :module_enabled
before_action :snippet, only: [:show, :edit, :destroy, :update, :raw]
2014-09-02 18:07:02 +05:30
# Allow read any snippet
2015-09-11 14:41:01 +05:30
before_action :authorize_read_project_snippet!
2014-09-02 18:07:02 +05:30
# Allow write(create) snippet
2015-09-11 14:41:01 +05:30
before_action :authorize_create_project_snippet!, only: [:new, :create]
2014-09-02 18:07:02 +05:30
# Allow modify snippet
2015-09-11 14:41:01 +05:30
before_action :authorize_update_project_snippet!, only: [:edit, :update]
2014-09-02 18:07:02 +05:30
# Allow destroy snippet
2015-09-11 14:41:01 +05:30
before_action :authorize_admin_project_snippet!, only: [:destroy]
2014-09-02 18:07:02 +05:30
respond_to :html
def index
2015-04-26 12:48:37 +05:30
@snippets = SnippetsFinder.new.execute(current_user, {
filter: :by_project,
project: @project
})
2015-11-26 14:37:03 +05:30
@snippets = @snippets.page(params[:page]).per(PER_PAGE)
2014-09-02 18:07:02 +05:30
end
def new
@snippet = @noteable = @project.snippets.build
2014-09-02 18:07:02 +05:30
end
def create
2015-04-26 12:48:37 +05:30
@snippet = CreateSnippetService.new(@project, current_user,
snippet_params).execute
2015-09-25 12:07:36 +05:30
if @snippet.valid?
respond_with(@snippet,
location: namespace_project_snippet_path(@project.namespace,
@project, @snippet))
else
render :new
end
2014-09-02 18:07:02 +05:30
end
def edit
end
def update
2015-04-26 12:48:37 +05:30
UpdateSnippetService.new(project, current_user, @snippet,
snippet_params).execute
respond_with(@snippet,
location: namespace_project_snippet_path(@project.namespace,
@project, @snippet))
2014-09-02 18:07:02 +05:30
end
def show
@note = @project.notes.new(noteable: @snippet)
@notes = @snippet.notes.fresh
@noteable = @snippet
end
def destroy
return access_denied! unless can?(current_user, :admin_project_snippet, @snippet)
@snippet.destroy
2015-04-26 12:48:37 +05:30
redirect_to namespace_project_snippets_path(@project.namespace, @project)
2014-09-02 18:07:02 +05:30
end
def raw
send_data(
@snippet.content,
2015-04-26 12:48:37 +05:30
type: 'text/plain; charset=utf-8',
2014-09-02 18:07:02 +05:30
disposition: 'inline',
2015-04-26 12:48:37 +05:30
filename: @snippet.sanitized_file_name
2014-09-02 18:07:02 +05:30
)
end
protected
def snippet
@snippet ||= @project.snippets.find(params[:id])
end
2015-09-11 14:41:01 +05:30
def authorize_update_project_snippet!
return render_404 unless can?(current_user, :update_project_snippet, @snippet)
2014-09-02 18:07:02 +05:30
end
def authorize_admin_project_snippet!
return render_404 unless can?(current_user, :admin_project_snippet, @snippet)
end
def module_enabled
return render_404 unless @project.snippets_enabled
end
def snippet_params
2015-04-26 12:48:37 +05:30
params.require(:project_snippet).permit(:title, :content, :file_name, :private, :visibility_level)
2014-09-02 18:07:02 +05:30
end
end