application_controller.rb 2.79 KB
Newer Older
1
class Projects::ApplicationController < ApplicationController
2 3
  include RoutableActions

4
  skip_before_action :authenticate_user!
Douwe Maan's avatar
Douwe Maan committed
5 6
  before_action :project
  before_action :repository
7
  layout 'project'
8

9
  helper_method :repository, :can_collaborate_with_project?, :user_access
10 11 12

  private

13
  def project
Michael Kozono's avatar
Michael Kozono committed
14
    return @project if @project
15
    return nil unless params[:project_id] || params[:id]
Michael Kozono's avatar
Michael Kozono committed
16 17 18

    path = File.join(params[:namespace_id], params[:project_id] || params[:id])
    auth_proc = ->(project) { !project.pending_delete? }
19

Michael Kozono's avatar
Michael Kozono committed
20
    @project = find_routable!(Project, path, extra_authorization_proc: auth_proc)
21 22
  end

23 24 25 26 27 28 29
  def build_canonical_path(project)
    params[:namespace_id] = project.namespace.to_param
    params[:project_id] = project.to_param

    url_for(params)
  end

30 31 32 33
  def repository
    @repository ||= project.repository
  end

34
  def can_collaborate_with_project?(project = nil, ref: nil)
35 36
    project ||= @project

37
    can_create_merge_request =
38
      can?(current_user, :create_merge_request_in, project) &&
39 40
      current_user.already_forked?(project)

41
    can?(current_user, :push_code, project) ||
42
      can_create_merge_request ||
43
      user_access(project).can_push_to_branch?(ref)
44 45
  end

46 47 48 49
  def authorize_action!(action)
    unless can?(current_user, action, project)
      return access_denied!
    end
50 51
  end

52
  def check_project_feature_available!(feature)
53 54 55 56 57 58
    render_404 unless project.feature_available?(feature, current_user)
  end

  def check_issuables_available!
    render_404 unless project.feature_available?(:issues, current_user) ||
        project.feature_available?(:merge_requests, current_user)
59 60
  end

61
  def method_missing(method_sym, *arguments, &block)
62 63
    case method_sym.to_s
    when /\Aauthorize_(.*)!\z/
64
      authorize_action!($1.to_sym)
65 66
    when /\Acheck_(.*)_available!\z/
      check_project_feature_available!($1.to_sym)
67 68
    else
      super
69
    end
70
  end
71

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

78
  def require_branch_head
79
    unless @repository.branch_exists?(@ref)
Vinnie Okada's avatar
Vinnie Okada committed
80
      redirect_to(
81
        project_tree_path(@project, @ref),
Douwe Maan's avatar
Douwe Maan committed
82
        notice: "This action is not allowed unless you are on a branch"
Vinnie Okada's avatar
Vinnie Okada committed
83
      )
84 85
    end
  end
86

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

91
  def require_pages_enabled!
92
    not_found unless @project.pages_available?
93
  end
94 95 96 97

  def check_issues_available!
    return render_404 unless @project.feature_available?(:issues, current_user)
  end
98 99 100 101 102

  def user_access(project)
    @user_access ||= {}
    @user_access[project] ||= Gitlab::UserAccess.new(current_user, project: project)
  end
103
end