application_controller.rb 2.32 KB
Newer Older
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
1
2
class ApplicationController < ActionController::Base
  before_filter :authenticate_user!
Valery Sizov's avatar
Valery Sizov committed
3
  before_filter :set_current_user_for_mailer
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
4
5
6
  protect_from_forgery
  helper_method :abilities, :can?

7
8
  rescue_from Gitlabhq::Gitolite::AccessDenied do |exception|
    render :file => File.join(Rails.root, "public", "githost_error"), :layout => false
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
9
10
  end

gitlabhq's avatar
gitlabhq committed
11
12
  layout :layout_by_resource

Nihad Abbasov's avatar
Nihad Abbasov committed
13
  protected
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
14

gitlabhq's avatar
gitlabhq committed
15
16
17
18
19
20
21
22
  def layout_by_resource
    if devise_controller?
      "devise"
    else
      "application"
    end
  end

Valery Sizov's avatar
Valery Sizov committed
23
24
25
26
  def set_current_user_for_mailer
    MailerObserver.current_user = current_user
  end

Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
27
28
29
30
31
32
33
34
  def abilities
    @abilities ||= Six.new
  end

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

Nihad Abbasov's avatar
Nihad Abbasov committed
35
  def project
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
36
37
38
39
40
41
42
43
    @project ||= Project.find_by_code(params[:project_id])
  end

  def add_project_abilities
    abilities << Ability
  end

  def authenticate_admin!
gitlabhq's avatar
gitlabhq committed
44
    return render_404 unless current_user.is_admin?
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
45
46
47
  end

  def authorize_project!(action)
gitlabhq's avatar
gitlabhq committed
48
49
50
51
52
    return render_404 unless can?(current_user, action, project)
  end

  def access_denied!
    render_404
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
53
54
55
56
57
58
59
60
61
  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
62

63
  def load_refs
gitlabhq's avatar
gitlabhq committed
64
65
66
67
68
    unless params[:ref].blank?
      @ref = params[:ref]
    else
      @branch = params[:branch].blank? ? nil : params[:branch]
      @tag = params[:tag].blank? ? nil : params[:tag]
69
      @ref = @branch || @tag || @project.try(:default_branch) || Repository.default_ref
gitlabhq's avatar
gitlabhq committed
70
    end
71
72
73
74
  end

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

  def require_non_empty_project
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
78
    redirect_to @project unless @project.repo_exists? && @project.has_commits?
gitlabhq's avatar
gitlabhq committed
79
  end
80

81
82
83
84
85
86
  def respond_with_notes
    if params[:last_id] && params[:first_id]
      @notes = @notes.where("id >= ?", params[:first_id])
    elsif params[:last_id]
      @notes = @notes.where("id > ?", params[:last_id])
    elsif params[:first_id]
87
88
      @notes = @notes.where("id < ?", params[:first_id])
    else
89
90
91
      nil
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
92
93
94
95
96
97

  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
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
98
end