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.

144 lines
3.9 KiB

8 years ago
6 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. include Localized
  7. include UserTrackingConcern
  8. include SessionTrackingConcern
  9. include CacheConcern
  10. include DomainControlHelper
  11. include ThemingConcern
  12. helper_method :current_account
  13. helper_method :current_session
  14. helper_method :current_flavour
  15. helper_method :current_skin
  16. helper_method :single_user_mode?
  17. helper_method :use_seamless_external_login?
  18. helper_method :whitelist_mode?
  19. rescue_from ActionController::ParameterMissing, Paperclip::AdapterRegistry::NoHandlerError, with: :bad_request
  20. rescue_from Mastodon::NotPermittedError, with: :forbidden
  21. rescue_from ActionController::RoutingError, ActiveRecord::RecordNotFound, with: :not_found
  22. rescue_from ActionController::UnknownFormat, with: :not_acceptable
  23. rescue_from ActionController::InvalidAuthenticityToken, with: :unprocessable_entity
  24. rescue_from Mastodon::RateLimitExceededError, with: :too_many_requests
  25. rescue_from HTTP::Error, OpenSSL::SSL::SSLError, with: :internal_server_error
  26. rescue_from Mastodon::RaceConditionError, Stoplight::Error::RedLight, ActiveRecord::SerializationFailure, with: :service_unavailable
  27. rescue_from Seahorse::Client::NetworkingError do |e|
  28. Rails.logger.warn "Storage server error: #{e}"
  29. service_unavailable
  30. end
  31. before_action :store_current_location, except: :raise_not_found, unless: :devise_controller?
  32. before_action :require_functional!, if: :user_signed_in?
  33. skip_before_action :verify_authenticity_token, only: :raise_not_found
  34. def raise_not_found
  35. raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}"
  36. end
  37. private
  38. def authorized_fetch_mode?
  39. ENV['AUTHORIZED_FETCH'] == 'true' || Rails.configuration.x.whitelist_mode
  40. end
  41. def public_fetch_mode?
  42. !authorized_fetch_mode?
  43. end
  44. def store_current_location
  45. store_location_for(:user, request.url) unless [:json, :rss].include?(request.format&.to_sym)
  46. end
  47. def require_functional!
  48. redirect_to edit_user_registration_path unless current_user.functional?
  49. end
  50. def after_sign_out_path_for(_resource_or_scope)
  51. if ENV['OMNIAUTH_ONLY'] == 'true' && ENV['OIDC_ENABLED'] == 'true'
  52. '/auth/auth/openid_connect/logout'
  53. else
  54. new_user_session_path
  55. end
  56. end
  57. protected
  58. def truthy_param?(key)
  59. ActiveModel::Type::Boolean.new.cast(params[key])
  60. end
  61. def forbidden
  62. respond_with_error(403)
  63. end
  64. def not_found
  65. respond_with_error(404)
  66. end
  67. def gone
  68. respond_with_error(410)
  69. end
  70. def unprocessable_entity
  71. respond_with_error(422)
  72. end
  73. def not_acceptable
  74. respond_with_error(406)
  75. end
  76. def bad_request
  77. respond_with_error(400)
  78. end
  79. def internal_server_error
  80. respond_with_error(500)
  81. end
  82. def service_unavailable
  83. respond_with_error(503)
  84. end
  85. def too_many_requests
  86. respond_with_error(429)
  87. end
  88. def single_user_mode?
  89. @single_user_mode ||= Rails.configuration.x.single_user_mode && Account.where('id > 0').exists?
  90. end
  91. def use_seamless_external_login?
  92. Devise.pam_authentication || Devise.ldap_authentication
  93. end
  94. def current_account
  95. return @current_account if defined?(@current_account)
  96. @current_account = current_user&.account
  97. end
  98. def current_session
  99. return @current_session if defined?(@current_session)
  100. @current_session = SessionActivation.find_by(session_id: cookies.signed['_session_id']) if cookies.signed['_session_id'].present?
  101. end
  102. def respond_with_error(code)
  103. respond_to do |format|
  104. format.any do
  105. use_pack 'error'
  106. render "errors/#{code}", layout: 'error', status: code, formats: [:html]
  107. end
  108. format.json { render json: { error: Rack::Utils::HTTP_STATUS_CODES[code] }, status: code }
  109. end
  110. end
  111. end