application_controller.rb 3.35 KB
Newer Older
gitlabhq's avatar
gitlabhq committed
1
2
class ApplicationController < ActionController::Base
  before_filter :authenticate_user!
3
  before_filter :reject_blocked!
4
  before_filter :set_current_user_for_observers
5
  before_filter :add_abilities
randx's avatar
randx committed
6
  before_filter :dev_tools if Rails.env == 'development'
7

gitlabhq's avatar
gitlabhq committed
8
  protect_from_forgery
9

gitlabhq's avatar
gitlabhq committed
10
11
  helper_method :abilities, :can?

12
  rescue_from Gitlab::Gitolite::AccessDenied do |exception|
Riyad Preukschas's avatar
Riyad Preukschas committed
13
    log_exception(exception)
14
    render "errors/gitolite", layout: "errors", status: 500
15
16
  end

17
  rescue_from Encoding::CompatibilityError do |exception|
Riyad Preukschas's avatar
Riyad Preukschas committed
18
    log_exception(exception)
19
    render "errors/encoding", layout: "errors", status: 500
20
21
  end

22
  rescue_from ActiveRecord::RecordNotFound do |exception|
Riyad Preukschas's avatar
Riyad Preukschas committed
23
    log_exception(exception)
24
    render "errors/not_found", layout: "errors", status: 404
gitlabhq's avatar
gitlabhq committed
25
26
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
27
  protected
gitlabhq's avatar
gitlabhq committed
28

Riyad Preukschas's avatar
Riyad Preukschas committed
29
30
31
32
33
34
  def log_exception(exception)
    application_trace = ActionDispatch::ExceptionWrapper.new(env, exception).application_trace
    application_trace.map!{ |t| "  #{t}\n" }
    logger.error "\n#{exception.class.name} (#{exception.message}):\n#{application_trace.join}"
  end

35
36
  def reject_blocked!
    if current_user && current_user.blocked
37
      sign_out current_user
38
      flash[:alert] = "Your account is blocked. Retry when an admin unblock it."
39
40
41
42
      redirect_to new_user_session_path
    end
  end

randx's avatar
randx committed
43
44
45
  def after_sign_in_path_for resource
    if resource.is_a?(User) && resource.respond_to?(:blocked) && resource.blocked
      sign_out resource
46
      flash[:alert] = "Your account is blocked. Retry when an admin unblock it."
randx's avatar
randx committed
47
48
49
50
51
52
      new_user_session_path
    else
      super
    end
  end

53
  def set_current_user_for_observers
54
    MergeRequestObserver.current_user = current_user
55
56
57
    IssueObserver.current_user = current_user
  end

gitlabhq's avatar
gitlabhq committed
58
59
60
61
62
63
64
65
  def abilities
    @abilities ||= Six.new
  end

  def can?(object, action, subject)
    abilities.allowed?(object, action, subject)
  end

Nihad Abbasov's avatar
Nihad Abbasov committed
66
  def project
67
68
    id = params[:project_id] || params[:id]

69
70
71
72
73
74
75
76
    @project = Project.find_with_namespace(id)

    if @project and can?(current_user, :read_project, @project)
      @project
    else
      @project = nil
      render_404
    end
gitlabhq's avatar
gitlabhq committed
77
78
  end

79
  def add_abilities
gitlabhq's avatar
gitlabhq committed
80
81
82
83
    abilities << Ability
  end

  def authorize_project!(action)
84
    return access_denied! unless can?(current_user, action, project)
gitlabhq's avatar
gitlabhq committed
85
86
  end

87
  def authorize_code_access!
88
    return access_denied! unless can?(current_user, :download_code, project)
89
90
  end

gitlabhq's avatar
gitlabhq committed
91
  def access_denied!
92
    render "errors/access_denied", layout: "errors", status: 404
93
94
95
  end

  def not_found!
96
    render "errors/not_found", layout: "errors", status: 404
97
98
99
  end

  def git_not_found!
100
    render "errors/git_not_found", layout: "errors", status: 404
gitlabhq's avatar
gitlabhq committed
101
102
103
104
105
106
107
108
109
  end

  def method_missing(method_sym, *arguments, &block)
    if method_sym.to_s =~ /^authorize_(.*)!$/
      authorize_project!($1.to_sym)
    else
      super
    end
  end
gitlabhq's avatar
gitlabhq committed
110

111
  def render_404
112
    render file: Rails.root.join("public", "404"), layout: false, status: "404"
gitlabhq's avatar
gitlabhq committed
113
  end
gitlabhq's avatar
gitlabhq committed
114

115
116
117
118
  def render_403
    render file: Rails.root.join("public", "403"), layout: false, status: "403"
  end

gitlabhq's avatar
gitlabhq committed
119
  def require_non_empty_project
120
    redirect_to @project if @project.empty_repo?
gitlabhq's avatar
gitlabhq committed
121
  end
122

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
123
124
125
126
127
  def no_cache_headers
    response.headers["Cache-Control"] = "no-cache, no-store, max-age=0, must-revalidate"
    response.headers["Pragma"] = "no-cache"
    response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
  end
128

randx's avatar
randx committed
129
130
131
  def dev_tools
    Rack::MiniProfiler.authorize_request
  end
gitlabhq's avatar
gitlabhq committed
132
end