You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

165 lines
4.4 KiB

8 years ago
7 years ago
7 years ago
7 years ago
8 years ago
  1. # frozen_string_literal: true
  2. class ApplicationController < ActionController::Base
  3. # Prevent CSRF attacks by raising an exception.
  4. # For APIs, you may want to use :null_session instead.
  5. protect_from_forgery with: :exception
  6. force_ssl if: :https_enabled?
  7. include Localized
  8. include UserTrackingConcern
  9. include SessionTrackingConcern
  10. helper_method :current_account
  11. helper_method :current_session
  12. helper_method :current_theme
  13. helper_method :single_user_mode?
  14. helper_method :use_seamless_external_login?
  15. rescue_from ActionController::RoutingError, with: :not_found
  16. rescue_from ActiveRecord::RecordNotFound, with: :not_found
  17. rescue_from ActionController::InvalidAuthenticityToken, with: :unprocessable_entity
  18. rescue_from ActionController::UnknownFormat, with: :not_acceptable
  19. rescue_from Mastodon::NotPermittedError, with: :forbidden
  20. before_action :store_current_location, except: :raise_not_found, unless: :devise_controller?
  21. before_action :check_user_permissions, if: :user_signed_in?
  22. def raise_not_found
  23. raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}"
  24. end
  25. private
  26. def https_enabled?
  27. Rails.env.production?
  28. end
  29. def authorized_fetch_mode?
  30. ENV['AUTHORIZED_FETCH'] == 'true'
  31. end
  32. def public_fetch_mode?
  33. !authorized_fetch_mode?
  34. end
  35. def store_current_location
  36. store_location_for(:user, request.url) unless request.format == :json
  37. end
  38. def require_admin!
  39. forbidden unless current_user&.admin?
  40. end
  41. def require_staff!
  42. forbidden unless current_user&.staff?
  43. end
  44. def check_user_permissions
  45. forbidden if current_user.disabled? || current_user.account.suspended?
  46. end
  47. def after_sign_out_path_for(_resource_or_scope)
  48. new_user_session_path
  49. end
  50. protected
  51. def truthy_param?(key)
  52. ActiveModel::Type::Boolean.new.cast(params[key])
  53. end
  54. def forbidden
  55. respond_with_error(403)
  56. end
  57. def not_found
  58. respond_with_error(404)
  59. end
  60. def gone
  61. respond_with_error(410)
  62. end
  63. def unprocessable_entity
  64. respond_with_error(422)
  65. end
  66. def not_acceptable
  67. respond_with_error(406)
  68. end
  69. def single_user_mode?
  70. @single_user_mode ||= Rails.configuration.x.single_user_mode && Account.where('id > 0').exists?
  71. end
  72. def use_seamless_external_login?
  73. Devise.pam_authentication || Devise.ldap_authentication
  74. end
  75. def current_account
  76. return @current_account if defined?(@current_account)
  77. @current_account = current_user&.account
  78. end
  79. def current_session
  80. return @current_session if defined?(@current_session)
  81. @current_session = SessionActivation.find_by(session_id: cookies.signed['_session_id']) if cookies.signed['_session_id'].present?
  82. end
  83. def current_theme
  84. return Setting.theme unless Themes.instance.names.include? current_user&.setting_theme
  85. current_user.setting_theme
  86. end
  87. def cache_collection(raw, klass)
  88. return raw unless klass.respond_to?(:with_includes)
  89. raw = raw.cache_ids.to_a if raw.is_a?(ActiveRecord::Relation)
  90. cached_keys_with_value = Rails.cache.read_multi(*raw).transform_keys(&:id)
  91. uncached_ids = raw.map(&:id) - cached_keys_with_value.keys
  92. klass.reload_stale_associations!(cached_keys_with_value.values) if klass.respond_to?(:reload_stale_associations!)
  93. unless uncached_ids.empty?
  94. uncached = klass.where(id: uncached_ids).with_includes.each_with_object({}) { |item, h| h[item.id] = item }
  95. uncached.each_value do |item|
  96. Rails.cache.write(item, item)
  97. end
  98. end
  99. raw.map { |item| cached_keys_with_value[item.id] || uncached[item.id] }.compact
  100. end
  101. def respond_with_error(code)
  102. respond_to do |format|
  103. format.any { head code }
  104. format.html do
  105. set_locale
  106. render "errors/#{code}", layout: 'error', status: code
  107. end
  108. end
  109. end
  110. def render_cached_json(cache_key, **options)
  111. options[:expires_in] ||= 3.minutes
  112. cache_public = options.key?(:public) ? options.delete(:public) : true
  113. content_type = options.delete(:content_type) || 'application/json'
  114. data = Rails.cache.fetch(cache_key, { raw: true }.merge(options)) do
  115. yield.to_json
  116. end
  117. expires_in options[:expires_in], public: cache_public
  118. render json: data, content_type: content_type
  119. end
  120. def set_cache_headers
  121. response.headers['Vary'] = public_fetch_mode? ? 'Accept' : 'Accept, Signature'
  122. end
  123. end