1
0

confirmations_controller.rb 2.3 KB

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