application_controller.rb 3.01 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!
Valery Sizov's avatar
Valery Sizov committed
4
  before_filter :set_current_user_for_mailer
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
5
6
7
  protect_from_forgery
  helper_method :abilities, :can?

8
  rescue_from Gitlabhq::Gitolite::AccessDenied do |exception|
9
10
11
12
    render "errors/gitolite", :layout => "error"
  end

  rescue_from ActiveRecord::RecordNotFound do |exception|
13
    render "errors/not_found", :layout => "error", :status => 404
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
14
15
  end

gitlabhq's avatar
gitlabhq committed
16
17
  layout :layout_by_resource

Nihad Abbasov's avatar
Nihad Abbasov committed
18
  protected
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
19

20
21
22
23
24
25
26
27
  def reject_blocked!
    if current_user && current_user.blocked
      sign_out current_user 
      flash[:alert] = "Your account was blocked"
      redirect_to new_user_session_path
    end
  end

randx's avatar
randx committed
28
29
30
31
32
33
34
35
36
37
  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
38
39
40
41
42
43
44
45
  def layout_by_resource
    if devise_controller?
      "devise"
    else
      "application"
    end
  end

Valery Sizov's avatar
Valery Sizov committed
46
47
48
49
  def set_current_user_for_mailer
    MailerObserver.current_user = current_user
  end

Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
50
51
52
53
54
55
56
57
  def abilities
    @abilities ||= Six.new
  end

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

Nihad Abbasov's avatar
Nihad Abbasov committed
58
  def project
59
60
    @project ||= current_user.projects.find_by_code(params[:project_id])
    @project || render_404
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
61
62
63
64
65
66
67
  end

  def add_project_abilities
    abilities << Ability
  end

  def authenticate_admin!
gitlabhq's avatar
gitlabhq committed
68
    return render_404 unless current_user.is_admin?
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
69
70
71
  end

  def authorize_project!(action)
72
    return access_denied! unless can?(current_user, action, project)
gitlabhq's avatar
gitlabhq committed
73
74
  end

75
  def authorize_code_access!
76
    return access_denied! unless can?(current_user, :download_code, project)
77
78
  end

gitlabhq's avatar
gitlabhq committed
79
  def access_denied!
80
    render "errors/access_denied", :layout => "error", :status => 404
81
82
83
  end

  def not_found!
84
    render "errors/not_found", :layout => "error", :status => 404
85
86
87
  end

  def git_not_found!
88
    render "errors/git_not_found", :layout => "error", :status => 404
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
89
90
91
92
93
94
95
96
97
  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
98

99
  def load_refs
gitlabhq's avatar
gitlabhq committed
100
101
102
103
104
    unless params[:ref].blank?
      @ref = params[:ref]
    else
      @branch = params[:branch].blank? ? nil : params[:branch]
      @tag = params[:tag].blank? ? nil : params[:tag]
105
      @ref = @branch || @tag || @project.try(:default_branch) || Repository.default_ref
gitlabhq's avatar
gitlabhq committed
106
    end
107
108
109
110
  end

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

  def require_non_empty_project
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
114
    redirect_to @project unless @project.repo_exists? && @project.has_commits?
gitlabhq's avatar
gitlabhq committed
115
  end
116

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
117
118
119
120
121
  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
122
123
124
125

  def render_full_content
    @full_content = true
  end
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
126
end