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.

157 lines
4.1 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_suspension, 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_suspension
  39. forbidden if 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 forbidden
  46. respond_with_error(403)
  47. end
  48. def not_found
  49. respond_with_error(404)
  50. end
  51. def gone
  52. respond_with_error(410)
  53. end
  54. def unprocessable_entity
  55. respond_with_error(422)
  56. end
  57. def not_acceptable
  58. respond_with_error(406)
  59. end
  60. def single_user_mode?
  61. @single_user_mode ||= Rails.configuration.x.single_user_mode && Account.exists?
  62. end
  63. def use_seamless_external_login?
  64. Devise.pam_authentication || Devise.ldap_authentication
  65. end
  66. def current_account
  67. @current_account ||= current_user.try(:account)
  68. end
  69. def current_session
  70. @current_session ||= SessionActivation.find_by(session_id: cookies.signed['_session_id'])
  71. end
  72. def current_theme
  73. return Setting.default_settings['theme'] unless Themes.instance.names.include? current_user&.setting_theme
  74. current_user.setting_theme
  75. end
  76. def cache_collection(raw, klass)
  77. return raw unless klass.respond_to?(:with_includes)
  78. raw = raw.cache_ids.to_a if raw.is_a?(ActiveRecord::Relation)
  79. uncached_ids = []
  80. cached_keys_with_value = Rails.cache.read_multi(*raw.map(&:cache_key))
  81. raw.each do |item|
  82. uncached_ids << item.id unless cached_keys_with_value.key?(item.cache_key)
  83. end
  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.map { |item| [item.id, item] }.to_h
  87. uncached.each_value do |item|
  88. Rails.cache.write(item.cache_key, item)
  89. end
  90. end
  91. raw.map { |item| cached_keys_with_value[item.cache_key] || 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_key = cache_key.join(':') if cache_key.is_a?(Enumerable)
  105. cache_public = options.key?(:public) ? options.delete(:public) : true
  106. content_type = options.delete(:content_type) || 'application/json'
  107. data = Rails.cache.fetch(cache_key, { raw: true }.merge(options)) do
  108. yield.to_json
  109. end
  110. expires_in options[:expires_in], public: cache_public
  111. render json: data, content_type: content_type
  112. end
  113. def set_cache_headers
  114. response.headers['Vary'] = 'Accept'
  115. end
  116. def skip_session!
  117. request.session_options[:skip] = true
  118. end
  119. end