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.

123 lines
3.0 KiB

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