application_controller.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. include Localized
  7. include UserTrackingConcern
  8. include SessionTrackingConcern
  9. include CacheConcern
  10. include DomainControlHelper
  11. include DatabaseHelper
  12. include AuthorizedFetchHelper
  13. helper_method :current_account
  14. helper_method :current_session
  15. helper_method :current_theme
  16. helper_method :single_user_mode?
  17. helper_method :use_seamless_external_login?
  18. helper_method :omniauth_only?
  19. helper_method :sso_account_settings
  20. helper_method :limited_federation_mode?
  21. helper_method :body_class_string
  22. helper_method :skip_csrf_meta_tags?
  23. rescue_from ActionController::ParameterMissing, Paperclip::AdapterRegistry::NoHandlerError, with: :bad_request
  24. rescue_from Mastodon::NotPermittedError, with: :forbidden
  25. rescue_from ActionController::RoutingError, ActiveRecord::RecordNotFound, with: :not_found
  26. rescue_from ActionController::UnknownFormat, with: :not_acceptable
  27. rescue_from ActionController::InvalidAuthenticityToken, with: :unprocessable_entity
  28. rescue_from Mastodon::RateLimitExceededError, with: :too_many_requests
  29. rescue_from HTTP::Error, OpenSSL::SSL::SSLError, with: :internal_server_error
  30. rescue_from Mastodon::RaceConditionError, Stoplight::Error::RedLight, ActiveRecord::SerializationFailure, with: :service_unavailable
  31. rescue_from Seahorse::Client::NetworkingError do |e|
  32. Rails.logger.warn "Storage server error: #{e}"
  33. service_unavailable
  34. end
  35. before_action :store_referrer, except: :raise_not_found, if: :devise_controller?
  36. before_action :require_functional!, if: :user_signed_in?
  37. before_action :set_cache_control_defaults
  38. skip_before_action :verify_authenticity_token, only: :raise_not_found
  39. def raise_not_found
  40. raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}"
  41. end
  42. private
  43. def public_fetch_mode?
  44. !authorized_fetch_mode?
  45. end
  46. def store_referrer
  47. return if request.referer.blank?
  48. redirect_uri = URI(request.referer)
  49. return if redirect_uri.path.start_with?('/auth')
  50. stored_url = redirect_uri.to_s if redirect_uri.host == request.host && redirect_uri.port == request.port
  51. store_location_for(:user, stored_url)
  52. end
  53. def require_functional!
  54. redirect_to edit_user_registration_path unless current_user.functional?
  55. end
  56. def skip_csrf_meta_tags?
  57. false
  58. end
  59. def after_sign_out_path_for(_resource_or_scope)
  60. if ENV['OMNIAUTH_ONLY'] == 'true' && ENV['OIDC_ENABLED'] == 'true'
  61. '/auth/auth/openid_connect/logout'
  62. else
  63. new_user_session_path
  64. end
  65. end
  66. protected
  67. def truthy_param?(key)
  68. ActiveModel::Type::Boolean.new.cast(params[key])
  69. end
  70. def forbidden
  71. respond_with_error(403)
  72. end
  73. def not_found
  74. respond_with_error(404)
  75. end
  76. def gone
  77. respond_with_error(410)
  78. end
  79. def unprocessable_entity
  80. respond_with_error(422)
  81. end
  82. def not_acceptable
  83. respond_with_error(406)
  84. end
  85. def bad_request
  86. respond_with_error(400)
  87. end
  88. def internal_server_error
  89. respond_with_error(500)
  90. end
  91. def service_unavailable
  92. respond_with_error(503)
  93. end
  94. def too_many_requests
  95. respond_with_error(429)
  96. end
  97. def single_user_mode?
  98. @single_user_mode ||= Rails.configuration.x.single_user_mode && Account.where('id > 0').exists?
  99. end
  100. def use_seamless_external_login?
  101. Devise.pam_authentication || Devise.ldap_authentication
  102. end
  103. def omniauth_only?
  104. ENV['OMNIAUTH_ONLY'] == 'true'
  105. end
  106. def sso_account_settings
  107. ENV.fetch('SSO_ACCOUNT_SETTINGS', nil)
  108. end
  109. def current_account
  110. return @current_account if defined?(@current_account)
  111. @current_account = current_user&.account
  112. end
  113. def current_session
  114. return @current_session if defined?(@current_session)
  115. @current_session = SessionActivation.find_by(session_id: cookies.signed['_session_id']) if cookies.signed['_session_id'].present?
  116. end
  117. def current_theme
  118. return Setting.theme unless Themes.instance.names.include? current_user&.setting_theme
  119. current_user.setting_theme
  120. end
  121. def body_class_string
  122. @body_classes || ''
  123. end
  124. def respond_with_error(code)
  125. respond_to do |format|
  126. format.any { render "errors/#{code}", layout: 'error', status: code, formats: [:html] }
  127. format.json { render json: { error: Rack::Utils::HTTP_STATUS_CODES[code] }, status: code }
  128. end
  129. end
  130. def set_cache_control_defaults
  131. response.cache_control.replace(private: true, no_store: true)
  132. end
  133. end