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.
 
 
 
 

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