The code powering m.abunchtell.com https://m.abunchtell.com
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.
 
 
 
 

146 lines
3.8 KiB

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