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.

152 lines
4.0 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 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 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.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. cached_keys_with_value = Rails.cache.read_multi(*raw).transform_keys(&:id)
  80. uncached_ids = raw.map(&:id) - cached_keys_with_value.keys
  81. klass.reload_stale_associations!(cached_keys_with_value.values) if klass.respond_to?(:reload_stale_associations!)
  82. unless uncached_ids.empty?
  83. uncached = klass.where(id: uncached_ids).with_includes.map { |item| [item.id, item] }.to_h
  84. uncached.each_value do |item|
  85. Rails.cache.write(item, item)
  86. end
  87. end
  88. raw.map { |item| cached_keys_with_value[item.id] || uncached[item.id] }.compact
  89. end
  90. def respond_with_error(code)
  91. respond_to do |format|
  92. format.any { head code }
  93. format.html do
  94. set_locale
  95. render "errors/#{code}", layout: 'error', status: code
  96. end
  97. end
  98. end
  99. def render_cached_json(cache_key, **options)
  100. options[:expires_in] ||= 3.minutes
  101. cache_public = options.key?(:public) ? options.delete(:public) : true
  102. content_type = options.delete(:content_type) || 'application/json'
  103. data = Rails.cache.fetch(cache_key, { raw: true }.merge(options)) do
  104. yield.to_json
  105. end
  106. expires_in options[:expires_in], public: cache_public
  107. render json: data, content_type: content_type
  108. end
  109. def set_cache_headers
  110. response.headers['Vary'] = 'Accept'
  111. end
  112. def skip_session!
  113. request.session_options[:skip] = true
  114. end
  115. end