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.

154 lines
4.1 KiB

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