闭社主体 forked from https://github.com/tootsuite/mastodon
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.3 KiB

  1. # frozen_string_literal: true
  2. class Auth::RegistrationsController < Devise::RegistrationsController
  3. include RegistrationSpamConcern
  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. before_action :set_registration_form_time, only: :new
  14. skip_before_action :require_functional!, only: [:edit, :update]
  15. def new
  16. super(&:build_invite_request)
  17. end
  18. def destroy
  19. not_found
  20. end
  21. def update
  22. super do |resource|
  23. if resource.saved_change_to_encrypted_password?
  24. resource.clear_other_sessions(current_session.session_id)
  25. end
  26. end
  27. end
  28. protected
  29. def update_resource(resource, params)
  30. params[:password] = nil if Devise.pam_authentication && resource.encrypted_password.blank?
  31. super
  32. end
  33. def build_resource(hash = nil)
  34. super(hash)
  35. resource.locale = I18n.locale
  36. resource.invite_code = params[:invite_code] if resource.invite_code.blank?
  37. resource.registration_form_time = session[:registration_form_time]
  38. resource.sign_up_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, :website, :confirm_password)
  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