BigW Consortium Gitlab

application_controller.rb 4.31 KB
Newer Older
gitlabhq committed
1 2
class ApplicationController < ActionController::Base
  before_filter :authenticate_user!
3
  before_filter :reject_blocked!
4
  before_filter :check_password_expiration
5
  before_filter :set_current_user_for_thread
6
  before_filter :add_abilities
randx committed
7
  before_filter :dev_tools if Rails.env == 'development'
8
  before_filter :default_headers
9
  before_filter :add_gon_variables
10

gitlabhq committed
11
  protect_from_forgery
12

gitlabhq committed
13 14
  helper_method :abilities, :can?

15
  rescue_from Encoding::CompatibilityError do |exception|
Riyad Preukschas committed
16
    log_exception(exception)
17
    render "errors/encoding", layout: "errors", status: 500
18 19
  end

20
  rescue_from ActiveRecord::RecordNotFound do |exception|
Riyad Preukschas committed
21
    log_exception(exception)
22
    render "errors/not_found", layout: "errors", status: 404
gitlabhq committed
23 24
  end

Nihad Abbasov committed
25
  protected
gitlabhq committed
26

Riyad Preukschas committed
27 28 29 30 31 32
  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

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

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

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

gitlabhq committed
55 56 57 58 59 60 61 62
  def abilities
    @abilities ||= Six.new
  end

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

Nihad Abbasov committed
63
  def project
64 65
    id = params[:project_id] || params[:id]

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

    if @project and can?(current_user, :read_project, @project)
      @project
    else
      @project = nil
72
      render_404 and return
73
    end
gitlabhq committed
74 75
  end

76 77 78 79 80 81
  def repository
    @repository ||= project.repository
  rescue Grit::NoSuchPathError
    nil
  end

82
  def add_abilities
gitlabhq committed
83 84 85 86
    abilities << Ability
  end

  def authorize_project!(action)
87
    return access_denied! unless can?(current_user, action, project)
gitlabhq committed
88 89
  end

90
  def authorize_code_access!
91
    return access_denied! unless can?(current_user, :download_code, project) or project.public?
92 93
  end

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

98 99 100 101
  def authorize_create_team!
    return access_denied! unless can?(current_user, :create_team, nil)
  end

gitlabhq committed
102
  def access_denied!
103
    render "errors/access_denied", layout: "errors", status: 404
104 105 106
  end

  def not_found!
107
    render "errors/not_found", layout: "errors", status: 404
108 109 110
  end

  def git_not_found!
111
    render "errors/git_not_found", layout: "errors", status: 404
gitlabhq committed
112 113 114 115 116 117 118 119 120
  end

  def method_missing(method_sym, *arguments, &block)
    if method_sym.to_s =~ /^authorize_(.*)!$/
      authorize_project!($1.to_sym)
    else
      super
    end
  end
gitlabhq committed
121

122
  def render_404
123
    render file: Rails.root.join("public", "404"), layout: false, status: "404"
gitlabhq committed
124
  end
gitlabhq committed
125

126 127 128 129
  def render_403
    render file: Rails.root.join("public", "403"), layout: false, status: "403"
  end

gitlabhq committed
130
  def require_non_empty_project
131
    redirect_to @project if @project.empty_repo?
gitlabhq committed
132
  end
133

134 135 136 137 138
  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
139

randx committed
140 141 142
  def dev_tools
    Rack::MiniProfiler.authorize_request
  end
143

144 145 146 147
  def default_headers
    headers['X-Frame-Options'] = 'DENY'
    headers['X-XSS-Protection'] = '1; mode=block'
  end
148 149 150

  def add_gon_variables
    gon.default_issues_tracker = Project.issues_tracker.default_value
151
    gon.api_version = API::API.version
152
    gon.api_token = current_user.private_token if current_user
153
    gon.gravatar_url = request.ssl? || Gitlab.config.gitlab.https ? Gitlab.config.gravatar.ssl_url : Gitlab.config.gravatar.plain_url
154
    gon.relative_url_root = Gitlab.config.gitlab.relative_url_root
155
  end
156 157

  def check_password_expiration
158
    if current_user && current_user.password_expires_at && current_user.password_expires_at < Time.now
159 160 161
      redirect_to new_profile_password_path and return
    end
  end
gitlabhq committed
162
end