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 ...@@ -23,4 +23,10 @@ class Projects::ApplicationController < ApplicationController
'public_projects' 'public_projects'
end end
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 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::BaseTreeController
class Projects::EditTreeController < Projects::ApplicationController before_filter :require_branch_head
include ExtractsPath before_filter :blob
# Authorize
before_filter :authorize_read_project!
before_filter :authorize_code_access!
before_filter :require_non_empty_project
before_filter :edit_requirements, only: [:show, :update]
def show def show
@last_commit = Gitlab::Git::Commit.last_for_path(@repository, @ref, @path).sha @last_commit = Gitlab::Git::Commit.last_for_path(@repository, @ref, @path).sha
end end
def update def update
edit_file_action = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref, @path) result = Files::UpdateContext.new(@project, current_user, params, @ref, @path).execute
updated_successfully = edit_file_action.commit!(
params[:content],
params[:commit_message],
params[:last_commit]
)
if updated_successfully if result[:status] == :success
redirect_to project_blob_path(@project, @id), notice: "Your changes have been successfully commited" flash[:notice] = "Your changes have been successfully commited"
redirect_to project_blob_path(@project, @id)
else else
flash[:notice] = "Your changes could not be commited, because the file has been changed" flash[:alert] = result[:error]
render :show render :show
end end
end end
private private
def edit_requirements def blob
@blob = @repository.blob_at(@commit.id, @path) @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
end end
end end
class Projects::NewTreeController < Projects::ApplicationController class Projects::NewTreeController < Projects::BaseTreeController
include ExtractsPath before_filter :require_branch_head
# Authorize
before_filter :authorize_read_project!
before_filter :authorize_code_access!
before_filter :require_non_empty_project
def show def show
end end
......
# Controller for viewing a repository's file structure # Controller for viewing a repository's file structure
class Projects::TreeController < Projects::ApplicationController class Projects::TreeController < Projects::BaseTreeController
include ExtractsPath
# Authorize
before_filter :authorize_read_project!
before_filter :authorize_code_access!
before_filter :require_non_empty_project
def show def show
return not_found! if tree.entries.empty? 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