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.

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