application_controller.rb 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 ActiveRecord::RecordNotFound, with: :not_found
  20. rescue_from ActionController::InvalidAuthenticityToken, with: :unprocessable_entity
  21. rescue_from ActionController::UnknownFormat, with: :not_acceptable
  22. rescue_from Mastodon::NotPermittedError, with: :forbidden
  23. before_action :store_current_location, except: :raise_not_found, unless: :devise_controller?
  24. before_action :require_functional!, if: :user_signed_in?
  25. def raise_not_found
  26. raise ActionController::RoutingError, "No route matches #{params[:unmatched_route]}"
  27. end
  28. private
  29. def https_enabled?
  30. Rails.env.production?
  31. end
  32. def authorized_fetch_mode?
  33. ENV['AUTHORIZED_FETCH'] == 'true' || Rails.configuration.x.whitelist_mode
  34. end
  35. def public_fetch_mode?
  36. !authorized_fetch_mode?
  37. end
  38. def store_current_location
  39. store_location_for(:user, request.url) unless request.format == :json
  40. end
  41. def require_admin!
  42. forbidden unless current_user&.admin?
  43. end
  44. def require_staff!
  45. forbidden unless current_user&.staff?
  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. new_user_session_path
  52. end
  53. protected
  54. def truthy_param?(key)
  55. ActiveModel::Type::Boolean.new.cast(params[key])
  56. end
  57. def forbidden
  58. respond_with_error(403)
  59. end
  60. def not_found
  61. respond_with_error(404)
  62. end
  63. def gone
  64. respond_with_error(410)
  65. end
  66. def unprocessable_entity
  67. respond_with_error(422)
  68. end
  69. def not_acceptable
  70. respond_with_error(406)
  71. end
  72. def single_user_mode?
  73. @single_user_mode ||= Rails.configuration.x.single_user_mode && Account.where('id > 0').exists?
  74. end
  75. def use_seamless_external_login?
  76. Devise.pam_authentication || Devise.ldap_authentication
  77. end
  78. def current_account
  79. return @current_account if defined?(@current_account)
  80. @current_account = current_user&.account
  81. end
  82. def current_session
  83. return @current_session if defined?(@current_session)
  84. @current_session = SessionActivation.find_by(session_id: cookies.signed['_session_id']) if cookies.signed['_session_id'].present?
  85. end
  86. def current_theme
  87. return Setting.theme unless Themes.instance.names.include? current_user&.setting_theme
  88. current_user.setting_theme
  89. end
  90. def respond_with_error(code)
  91. respond_to do |format|
  92. format.any { head code }
  93. format.html { render "errors/#{code}", layout: 'error', status: code }
  94. end
  95. end
  96. end