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.

129 lines
3.3 KiB

8 years ago
8 years ago
8 years ago
8 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. helper_method :current_account
  10. helper_method :current_session
  11. helper_method :current_theme
  12. helper_method :theme_data
  13. helper_method :single_user_mode?
  14. rescue_from ActionController::RoutingError, with: :not_found
  15. rescue_from ActiveRecord::RecordNotFound, with: :not_found
  16. rescue_from ActionController::InvalidAuthenticityToken, with: :unprocessable_entity
  17. rescue_from Mastodon::NotPermittedError, with: :forbidden
  18. before_action :store_current_location, except: :raise_not_found, unless: :devise_controller?
  19. before_action :check_suspension, if: :user_signed_in?
  20. def raise_not_found
  21. raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}"
  22. end
  23. private
  24. def https_enabled?
  25. Rails.env.production? && ENV['LOCAL_HTTPS'] == 'true'
  26. end
  27. def store_current_location
  28. store_location_for(:user, request.url)
  29. end
  30. def require_admin!
  31. redirect_to root_path unless current_user&.admin?
  32. end
  33. def require_staff!
  34. redirect_to root_path unless current_user&.staff?
  35. end
  36. def check_suspension
  37. forbidden if current_user.account.suspended?
  38. end
  39. def after_sign_out_path_for(_resource_or_scope)
  40. new_user_session_path
  41. end
  42. protected
  43. def forbidden
  44. respond_with_error(403)
  45. end
  46. def not_found
  47. respond_with_error(404)
  48. end
  49. def gone
  50. respond_with_error(410)
  51. end
  52. def unprocessable_entity
  53. respond_with_error(422)
  54. end
  55. def single_user_mode?
  56. @single_user_mode ||= Rails.configuration.x.single_user_mode && Account.exists?
  57. end
  58. def current_account
  59. @current_account ||= current_user.try(:account)
  60. end
  61. def current_session
  62. @current_session ||= SessionActivation.find_by(session_id: cookies.signed['_session_id'])
  63. end
  64. def current_theme
  65. return Setting.default_settings['theme'] unless Themes.instance.names.include? current_user&.setting_theme
  66. current_user.setting_theme
  67. end
  68. def theme_data
  69. Themes.instance.get(current_theme)
  70. end
  71. def cache_collection(raw, klass)
  72. return raw unless klass.respond_to?(:with_includes)
  73. raw = raw.cache_ids.to_a if raw.is_a?(ActiveRecord::Relation)
  74. uncached_ids = []
  75. cached_keys_with_value = Rails.cache.read_multi(*raw.map(&:cache_key))
  76. raw.each do |item|
  77. uncached_ids << item.id unless cached_keys_with_value.key?(item.cache_key)
  78. end
  79. klass.reload_stale_associations!(cached_keys_with_value.values) if klass.respond_to?(:reload_stale_associations!)
  80. unless uncached_ids.empty?
  81. uncached = klass.where(id: uncached_ids).with_includes.map { |item| [item.id, item] }.to_h
  82. uncached.each_value do |item|
  83. Rails.cache.write(item.cache_key, item)
  84. end
  85. end
  86. raw.map { |item| cached_keys_with_value[item.cache_key] || uncached[item.id] }.compact
  87. end
  88. def respond_with_error(code)
  89. respond_to do |format|
  90. format.any { head code }
  91. format.html do
  92. set_locale
  93. render "errors/#{code}", layout: 'error', status: code
  94. end
  95. end
  96. end
  97. end