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

175 lines
4.5 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 SnippetsController < 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
2018-03-17 18:26:18 +05:30
include PreviewMarkdown
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-08-17 22:00:37 +05:30
before_action :snippet, only: [:show, :edit, :destroy, :update, :raw]
2014-09-02 18:07:02 +05:30
2020-01-01 13:55:28 +05:30
before_action :authorize_create_snippet!, only: [:new, :create]
2017-08-17 22:00:37 +05:30
before_action :authorize_read_snippet!, only: [:show, :raw]
2015-09-11 14:41:01 +05:30
before_action :authorize_update_snippet!, only: [:edit, :update]
before_action :authorize_admin_snippet!, only: [:destroy]
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
skip_before_action :authenticate_user!, only: [:index, :show, :raw]
2015-04-26 12:48:37 +05:30
2015-09-11 14:41:01 +05:30
layout 'snippets'
2014-09-02 18:07:02 +05:30
respond_to :html
def index
2015-09-11 14:41:01 +05:30
if params[:username].present?
2018-12-13 13:39:08 +05:30
@user = UserFinder.new(params[:username]).find_by_username!
2015-09-11 14:41:01 +05:30
2017-08-17 22:00:37 +05:30
@snippets = SnippetsFinder.new(current_user, author: @user, scope: params[:scope])
2019-12-04 20:38:33 +05:30
.execute
.page(params[:page])
.inc_author
return if redirect_out_of_range(@snippets)
@noteable_meta_data = noteable_meta_data(@snippets, 'Snippet')
2015-09-11 14:41:01 +05:30
2015-09-25 12:07:36 +05:30
render 'index'
2014-09-02 18:07:02 +05:30
else
2015-09-25 12:07:36 +05:30
redirect_to(current_user ? dashboard_snippets_path : explore_snippets_path)
2014-09-02 18:07:02 +05:30
end
end
def new
@snippet = PersonalSnippet.new
end
def create
2017-08-17 22:00:37 +05:30
create_params = snippet_params.merge(spammable_params)
2020-03-09 13:42:32 +05:30
service_response = Snippets::CreateService.new(nil, current_user, create_params).execute
@snippet = service_response.payload[:snippet]
2020-04-08 14:13:33 +05:30
repository_operation_error = service_response.error? && !@snippet.persisted? && @snippet.valid?
2014-09-02 18:07:02 +05:30
2020-04-08 14:13:33 +05:30
if repository_operation_error
flash.now[:alert] = service_response.message
2017-09-10 17:25:29 +05:30
2020-04-08 14:13:33 +05:30
render :new
else
move_temporary_files if @snippet.valid? && params[:files]
recaptcha_check_with_fallback { render :new }
end
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-09 13:42:32 +05:30
service_response = Snippets::UpdateService.new(nil, current_user, update_params).execute(@snippet)
@snippet = service_response.payload[:snippet]
2017-08-17 22:00:37 +05:30
2020-04-08 14:13:33 +05:30
check_repository_error
2014-09-02 18:07:02 +05:30
end
def show
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
2020-04-08 14:13:33 +05:30
@note = Note.new(noteable: @snippet)
@noteable = @snippet
@discussions = @snippet.discussions
@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
2018-10-15 14:42:47 +05:30
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-09 13:42:32 +05:30
service_response = Snippets::DestroyService.new(current_user, @snippet).execute
2014-09-02 18:07:02 +05:30
2020-03-09 13:42:32 +05:30
if service_response.success?
redirect_to dashboard_snippets_path, status: :found
elsif service_response.http_status == 403
access_denied!
else
redirect_to snippet_path(@snippet),
status: :found,
alert: service_response.message
end
2014-09-02 18:07:02 +05:30
end
protected
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
def snippet
2018-11-18 11:00:15 +05:30
@snippet ||= PersonalSnippet.inc_relations_for_view.find_by(id: params[:id])
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2017-08-17 22:00:37 +05:30
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
2020-04-08 14:13:33 +05:30
def blob
return unless snippet
@blob ||= if Feature.enabled?(:version_snippets, current_user) && !snippet.repository.empty?
snippet.blobs.first
else
snippet.blob
end
end
2017-09-10 17:25:29 +05:30
def spammable_path
snippet_path(@snippet)
end
2015-11-26 14:37:03 +05:30
def authorize_read_snippet!
2020-03-09 13:42:32 +05:30
return if can?(current_user, :read_snippet, @snippet)
2017-08-17 22:00:37 +05:30
if current_user
render_404
else
authenticate_user!
end
2015-11-26 14:37:03 +05:30
end
2015-09-11 14:41:01 +05:30
def authorize_update_snippet!
2020-03-09 13:42:32 +05:30
return render_404 unless can?(current_user, :update_snippet, @snippet)
2014-09-02 18:07:02 +05:30
end
def authorize_admin_snippet!
2020-03-09 13:42:32 +05:30
return render_404 unless can?(current_user, :admin_snippet, @snippet)
2014-09-02 18:07:02 +05:30
end
2020-01-01 13:55:28 +05:30
def authorize_create_snippet!
2020-03-09 13:42:32 +05:30
return render_404 unless can?(current_user, :create_snippet)
2020-01-01 13:55:28 +05:30
end
2014-09-02 18:07:02 +05:30
def snippet_params
2017-09-10 17:25:29 +05:30
params.require(:personal_snippet).permit(:title, :content, :file_name, :private, :visibility_level, :description)
end
def move_temporary_files
params[:files].each do |file|
2019-07-07 11:18:12 +05:30
FileMover.new(file, from_model: current_user, to_model: @snippet).execute
2017-09-10 17:25:29 +05:30
end
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
end