application_controller.rb 3.1 KB

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