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.

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