application_controller.rb 4.9 KB

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