application_controller.rb 2.9 KB
Newer Older
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
1
2
class ApplicationController < ActionController::Base
  before_filter :authenticate_user!
3
  before_filter :reject_blocked!
4
5
  before_filter :set_current_user_for_mailer
  before_filter :set_current_user_for_observers
randx's avatar
randx committed
6
  before_filter :dev_tools if Rails.env == 'development'
7

Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
8
  protect_from_forgery
9

Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
10
11
  helper_method :abilities, :can?

12
  rescue_from Gitlab::Gitolite::AccessDenied do |exception|
13
    render "errors/gitolite", layout: "error", status: 500
14
15
  end

16
  rescue_from Encoding::CompatibilityError do |exception|
17
    render "errors/encoding", layout: "error", status: 500
18
19
  end

20
  rescue_from ActiveRecord::RecordNotFound do |exception|
21
    render "errors/not_found", layout: "error", status: 404
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
22
23
  end

gitlabhq's avatar
gitlabhq committed
24
25
  layout :layout_by_resource

Nihad Abbasov's avatar
Nihad Abbasov committed
26
  protected
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
27

28
29
  def reject_blocked!
    if current_user && current_user.blocked
30
      sign_out current_user
31
32
33
34
35
      flash[:alert] = "Your account was blocked"
      redirect_to new_user_session_path
    end
  end

randx's avatar
randx committed
36
37
38
39
40
41
42
43
44
45
  def after_sign_in_path_for resource
    if resource.is_a?(User) && resource.respond_to?(:blocked) && resource.blocked
      sign_out resource
      flash[:alert] = "Your account was blocked"
      new_user_session_path
    else
      super
    end
  end

gitlabhq's avatar
gitlabhq committed
46
47
  def layout_by_resource
    if devise_controller?
Nihad Abbasov's avatar
Nihad Abbasov committed
48
      "devise_layout"
gitlabhq's avatar
gitlabhq committed
49
50
51
52
53
    else
      "application"
    end
  end

Valery Sizov's avatar
Valery Sizov committed
54
55
56
57
  def set_current_user_for_mailer
    MailerObserver.current_user = current_user
  end

58
59
60
61
  def set_current_user_for_observers
    IssueObserver.current_user = current_user
  end

Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
62
63
64
65
66
67
68
69
  def abilities
    @abilities ||= Six.new
  end

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

Nihad Abbasov's avatar
Nihad Abbasov committed
70
  def project
71
72
    @project ||= current_user.projects.find_by_code(params[:project_id])
    @project || render_404
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
73
74
75
76
77
78
79
  end

  def add_project_abilities
    abilities << Ability
  end

  def authorize_project!(action)
80
    return access_denied! unless can?(current_user, action, project)
gitlabhq's avatar
gitlabhq committed
81
82
  end

83
  def authorize_code_access!
84
    return access_denied! unless can?(current_user, :download_code, project)
85
86
  end

gitlabhq's avatar
gitlabhq committed
87
  def access_denied!
88
    render "errors/access_denied", layout: "error", status: 404
89
90
91
  end

  def not_found!
92
    render "errors/not_found", layout: "error", status: 404
93
94
95
  end

  def git_not_found!
96
    render "errors/git_not_found", layout: "error", status: 404
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
97
98
99
100
101
102
103
104
105
  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
106

107
  def render_404
108
    render file: Rails.root.join("public", "404"), layout: false, status: "404"
gitlabhq's avatar
gitlabhq committed
109
  end
gitlabhq's avatar
gitlabhq committed
110
111

  def require_non_empty_project
112
    redirect_to @project if @project.empty_repo?
gitlabhq's avatar
gitlabhq committed
113
  end
114

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
115
116
117
118
119
  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
120

randx's avatar
randx committed
121
122
123
  def dev_tools
    Rack::MiniProfiler.authorize_request
  end
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
124
end