application_controller.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. class ApplicationController < ActionController::Base
  2. include ApplicationHelper
  3. include RequestOriginValidation
  4. # Prevent CSRF attacks by raising an exception.
  5. # For APIs, you may want to use :null_session instead.
  6. protect_from_forgery with: :exception
  7. before_filter :cors
  8. before_filter :configure_permitted_parameters, if: :devise_controller?
  9. before_filter :set_locale
  10. before_filter :user_conditional_logic
  11. skip_before_action :set_ahoy_cookies
  12. skip_before_action :track_ahoy_visit
  13. skip_before_action :set_ahoy_request_store
  14. def user_conditional_logic
  15. if user_signed_in?
  16. lock_users_with_expired_passwords! unless user_is_being_told_to_reset_pass_or_is_resetting_pass?
  17. end
  18. end
  19. # This method seems to check if the request is coming from a domain listed in
  20. # `cors_allowed_domains` in application.yml, and if it is, the response gets
  21. # a header allowing the requesting domain to use this app's CRUD
  22. def cors
  23. if Actioncenter::Application.config.cors_allowed_domains.include? request.env["HTTP_ORIGIN"] or Actioncenter::Application.config.cors_allowed_domains.include? "*"
  24. response.headers["Access-Control-Allow-Origin"] = request.env["HTTP_ORIGIN"]
  25. end
  26. end
  27. def self.manifest(value = nil)
  28. if value.nil?
  29. @manifest
  30. else
  31. @manifest = value
  32. end
  33. end
  34. def manifest
  35. self.class.manifest || "application"
  36. end
  37. # if the current_user's password is expired, force them to the reset page
  38. # or lock them out of secure areas
  39. def lock_users_with_expired_passwords!
  40. if current_user.password_expired?
  41. verifier = ActiveSupport::MessageVerifier.new(Rails.application.secrets.secret_key_base)
  42. redirect_to sessions_password_reset_path(continue: verifier.generate(request.path))
  43. end
  44. end
  45. def user_is_being_told_to_reset_pass_or_is_resetting_pass?
  46. (params[:controller] == "sessions" && params[:action] == "password_reset") ||
  47. (params[:controller] == "registrations" && params[:action] == "update")
  48. end
  49. protected
  50. def configure_permitted_parameters
  51. devise_parameter_sanitizer.for(:sign_up) << :record_activity
  52. devise_parameter_sanitizer.for(:sign_up) << :subscribe
  53. end
  54. def set_locale
  55. I18n.locale = http_accept_language.compatible_language_from(I18n.available_locales)
  56. end
  57. end