application_controller.rb 4.2 KB
Newer Older
G
gitlabhq 已提交
1
  before_filter :authenticate_user!
2
  before_filter :reject_blocked!
3
  before_filter :check_password_expiration
4
  before_filter :set_current_user_for_thread
5
  before_filter :add_abilities
R
randx 已提交
6
  before_filter :dev_tools if Rails.env == 'development'
7
  before_filter :default_headers
8
  before_filter :add_gon_variables
9

G
gitlabhq 已提交
10
  protect_from_forgery
11

G
gitlabhq 已提交
12 13
  helper_method :abilities, :can?

14
  rescue_from Encoding::CompatibilityError do |exception|
R
Riyad Preukschas 已提交
15
    log_exception(exception)
C
Cyril 已提交
16
    render "errors/encoding", layout: "errors", status: 500
17 18
  end

19
  rescue_from ActiveRecord::RecordNotFound do |exception|
R
Riyad Preukschas 已提交
20
    log_exception(exception)
C
Cyril 已提交
21
    render "errors/not_found", layout: "errors", status: 404
G
gitlabhq 已提交
22 23
  end

N
Nihad Abbasov 已提交
24
  protected
G
gitlabhq 已提交
25

R
Riyad Preukschas 已提交
26 27 28 29 30 31
  def log_exception(exception)
    application_trace = ActionDispatch::ExceptionWrapper.new(env, exception).application_trace
    application_trace.map!{ |t| "  #{t}\n" }
    logger.error "\n#{exception.class.name} (#{exception.message}):\n#{application_trace.join}"
  end

32
  def reject_blocked!
33
    if current_user && current_user.blocked?
34
      sign_out current_user
35
      flash[:alert] = "Your account is blocked. Retry when an admin has unblocked it."
36 37 38 39
      redirect_to new_user_session_path
    end
  end

R
randx 已提交
40
  def after_sign_in_path_for resource
41
    if resource.is_a?(User) && resource.respond_to?(:blocked?) && resource.blocked?
R
randx 已提交
42
      sign_out resource
43
      flash[:alert] = "Your account is blocked. Retry when an admin has unblocked it."
R
randx 已提交
44 45 46 47 48 49
      new_user_session_path
    else
      super
    end
  end

50 51
  def set_current_user_for_thread
    Thread.current[:current_user] = current_user
52 53
  end

G
gitlabhq 已提交
54 55 56 57 58 59 60 61
  def abilities
    @abilities ||= Six.new
  end

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

N
Nihad Abbasov 已提交
62
  def project
63 64
    id = params[:project_id] || params[:id]

65 66 67 68 69 70
    @project = Project.find_with_namespace(id)

    if @project and can?(current_user, :read_project, @project)
      @project
    else
      @project = nil
D
Dmitriy Zaporozhets 已提交
71
      render_404 and return
72
    end
G
gitlabhq 已提交
73 74
  end

D
Dmitriy Zaporozhets 已提交
75 76 77 78 79 80
  def repository
    @repository ||= project.repository
  rescue Grit::NoSuchPathError
    nil
  end

81
  def add_abilities
G
gitlabhq 已提交
82 83 84 85
    abilities << Ability
  end

  def authorize_project!(action)
86
    return access_denied! unless can?(current_user, action, project)
G
gitlabhq 已提交
87 88
  end

89
  def authorize_code_access!
S
Stephen Lottermoser 已提交
90
    return access_denied! unless can?(current_user, :download_code, project) or project.public?
91 92
  end

93 94 95 96
  def authorize_push!
    return access_denied! unless can?(current_user, :push_code, project)
  end

G
gitlabhq 已提交
97
  def access_denied!
C
Cyril 已提交
98
    render "errors/access_denied", layout: "errors", status: 404
99 100 101
  end

  def not_found!
C
Cyril 已提交
102
    render "errors/not_found", layout: "errors", status: 404
103 104 105
  end

  def git_not_found!
C
Cyril 已提交
106
    render "errors/git_not_found", layout: "errors", status: 404
G
gitlabhq 已提交
107 108 109 110 111 112 113 114 115
  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 已提交
116

117
  def render_404
118
    render file: Rails.root.join("public", "404"), layout: false, status: "404"
G
gitlabhq 已提交
119
  end
G
gitlabhq 已提交
120

121 122 123 124
  def render_403
    render file: Rails.root.join("public", "403"), layout: false, status: "403"
  end

G
gitlabhq 已提交
125
  def require_non_empty_project
126
    redirect_to @project if @project.empty_repo?
G
gitlabhq 已提交
127
  end
D
Dmitriy Zaporozhets 已提交
128

D
Dmitriy Zaporozhets 已提交
129 130 131 132 133
  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 已提交
134

R
randx 已提交
135 136 137
  def dev_tools
    Rack::MiniProfiler.authorize_request
  end
138

139 140 141 142
  def default_headers
    headers['X-Frame-Options'] = 'DENY'
    headers['X-XSS-Protection'] = '1; mode=block'
  end
143 144 145

  def add_gon_variables
    gon.default_issues_tracker = Project.issues_tracker.default_value
146
    gon.api_version = API::API.version
147
    gon.api_token = current_user.private_token if current_user
148
    gon.gravatar_url = request.ssl? || Gitlab.config.gitlab.https ? Gitlab.config.gravatar.ssl_url : Gitlab.config.gravatar.plain_url
149
    gon.relative_url_root = Gitlab.config.gitlab.relative_url_root
150
  end
151 152

  def check_password_expiration
153
    if current_user && current_user.password_expires_at && current_user.password_expires_at < Time.now
154 155 156
      redirect_to new_profile_password_path and return
    end
  end
G
gitlabhq 已提交
157
end