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.

110 lines
2.7 KiB

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