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.

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