1
0

registrations_controller.rb 2.2 KB

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