Commit 160bd862 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Refactoring for EditTree and NewTree controllers and contexts

Signed-off-by: default avatarDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
parent f61850e6
module Files
class UpdateContext < BaseContext
def execute
allowed = if project.protected_branch?(ref)
can?(current_user, :push_code_to_protected_branches, project)
else
can?(current_user, :push_code, project)
end
unless allowed
return error("You are not allowed to push into this branch")
end
unless repository.branch_names.include?(ref)
return error("You can only create files if you are on top of a branch")
end
blob = repository.blob_at(ref, path)
unless blob
return error("You can only edit text files")
end
new_file_action = Gitlab::Satellite::EditFileAction.new(current_user, project, ref, path)
created_successfully = new_file_action.commit!(
params[:content],
params[:commit_message],
params[:last_commit]
)
if created_successfully
success
else
error("Your changes could not be commited, because the file has been changed")
end
end
end
end
......@@ -23,4 +23,10 @@ class Projects::ApplicationController < ApplicationController
'public_projects'
end
end
def require_branch_head
unless @repository.branch_names.include?(@ref)
redirect_to project_tree_path(@project, @ref), notice: "This action is not allowed unless you are on top of a branch"
end
end
end
class Projects::BaseTreeController < Projects::ApplicationController
include ExtractsPath
before_filter :authorize_read_project!
before_filter :authorize_code_access!
before_filter :require_non_empty_project
end
# Controller for edit a repository's file
class Projects::EditTreeController < Projects::ApplicationController
include ExtractsPath
# Authorize
before_filter :authorize_read_project!
before_filter :authorize_code_access!
before_filter :require_non_empty_project
before_filter :edit_requirements, only: [:show, :update]
class Projects::EditTreeController < Projects::BaseTreeController
before_filter :require_branch_head
before_filter :blob
def show
@last_commit = Gitlab::Git::Commit.last_for_path(@repository, @ref, @path).sha
end
def update
edit_file_action = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref, @path)
updated_successfully = edit_file_action.commit!(
params[:content],
params[:commit_message],
params[:last_commit]
)
result = Files::UpdateContext.new(@project, current_user, params, @ref, @path).execute
if updated_successfully
redirect_to project_blob_path(@project, @id), notice: "Your changes have been successfully commited"
if result[:status] == :success
flash[:notice] = "Your changes have been successfully commited"
redirect_to project_blob_path(@project, @id)
else
flash[:notice] = "Your changes could not be commited, because the file has been changed"
flash[:alert] = result[:error]
render :show
end
end
private
def edit_requirements
@blob = @repository.blob_at(@commit.id, @path)
unless @blob
redirect_to project_blob_path(@project, @id), notice: "You can only edit text files"
end
allowed = if project.protected_branch? @ref
can?(current_user, :push_code_to_protected_branches, project)
else
can?(current_user, :push_code, project)
end
return access_denied! unless allowed
unless @repository.branch_names.include?(@ref)
redirect_to project_blob_path(@project, @id), notice: "You can only edit this file if you are on top of a branch"
end
def blob
@blob ||= @repository.blob_at(@commit.id, @path)
end
end
class Projects::NewTreeController < Projects::ApplicationController
include ExtractsPath
# Authorize
before_filter :authorize_read_project!
before_filter :authorize_code_access!
before_filter :require_non_empty_project
class Projects::NewTreeController < Projects::BaseTreeController
before_filter :require_branch_head
def show
end
......
# Controller for viewing a repository's file structure
class Projects::TreeController < Projects::ApplicationController
include ExtractsPath
# Authorize
before_filter :authorize_read_project!
before_filter :authorize_code_access!
before_filter :require_non_empty_project
class Projects::TreeController < Projects::BaseTreeController
def show
return not_found! if tree.entries.empty?
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment