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

127 lines
3.3 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
# Allow read any snippet
2016-06-02 11:05:42 +05:30
before_action :authorize_read_project_snippet!, except: [:new, :create, :index]
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
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)
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
@snippet = CreateSnippetService.new(@project, current_user, create_params).execute
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(project, 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
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
return access_denied! unless can?(current_user, :admin_project_snippet, @snippet)
@snippet.destroy
2018-11-18 11:00:15 +05:30
redirect_to project_snippets_path(@project), status: :found
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
2016-06-02 11:05:42 +05:30
def authorize_read_project_snippet!
return render_404 unless can?(current_user, :read_project_snippet, @snippet)
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 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