application_controller.rb 2.48 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
    return render_404 unless can?(current_user, action, project)
  end

51
52
53
54
  def authorize_code_access!
    return render_404 unless can?(current_user, :download_code, project)
  end

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

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

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

  def require_non_empty_project
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
82
    redirect_to @project unless @project.repo_exists? && @project.has_commits?
gitlabhq's avatar
gitlabhq committed
83
  end
84

85
86
87
88
89
90
  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]
91
92
      @notes = @notes.where("id < ?", params[:first_id])
    else
93
94
95
      nil
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
96
97
98
99
100
101

  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
102
103
104
105

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