application_controller.rb 3.13 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
6
  before_filter :set_current_user_for_mailer
  before_filter :check_token_auth
  before_filter :set_current_user_for_observers
randx's avatar
randx committed
7
  before_filter :dev_tools if Rails.env == 'development'
8

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

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

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

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

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

gitlabhq's avatar
gitlabhq committed
25
26
  layout :layout_by_resource

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

29
30
31
32
33
34
35
  def check_token_auth
    # Redirect to login page if not atom feed
    if params[:private_token].present? && params[:format] != 'atom'
      redirect_to new_user_session_path
    end
  end

36
37
  def reject_blocked!
    if current_user && current_user.blocked
38
      sign_out current_user
39
40
41
42
43
      flash[:alert] = "Your account was blocked"
      redirect_to new_user_session_path
    end
  end

randx's avatar
randx committed
44
45
46
47
48
49
50
51
52
53
  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
54
55
  def layout_by_resource
    if devise_controller?
Nihad Abbasov's avatar
Nihad Abbasov committed
56
      "devise_layout"
gitlabhq's avatar
gitlabhq committed
57
58
59
60
61
    else
      "application"
    end
  end

Valery Sizov's avatar
Valery Sizov committed
62
63
64
65
  def set_current_user_for_mailer
    MailerObserver.current_user = current_user
  end

66
67
68
69
  def set_current_user_for_observers
    IssueObserver.current_user = current_user
  end

Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
70
71
72
73
74
75
76
77
  def abilities
    @abilities ||= Six.new
  end

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

Nihad Abbasov's avatar
Nihad Abbasov committed
78
  def project
79
80
    @project ||= current_user.projects.find_by_code(params[:project_id])
    @project || render_404
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
81
82
83
84
85
86
87
  end

  def add_project_abilities
    abilities << Ability
  end

  def authorize_project!(action)
88
    return access_denied! unless can?(current_user, action, project)
gitlabhq's avatar
gitlabhq committed
89
90
  end

91
  def authorize_code_access!
92
    return access_denied! unless can?(current_user, :download_code, project)
93
94
  end

gitlabhq's avatar
gitlabhq committed
95
  def access_denied!
96
    render "errors/access_denied", layout: "error", status: 404
97
98
99
  end

  def not_found!
100
    render "errors/not_found", layout: "error", status: 404
101
102
103
  end

  def git_not_found!
104
    render "errors/git_not_found", layout: "error", status: 404
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
105
106
107
108
109
110
111
112
113
  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
114

115
  def render_404
116
    render file: File.join(Rails.root, "public", "404"), layout: false, status: "404"
gitlabhq's avatar
gitlabhq committed
117
  end
gitlabhq's avatar
gitlabhq committed
118
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
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
132
end