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

6
7
  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
8
9
  end

gitlabhq's avatar
gitlabhq committed
10
11
  layout :layout_by_resource

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

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

Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
22
23
24
25
26
27
28
29
  def abilities
    @abilities ||= Six.new
  end

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

Nihad Abbasov's avatar
Nihad Abbasov committed
30
  def project
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
31
32
33
34
35
36
37
38
    @project ||= Project.find_by_code(params[:project_id])
  end

  def add_project_abilities
    abilities << Ability
  end

  def authenticate_admin!
gitlabhq's avatar
gitlabhq committed
39
    return render_404 unless current_user.is_admin?
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
40
41
42
  end

  def authorize_project!(action)
gitlabhq's avatar
gitlabhq committed
43
44
45
46
47
    return render_404 unless can?(current_user, action, project)
  end

  def access_denied!
    render_404
Dmitriy Zaporozhets's avatar
v1.0  
Dmitriy Zaporozhets committed
48
49
50
51
52
53
54
55
56
  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
57

58
  def load_refs
gitlabhq's avatar
gitlabhq committed
59
60
61
62
63
    unless params[:ref].blank?
      @ref = params[:ref]
    else
      @branch = params[:branch].blank? ? nil : params[:branch]
      @tag = params[:tag].blank? ? nil : params[:tag]
64
      @ref = @branch || @tag || Repository.default_ref
gitlabhq's avatar
gitlabhq committed
65
    end
66
67
68
69
  end

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

  def require_non_empty_project
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
73
    redirect_to @project unless @project.repo_exists? && @project.has_commits?
gitlabhq's avatar
gitlabhq committed
74
  end
75

76
77
78
79
80
81
  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]
82
83
      @notes = @notes.where("id < ?", params[:first_id])
    else
84
85
86
      nil
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
87
88
89
90
91
92

  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
93
end