application_controller.rb 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 store_current_location
  30. store_location_for(:user, request.url) unless request.format == :json
  31. end
  32. def require_admin!
  33. forbidden unless current_user&.admin?
  34. end
  35. def require_staff!
  36. forbidden unless current_user&.staff?
  37. end
  38. def check_user_permissions
  39. forbidden if current_user.disabled? || current_user.account.suspended?
  40. end
  41. def after_sign_out_path_for(_resource_or_scope)
  42. new_user_session_path
  43. end
  44. protected
  45. def truthy_param?(key)
  46. ActiveModel::Type::Boolean.new.cast(params[key])
  47. end
  48. def forbidden
  49. respond_with_error(403)
  50. end
  51. def not_found
  52. respond_with_error(404)
  53. end
  54. def gone
  55. respond_with_error(410)
  56. end
  57. def unprocessable_entity
  58. respond_with_error(422)
  59. end
  60. def not_acceptable
  61. respond_with_error(406)
  62. end
  63. def single_user_mode?
  64. @single_user_mode ||= Rails.configuration.x.single_user_mode && Account.exists?
  65. end
  66. def use_seamless_external_login?
  67. Devise.pam_authentication || Devise.ldap_authentication
  68. end
  69. def current_account
  70. @current_account ||= current_user.try(:account)
  71. end
  72. def current_session
  73. @current_session ||= SessionActivation.find_by(session_id: cookies.signed['_session_id'])
  74. end
  75. def current_theme
  76. return Setting.theme unless Themes.instance.names.include? current_user&.setting_theme
  77. current_user.setting_theme
  78. end
  79. def cache_collection(raw, klass)
  80. return raw unless klass.respond_to?(:with_includes)
  81. raw = raw.cache_ids.to_a if raw.is_a?(ActiveRecord::Relation)
  82. cached_keys_with_value = Rails.cache.read_multi(*raw).transform_keys(&:id)
  83. uncached_ids = raw.map(&:id) - cached_keys_with_value.keys
  84. klass.reload_stale_associations!(cached_keys_with_value.values) if klass.respond_to?(:reload_stale_associations!)
  85. unless uncached_ids.empty?
  86. uncached = klass.where(id: uncached_ids).with_includes.each_with_object({}) { |item, h| h[item.id] = item }
  87. uncached.each_value do |item|
  88. Rails.cache.write(item, item)
  89. end
  90. end
  91. raw.map { |item| cached_keys_with_value[item.id] || uncached[item.id] }.compact
  92. end
  93. def respond_with_error(code)
  94. respond_to do |format|
  95. format.any { head code }
  96. format.html do
  97. set_locale
  98. render "errors/#{code}", layout: 'error', status: code
  99. end
  100. end
  101. end
  102. def render_cached_json(cache_key, **options)
  103. options[:expires_in] ||= 3.minutes
  104. cache_public = options.key?(:public) ? options.delete(:public) : true
  105. content_type = options.delete(:content_type) || 'application/json'
  106. data = Rails.cache.fetch(cache_key, { raw: true }.merge(options)) do
  107. yield.to_json
  108. end
  109. expires_in options[:expires_in], public: cache_public
  110. render json: data, content_type: content_type
  111. end
  112. def set_cache_headers
  113. response.headers['Vary'] = 'Accept'
  114. end
  115. def mark_cacheable!
  116. expires_in 0, public: true
  117. end
  118. end