gitlab-org--gitlab-foss/app/controllers/projects/edit_tree_controller.rb
Johannes Schleifenbaum cc505f550d Check if a file is editied "on" a branch or a commit.
The previous behavior was a "disabled" link, which was still clickable
and linked to the edit file action.

If the 	user accesses a path like "/edit/master/README" show the file
edit form (just like before).

If the user accesses a path like
"/edit/8d5c1f375ce99e5df84e26b0eafbf1448c6a001e/README", redirect the
user to the file detail page with a note, that he cannot edit a file
"on" a commit.

fixes #5482
2013-10-30 11:34:26 +01:00

53 lines
1.6 KiB
Ruby

# 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]
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]
)
if updated_successfully
redirect_to project_blob_path(@project, @id), notice: "Your changes have been successfully commited"
else
flash[:notice] = "Your changes could not be commited, because the file has been changed"
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
end
end