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

153 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 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
2015-11-26 14:37:03 +05:30
# Allow read snippet
2017-08-17 22:00:37 +05:30
before_action :authorize_read_snippet!, only: [:show, :raw]
2015-11-26 14:37:03 +05:30
2014-09-02 18:07:02 +05:30
# Allow modify snippet
2015-09-11 14:41:01 +05:30
before_action :authorize_update_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_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)
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
@snippet = CreateSnippetService.new(nil, current_user, create_params).execute
2014-09-02 18:07:02 +05:30
2017-09-10 17:25:29 +05:30
move_temporary_files if @snippet.valid? && params[:files]
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)
UpdateSnippetService.new(nil, current_user, @snippet, update_params).execute
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
@note = Note.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
respond_to do |format|
format.html do
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
return access_denied! unless can?(current_user, :admin_personal_snippet, @snippet)
@snippet.destroy
2018-11-18 11:00:15 +05:30
redirect_to snippets_path, status: :found
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
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!
2017-08-17 22:00:37 +05:30
return if can?(current_user, :read_personal_snippet, @snippet)
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!
return render_404 unless can?(current_user, :update_personal_snippet, @snippet)
2014-09-02 18:07:02 +05:30
end
def authorize_admin_snippet!
return render_404 unless can?(current_user, :admin_personal_snippet, @snippet)
end
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