BigW Consortium Gitlab

tree_controller.rb 1.64 KB
Newer Older
1
# Controller for viewing a repository's file structure
2
class TreeController < ProjectResourceController
3
  include ExtractsPath
4 5 6 7 8 9

  # Authorize
  before_filter :authorize_read_project!
  before_filter :authorize_code_access!
  before_filter :require_non_empty_project

10
  before_filter :assign_ref_vars
11
  before_filter :edit_requirements, only: [:edit, :update]
12 13

  def show
14 15
    @hex_path  = Digest::SHA1.hexdigest(@path)
    @logs_path = logs_file_project_ref_path(@project, @ref, @path)
16

17 18 19 20 21 22
    respond_to do |format|
      format.html
      # Disable cache so browser history works
      format.js { no_cache_headers }
    end
  end
Valeriy Sizov committed
23 24

  def edit
25
    @last_commit = @project.last_commit_for(@ref, @path).sha
Valeriy Sizov committed
26 27 28
  end

  def update
29 30
    edit_file_action = Gitlab::Satellite::EditFileAction.new(current_user, @project, @ref, @path)
    updated_successfully = edit_file_action.commit!(
31 32
      params[:content],
      params[:commit_message],
33 34
      params[:last_commit]
    )
35

36
    if updated_successfully
37
      redirect_to project_tree_path(@project, @id), notice: "Your changes have been successfully commited"
Valeriy Sizov committed
38
    else
39
      flash[:notice] = "Your changes could not be commited, because the file has been changed"
40
      render :edit
Valeriy Sizov committed
41
    end
Valeriy Sizov committed
42
  end
43 44 45 46 47 48 49

  private

  def edit_requirements
    unless @tree.is_blob? && @tree.text?
      redirect_to project_tree_path(@project, @id), notice: "You can only edit text files"
    end
50 51 52 53 54 55 56 57

    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
58
  end
59
end