2013-03-23 15:13:51 -04:00
|
|
|
class Projects::SnippetsController < Projects::ApplicationController
|
2015-04-16 08:03:37 -04:00
|
|
|
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
|
2015-04-16 08:03:37 -04:00
|
|
|
before_action :authorize_read_project_snippet!
|
2013-03-23 14:14:37 -04:00
|
|
|
|
|
|
|
# Allow write(create) snippet
|
2015-06-26 10:44:21 -04:00
|
|
|
before_action :authorize_create_project_snippet!, only: [:new, :create]
|
2013-03-23 14:14:37 -04:00
|
|
|
|
|
|
|
# Allow modify snippet
|
2015-06-26 10:44:21 -04:00
|
|
|
before_action :authorize_update_project_snippet!, only: [:edit, :update]
|
2013-03-23 14:14:37 -04:00
|
|
|
|
|
|
|
# Allow destroy snippet
|
2015-04-16 08:03:37 -04:00
|
|
|
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
|
2016-01-15 05:29:53 -05:00
|
|
|
@snippet = @noteable = @project.snippets.build
|
2013-03-23 14:14:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2015-03-07 14:47:06 -05:00
|
|
|
@snippet = CreateSnippetService.new(@project, current_user,
|
|
|
|
snippet_params).execute
|
2015-08-26 17:59:52 -04:00
|
|
|
|
|
|
|
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
|
2015-03-07 14:47:06 -05:00
|
|
|
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)
|
2013-12-25 06:32:43 -05:00
|
|
|
@notes = @snippet.notes.fresh
|
2013-12-25 15:32:48 -05:00
|
|
|
@noteable = @snippet
|
2013-03-23 14:14:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2013-03-24 14:31:14 -04:00
|
|
|
return access_denied! unless can?(current_user, :admin_project_snippet, @snippet)
|
2013-03-23 14:14:37 -04:00
|
|
|
|
|
|
|
@snippet.destroy
|
|
|
|
|
2015-01-24 13:02:58 -05:00
|
|
|
redirect_to namespace_project_snippets_path(@project.namespace, @project)
|
2013-03-23 14:14:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def raw
|
|
|
|
send_data(
|
|
|
|
@snippet.content,
|
2014-08-16 08:55:36 -04:00
|
|
|
type: 'text/plain; charset=utf-8',
|
2013-03-23 14:14:37 -04:00
|
|
|
disposition: 'inline',
|
2014-12-12 06:28:48 -05:00
|
|
|
filename: @snippet.sanitized_file_name
|
2013-03-23 14:14:37 -04:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def snippet
|
|
|
|
@snippet ||= @project.snippets.find(params[:id])
|
|
|
|
end
|
|
|
|
|
2015-06-26 10:44:21 -04:00
|
|
|
def authorize_update_project_snippet!
|
2015-06-26 09:55:56 -04:00
|
|
|
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!
|
2013-03-24 14:31:14 -04:00
|
|
|
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
|
2014-06-26 11:51:11 -04:00
|
|
|
|
|
|
|
def snippet_params
|
2014-10-08 09:44:25 -04:00
|
|
|
params.require(:project_snippet).permit(:title, :content, :file_name, :private, :visibility_level)
|
2014-06-26 11:51:11 -04:00
|
|
|
end
|
2013-03-23 14:14:37 -04:00
|
|
|
end
|