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.

129 lines
3.2 KiB

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