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.

132 lines
3.4 KiB

  1. # frozen_string_literal: true
  2. class Auth::RegistrationsController < Devise::RegistrationsController
  3. include Devise::Controllers::Rememberable
  4. include RegistrationSpamConcern
  5. layout :determine_layout
  6. before_action :set_invite, only: [:new, :create]
  7. before_action :check_enabled_registrations, only: [:new, :create]
  8. before_action :configure_sign_up_params, only: [:create]
  9. before_action :set_sessions, only: [:edit, :update]
  10. before_action :set_instance_presenter, only: [:new, :create, :update]
  11. before_action :set_body_classes, only: [:new, :create, :edit, :update]
  12. before_action :require_not_suspended!, only: [:update]
  13. before_action :set_cache_headers, only: [:edit, :update]
  14. before_action :set_registration_form_time, only: :new
  15. skip_before_action :require_functional!, only: [:edit, :update]
  16. def new
  17. super(&:build_invite_request)
  18. end
  19. def destroy
  20. not_found
  21. end
  22. def update
  23. super do |resource|
  24. if resource.saved_change_to_encrypted_password?
  25. resource.clear_other_sessions(current_session.session_id)
  26. resource.forget_me!
  27. remember_me(resource)
  28. end
  29. end
  30. end
  31. protected
  32. def update_resource(resource, params)
  33. params[:password] = nil if Devise.pam_authentication && resource.encrypted_password.blank?
  34. super
  35. end
  36. def build_resource(hash = nil)
  37. super(hash)
  38. resource.locale = I18n.locale
  39. resource.invite_code = params[:invite_code] if resource.invite_code.blank?
  40. resource.registration_form_time = session[:registration_form_time]
  41. resource.sign_up_ip = request.remote_ip
  42. resource.build_account if resource.account.nil?
  43. end
  44. def configure_sign_up_params
  45. devise_parameter_sanitizer.permit(:sign_up) do |u|
  46. u.permit({ account_attributes: [:username], invite_request_attributes: [:text] }, :email, :password, :password_confirmation, :invite_code, :agreement, :website, :confirm_password)
  47. end
  48. end
  49. def after_sign_up_path_for(_resource)
  50. auth_setup_path
  51. end
  52. def after_sign_in_path_for(_resource)
  53. set_invite
  54. if @invite&.autofollow?
  55. short_account_path(@invite.user.account)
  56. else
  57. super
  58. end
  59. end
  60. def after_inactive_sign_up_path_for(_resource)
  61. new_user_session_path
  62. end
  63. def after_update_path_for(_resource)
  64. edit_user_registration_path
  65. end
  66. def check_enabled_registrations
  67. redirect_to root_path if single_user_mode? || !allowed_registrations?
  68. end
  69. def allowed_registrations?
  70. Setting.registrations_mode != 'none' || @invite&.valid_for_use?
  71. end
  72. def invite_code
  73. if params[:user]
  74. params[:user][:invite_code]
  75. else
  76. params[:invite_code]
  77. end
  78. end
  79. private
  80. def set_instance_presenter
  81. @instance_presenter = InstancePresenter.new
  82. end
  83. def set_body_classes
  84. @body_classes = %w(edit update).include?(action_name) ? 'admin' : 'lighter'
  85. end
  86. def set_invite
  87. invite = invite_code.present? ? Invite.find_by(code: invite_code) : nil
  88. @invite = invite&.valid_for_use? ? invite : nil
  89. end
  90. def determine_layout
  91. %w(edit update).include?(action_name) ? 'admin' : 'auth'
  92. end
  93. def set_sessions
  94. @sessions = current_user.session_activations
  95. end
  96. def require_not_suspended!
  97. forbidden if current_account.suspended?
  98. end
  99. def set_cache_headers
  100. response.headers['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate'
  101. end
  102. end