application_controller.rb 3.0 KB
Newer Older
G
gitlabhq 已提交
1 2
class ApplicationController < ActionController::Base
  before_filter :authenticate_user!
3
  before_filter :reject_blocked!
V
Valery Sizov 已提交
4
  before_filter :set_current_user_for_mailer
G
gitlabhq 已提交
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
G
gitlabhq 已提交
14 15
  end

G
gitlabhq 已提交
16 17
  layout :layout_by_resource

N
Nihad Abbasov 已提交
18
  protected
G
gitlabhq 已提交
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

R
randx 已提交
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

G
gitlabhq 已提交
38 39 40 41 42 43 44 45
  def layout_by_resource
    if devise_controller?
      "devise"
    else
      "application"
    end
  end

V
Valery Sizov 已提交
46 47 48 49
  def set_current_user_for_mailer
    MailerObserver.current_user = current_user
  end

G
gitlabhq 已提交
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

N
Nihad Abbasov 已提交
58
  def project
59 60
    @project ||= current_user.projects.find_by_code(params[:project_id])
    @project || render_404
G
gitlabhq 已提交
61 62 63 64 65 66 67
  end

  def add_project_abilities
    abilities << Ability
  end

  def authenticate_admin!
G
gitlabhq 已提交
68
    return render_404 unless current_user.is_admin?
G
gitlabhq 已提交
69 70 71
  end

  def authorize_project!(action)
72
    return access_denied! unless can?(current_user, action, project)
G
gitlabhq 已提交
73 74
  end

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

G
gitlabhq 已提交
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
G
gitlabhq 已提交
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
G
gitlabhq 已提交
98

99
  def load_refs
100
    if params[:ref].blank?
G
gitlabhq 已提交
101 102
      @branch = params[:branch].blank? ? nil : params[:branch]
      @tag = params[:tag].blank? ? nil : params[:tag]
103
      @ref = @branch || @tag || @project.try(:default_branch) || Repository.default_ref
104 105
    else
      @ref = params[:ref]
G
gitlabhq 已提交
106
    end
107 108 109 110
  end

  def render_404
    render :file => File.join(Rails.root, "public", "404"), :layout => false, :status => "404"
G
gitlabhq 已提交
111
  end
G
gitlabhq 已提交
112 113

  def require_non_empty_project
D
Dmitriy Zaporozhets 已提交
114
    redirect_to @project unless @project.repo_exists? && @project.has_commits?
G
gitlabhq 已提交
115
  end
D
Dmitriy Zaporozhets 已提交
116

D
Dmitriy Zaporozhets 已提交
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
D
Dmitriy Zaporozhets 已提交
122 123 124 125

  def render_full_content
    @full_content = true
  end
G
gitlabhq 已提交
126
end