BigW Consortium Gitlab

application_controller.rb 2.35 KB
Newer Older
1
class Projects::ApplicationController < ApplicationController
2
  skip_before_action :authenticate_user!
Douwe Maan committed
3 4
  before_action :project
  before_action :repository
5
  layout 'project'
6

7 8 9 10 11 12 13
  helper_method :repository, :can_collaborate_with_project?

  private

  def project
    unless @project
      namespace = params[:namespace_id]
14 15
      id = params[:project_id] || params[:id]

16 17 18 19 20 21 22 23 24 25 26 27 28
      # Redirect from
      #   localhost/group/project.git
      # to
      #   localhost/group/project
      #
      if id =~ /\.git\Z/
        redirect_to request.original_url.gsub(/\.git\/?\Z/, '')
        return
      end

      project_path = "#{namespace}/#{id}"
      @project = Project.find_with_namespace(project_path)

29
      if can?(current_user, :read_project, @project) && !@project.pending_delete?
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        if @project.path_with_namespace != project_path
          redirect_to request.original_url.gsub(project_path, @project.path_with_namespace)
        end
      else
        @project = nil

        if current_user.nil?
          authenticate_user!
        else
          render_404
        end
      end
    end

    @project
  end

  def repository
    @repository ||= project.repository
  end

  def can_collaborate_with_project?(project = nil)
    project ||= @project

    can?(current_user, :push_code, project) ||
      (current_user && current_user.already_forked?(project))
  end

  def authorize_project!(action)
    return access_denied! unless can?(current_user, action, project)
  end

  def method_missing(method_sym, *arguments, &block)
    if method_sym.to_s =~ /\Aauthorize_(.*)!\z/
      authorize_project!($1.to_sym)
    else
      super
67
    end
68
  end
69

70
  def require_non_empty_project
71 72 73
    # Be sure to return status code 303 to avoid a double DELETE:
    # http://api.rubyonrails.org/classes/ActionController/Redirecting.html
    redirect_to namespace_project_path(@project.namespace, @project), status: 303 if @project.empty_repo?
74 75
  end

76
  def require_branch_head
77
    unless @repository.branch_exists?(@ref)
Vinnie Okada committed
78 79
      redirect_to(
        namespace_project_tree_path(@project.namespace, @project, @ref),
80
        notice: "This action is not allowed unless you are on a branch"
Vinnie Okada committed
81
      )
82 83
    end
  end
84

85
  def apply_diff_view_cookie!
86
    cookies.permanent[:diff_view] = params.delete(:view) if params[:view].present?
87 88
  end

89
  def builds_enabled
90
    return render_404 unless @project.builds_enabled?
91
  end
92
end