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

138 lines
3.6 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
class Projects::SnippetsController < Projects::ApplicationController
2017-08-17 22:00:37 +05:30
include RendersNotes
2016-09-29 09:46:39 +05:30
include ToggleAwardEmoji
2017-08-17 22:00:37 +05:30
include SpammableActions
include SnippetsActions
include RendersBlob
2019-12-04 20:38:33 +05:30
include PaginatedCollection
include Gitlab::NoteableMetadata
2016-09-29 09:46:39 +05:30
2019-10-12 21:52:04 +05:30
skip_before_action :verify_authenticity_token,
if: -> { action_name == 'show' && js_request? }
2018-10-15 14:42:47 +05:30
2017-09-10 17:25:29 +05:30
before_action :check_snippets_available!
2017-08-17 22:00:37 +05:30
before_action :snippet, only: [:show, :edit, :destroy, :update, :raw, :toggle_award_emoji, :mark_as_spam]
2014-09-02 18:07:02 +05:30
2020-03-13 15:44:24 +05:30
# Allow create snippet
before_action :authorize_create_snippet!, only: [:new, :create]
2014-09-02 18:07:02 +05:30
2020-03-13 15:44:24 +05:30
# Allow read any snippet
before_action :authorize_read_snippet!, except: [:new, :create, :index]
2014-09-02 18:07:02 +05:30
# Allow modify snippet
2020-03-13 15:44:24 +05:30
before_action :authorize_update_snippet!, only: [:edit, :update]
2014-09-02 18:07:02 +05:30
# Allow destroy snippet
2020-03-13 15:44:24 +05:30
before_action :authorize_admin_snippet!, only: [:destroy]
2014-09-02 18:07:02 +05:30
respond_to :html
def index
2020-03-13 15:44:24 +05:30
@snippet_counts = Snippets::CountService
.new(current_user, project: @project)
.execute
2019-12-04 20:38:33 +05:30
@snippets = SnippetsFinder.new(current_user, project: @project, scope: params[:scope])
.execute
.page(params[:page])
.inc_author
return if redirect_out_of_range(@snippets)
@noteable_meta_data = noteable_meta_data(@snippets, 'Snippet')
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
2017-08-17 22:00:37 +05:30
create_params = snippet_params.merge(spammable_params)
2020-03-13 15:44:24 +05:30
service_response = Snippets::CreateService.new(project, current_user, create_params).execute
@snippet = service_response.payload[:snippet]
2017-08-17 22:00:37 +05:30
recaptcha_check_with_fallback { render :new }
2014-09-02 18:07:02 +05:30
end
def update
2017-08-17 22:00:37 +05:30
update_params = snippet_params.merge(spammable_params)
2020-03-13 15:44:24 +05:30
service_response = Snippets::UpdateService.new(project, current_user, update_params).execute(@snippet)
@snippet = service_response.payload[:snippet]
2017-08-17 22:00:37 +05:30
recaptcha_check_with_fallback { render :edit }
2014-09-02 18:07:02 +05:30
end
def show
2017-08-17 22:00:37 +05:30
blob = @snippet.blob
2017-09-10 17:25:29 +05:30
conditionally_expand_blob(blob)
2017-08-17 22:00:37 +05:30
respond_to do |format|
format.html do
@note = @project.notes.new(noteable: @snippet)
@noteable = @snippet
@discussions = @snippet.discussions
2018-03-17 18:26:18 +05:30
@notes = prepare_notes_for_rendering(@discussions.flat_map(&:notes), @noteable)
2017-08-17 22:00:37 +05:30
render 'show'
end
format.json do
render_blob_json(blob)
end
2019-01-03 12:48:30 +05:30
format.js do
if @snippet.embeddable?
render 'shared/snippets/show'
else
head :not_found
end
end
2017-08-17 22:00:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
def destroy
2020-03-13 15:44:24 +05:30
service_response = Snippets::DestroyService.new(current_user, @snippet).execute
if service_response.success?
redirect_to project_snippets_path(project), status: :found
elsif service_response.http_status == 403
access_denied!
else
redirect_to project_snippet_path(project, @snippet),
status: :found,
alert: service_response.message
end
2014-09-02 18:07:02 +05:30
end
protected
def snippet
2018-11-18 11:00:15 +05:30
@snippet ||= @project.snippets.inc_relations_for_view.find(params[:id])
2014-09-02 18:07:02 +05:30
end
2016-09-29 09:46:39 +05:30
alias_method :awardable, :snippet
2017-08-17 22:00:37 +05:30
alias_method :spammable, :snippet
2014-09-02 18:07:02 +05:30
2017-09-10 17:25:29 +05:30
def spammable_path
project_snippet_path(@project, @snippet)
end
2020-03-13 15:44:24 +05:30
def authorize_read_snippet!
return render_404 unless can?(current_user, :read_snippet, @snippet)
2016-06-02 11:05:42 +05:30
end
2020-03-13 15:44:24 +05:30
def authorize_update_snippet!
return render_404 unless can?(current_user, :update_snippet, @snippet)
2014-09-02 18:07:02 +05:30
end
2020-03-13 15:44:24 +05:30
def authorize_admin_snippet!
return render_404 unless can?(current_user, :admin_snippet, @snippet)
2014-09-02 18:07:02 +05:30
end
def snippet_params
2017-09-10 17:25:29 +05:30
params.require(:project_snippet).permit(:title, :content, :file_name, :private, :visibility_level, :description)
2014-09-02 18:07:02 +05:30
end
end