confirmations_controller.rb 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # frozen_string_literal: true
  2. class Auth::ConfirmationsController < Devise::ConfirmationsController
  3. include Auth::CaptchaConcern
  4. layout 'auth'
  5. before_action :set_body_classes
  6. before_action :set_confirmation_user!, only: [:show, :confirm_captcha]
  7. before_action :redirect_confirmed_user, if: :signed_in_confirmed_user?
  8. before_action :extend_csp_for_captcha!, only: [:show, :confirm_captcha]
  9. before_action :require_captcha_if_needed!, only: [:show]
  10. skip_before_action :check_self_destruct!
  11. skip_before_action :require_functional!
  12. def show
  13. old_session_values = session.to_hash
  14. reset_session
  15. session.update old_session_values.except('session_id')
  16. super
  17. end
  18. def new
  19. super
  20. resource.email = current_user.unconfirmed_email || current_user.email if user_signed_in?
  21. end
  22. def confirm_captcha
  23. check_captcha! do |message|
  24. flash.now[:alert] = message
  25. render :captcha
  26. return
  27. end
  28. show
  29. end
  30. def redirect_to_app?
  31. truthy_param?(:redirect_to_app)
  32. end
  33. helper_method :redirect_to_app?
  34. private
  35. def require_captcha_if_needed!
  36. render :captcha if captcha_required?
  37. end
  38. def set_confirmation_user!
  39. # We need to reimplement looking up the user because
  40. # Devise::ConfirmationsController#show looks up and confirms in one
  41. # step.
  42. confirmation_token = params[:confirmation_token]
  43. return if confirmation_token.nil?
  44. @confirmation_user = User.find_first_by_auth_conditions(confirmation_token: confirmation_token)
  45. end
  46. def captcha_user_bypass?
  47. @confirmation_user.nil? || @confirmation_user.confirmed?
  48. end
  49. def redirect_confirmed_user
  50. redirect_to(current_user.approved? ? root_path : edit_user_registration_path)
  51. end
  52. def signed_in_confirmed_user?
  53. user_signed_in? && current_user.confirmed? && current_user.unconfirmed_email.blank?
  54. end
  55. def set_body_classes
  56. @body_classes = 'lighter'
  57. end
  58. def after_resending_confirmation_instructions_path_for(_resource_name)
  59. if user_signed_in?
  60. if current_user.confirmed? && current_user.approved?
  61. edit_user_registration_path
  62. else
  63. auth_setup_path
  64. end
  65. else
  66. new_user_session_path
  67. end
  68. end
  69. def after_confirmation_path_for(_resource_name, user)
  70. if user.created_by_application && redirect_to_app?
  71. user.created_by_application.confirmation_redirect_uri
  72. elsif user_signed_in?
  73. web_url('start')
  74. else
  75. new_user_session_path
  76. end
  77. end
  78. end