registrations_controller.rb 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. def new
  11. super(&:build_invite_request)
  12. end
  13. def destroy
  14. not_found
  15. end
  16. protected
  17. def update_resource(resource, params)
  18. params[:password] = nil if Devise.pam_authentication && resource.encrypted_password.blank?
  19. super
  20. end
  21. def build_resource(hash = nil)
  22. super(hash)
  23. resource.locale = I18n.locale
  24. resource.invite_code = params[:invite_code] if resource.invite_code.blank?
  25. resource.agreement = true
  26. resource.current_sign_in_ip = request.remote_ip
  27. resource.build_account if resource.account.nil?
  28. end
  29. def configure_sign_up_params
  30. devise_parameter_sanitizer.permit(:sign_up) do |u|
  31. u.permit({ account_attributes: [:username], invite_request_attributes: [:text] }, :email, :password, :password_confirmation, :invite_code)
  32. end
  33. end
  34. def after_sign_up_path_for(_resource)
  35. new_user_session_path
  36. end
  37. def after_sign_in_path_for(_resource)
  38. set_invite
  39. if @invite&.autofollow?
  40. short_account_path(@invite.user.account)
  41. else
  42. super
  43. end
  44. end
  45. def after_inactive_sign_up_path_for(_resource)
  46. new_user_session_path
  47. end
  48. def after_update_path_for(_resource)
  49. edit_user_registration_path
  50. end
  51. def check_enabled_registrations
  52. redirect_to root_path if single_user_mode? || !allowed_registrations?
  53. end
  54. def allowed_registrations?
  55. Setting.registrations_mode != 'none' || @invite&.valid_for_use?
  56. end
  57. def invite_code
  58. if params[:user]
  59. params[:user][:invite_code]
  60. else
  61. params[:invite_code]
  62. end
  63. end
  64. private
  65. def set_instance_presenter
  66. @instance_presenter = InstancePresenter.new
  67. end
  68. def set_body_classes
  69. @body_classes = %w(edit update).include?(action_name) ? 'admin' : 'lighter'
  70. end
  71. def set_invite
  72. invite = invite_code.present? ? Invite.find_by(code: invite_code) : nil
  73. @invite = invite&.valid_for_use? ? invite : nil
  74. end
  75. def determine_layout
  76. %w(edit update).include?(action_name) ? 'admin' : 'auth'
  77. end
  78. def set_sessions
  79. @sessions = current_user.session_activations
  80. end
  81. end