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

105 lines
2.6 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
class SnippetsController < ApplicationController
2015-09-11 14:41:01 +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
2015-12-23 02:04:40 +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
2015-09-11 14:41:01 +05:30
skip_before_action :authenticate_user!, only: [:index, :user_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?
@user = User.find_by(username: params[:username])
render_404 and return unless @user
@snippets = SnippetsFinder.new.execute(current_user, {
filter: :by_user,
user: @user,
scope: params[:scope] }).
page(params[:page]).per(PER_PAGE)
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
2015-04-26 12:48:37 +05:30
@snippet = CreateSnippetService.new(nil, current_user,
snippet_params).execute
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
respond_with @snippet.becomes(Snippet)
2014-09-02 18:07:02 +05:30
end
def edit
end
def update
2015-04-26 12:48:37 +05:30
UpdateSnippetService.new(nil, current_user, @snippet,
snippet_params).execute
respond_with @snippet.becomes(Snippet)
2014-09-02 18:07:02 +05:30
end
def show
end
def destroy
return access_denied! unless can?(current_user, :admin_personal_snippet, @snippet)
@snippet.destroy
redirect_to snippets_path
end
def raw
send_data(
@snippet.content,
2015-04-26 12:48:37 +05:30
type: 'text/plain; charset=utf-8',
2014-09-02 18:07:02 +05:30
disposition: 'inline',
2015-04-26 12:48:37 +05:30
filename: @snippet.sanitized_file_name
2014-09-02 18:07:02 +05:30
)
end
protected
def snippet
2015-04-26 12:48:37 +05:30
@snippet ||= if current_user
PersonalSnippet.where("author_id = ? OR visibility_level IN (?)",
current_user.id,
[Snippet::PUBLIC, Snippet::INTERNAL]).
find(params[:id])
else
2015-11-26 14:37:03 +05:30
PersonalSnippet.find(params[:id])
2015-04-26 12:48:37 +05:30
end
2014-09-02 18:07:02 +05:30
end
2015-11-26 14:37:03 +05:30
def authorize_read_snippet!
authenticate_user! unless can?(current_user, :read_personal_snippet, @snippet)
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
2015-04-26 12:48:37 +05:30
params.require(:personal_snippet).permit(:title, :content, :file_name, :private, :visibility_level)
end
2014-09-02 18:07:02 +05:30
end