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

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

Douwe Maan's avatar
Douwe Maan committed
9 10 11 12
  helper_method :repository, :can_collaborate_with_project?

  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)
Douwe Maan's avatar
Douwe Maan committed
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

Douwe Maan's avatar
Douwe Maan committed
30 31 32 33 34 35 36 37 38 39 40
  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

41 42 43 44
  def authorize_action!(action)
    unless can?(current_user, action, project)
      return access_denied!
    end
Douwe Maan's avatar
Douwe Maan committed
45 46
  end

47
  def check_project_feature_available!(feature)
48 49 50 51 52 53
    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)
54 55
  end

Douwe Maan's avatar
Douwe Maan committed
56
  def method_missing(method_sym, *arguments, &block)
57 58
    case method_sym.to_s
    when /\Aauthorize_(.*)!\z/
59
      authorize_action!($1.to_sym)
60 61
    when /\Acheck_(.*)_available!\z/
      check_project_feature_available!($1.to_sym)
Douwe Maan's avatar
Douwe Maan committed
62 63
    else
      super
64
    end
Douwe Maan's avatar
Douwe Maan committed
65
  end
66

Douwe Maan's avatar
Douwe Maan committed
67
  def require_non_empty_project
68 69
    # Be sure to return status code 303 to avoid a double DELETE:
    # http://api.rubyonrails.org/classes/ActionController/Redirecting.html
70
    redirect_to project_path(@project), status: 303 if @project.empty_repo?
71 72
  end

73
  def require_branch_head
Paco Guzman's avatar
Paco Guzman committed
74
    unless @repository.branch_exists?(@ref)
Vinnie Okada's avatar
Vinnie Okada committed
75
      redirect_to(
76
        project_tree_path(@project, @ref),
Douwe Maan's avatar
Douwe Maan committed
77
        notice: "This action is not allowed unless you are on a branch"
Vinnie Okada's avatar
Vinnie Okada committed
78
      )
79 80
    end
  end
81

82
  def apply_diff_view_cookie!
83
    cookies.permanent[:diff_view] = params.delete(:view) if params[:view].present?
84 85
  end

86
  def require_pages_enabled!
87
    not_found unless @project.pages_available?
88
  end
89
end