application_controller.rb 3.62 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
15
16
    render "errors/gitolite", :layout => "error"
  end

17
18
19
20
  rescue_from Encoding::CompatibilityError do |exception|
    render "errors/encoding", :layout => "error", :status => 404
  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 authenticate_admin!
gitlabhq's avatar
gitlabhq committed
88
    return render_404 unless current_user.is_admin?
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
89
90
91
  end

  def authorize_project!(action)
92
    return access_denied! unless can?(current_user, action, project)
gitlabhq's avatar
gitlabhq committed
93
94
  end

95
  def authorize_code_access!
96
    return access_denied! unless can?(current_user, :download_code, project)
97
98
  end

gitlabhq's avatar
gitlabhq committed
99
  def access_denied!
100
    render "errors/access_denied", :layout => "error", :status => 404
101
102
103
  end

  def not_found!
104
    render "errors/not_found", :layout => "error", :status => 404
105
106
107
  end

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

119
  def load_refs
120
    if params[:ref].blank?
gitlabhq's avatar
gitlabhq committed
121
122
      @branch = params[:branch].blank? ? nil : params[:branch]
      @tag = params[:tag].blank? ? nil : params[:tag]
123
      @ref = @branch || @tag || @project.try(:default_branch) || Repository.default_ref
124
125
    else
      @ref = params[:ref]
gitlabhq's avatar
gitlabhq committed
126
    end
127
128
129
130
  end

  def render_404
    render :file => File.join(Rails.root, "public", "404"), :layout => false, :status => "404"
gitlabhq's avatar
gitlabhq committed
131
  end
gitlabhq's avatar
gitlabhq committed
132
133

  def require_non_empty_project
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
134
    redirect_to @project unless @project.repo_exists? && @project.has_commits?
gitlabhq's avatar
gitlabhq committed
135
  end
136

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
137
138
139
140
141
  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
142
143
144
145

  def render_full_content
    @full_content = true
  end
randx's avatar
randx committed
146
147
148
149

  def dev_tools
    Rack::MiniProfiler.authorize_request
  end
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
150
end