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

8
  rescue_from Gitlab::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
  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

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

randx's avatar
randx committed
35
36
37
38
39
40
41
42
43
44
  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
45
46
47
48
49
50
51
52
  def layout_by_resource
    if devise_controller?
      "devise"
    else
      "application"
    end
  end

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

Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
57
58
59
60
61
62
63
64
  def abilities
    @abilities ||= Six.new
  end

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

Nihad Abbasov's avatar
Nihad Abbasov committed
65
  def project
66
67
    @project ||= current_user.projects.find_by_code(params[:project_id])
    @project || render_404
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
68
69
70
71
72
73
74
  end

  def add_project_abilities
    abilities << Ability
  end

  def authenticate_admin!
gitlabhq's avatar
gitlabhq committed
75
    return render_404 unless current_user.is_admin?
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
76
77
78
  end

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

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

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

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

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

106
  def load_refs
107
    if params[:ref].blank?
gitlabhq's avatar
gitlabhq committed
108
109
      @branch = params[:branch].blank? ? nil : params[:branch]
      @tag = params[:tag].blank? ? nil : params[:tag]
110
      @ref = @branch || @tag || @project.try(:default_branch) || Repository.default_ref
111
112
    else
      @ref = params[:ref]
gitlabhq's avatar
gitlabhq committed
113
    end
114
115
116
117
  end

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

  def require_non_empty_project
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
121
    redirect_to @project unless @project.repo_exists? && @project.has_commits?
gitlabhq's avatar
gitlabhq committed
122
  end
123

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
124
125
126
127
128
  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
129
130
131
132

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