application_controller.rb 4.9 KB

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