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.

104 lines
2.4 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.build_account if resource.account.nil?
  25. end
  26. def configure_sign_up_params
  27. devise_parameter_sanitizer.permit(:sign_up) do |u|
  28. u.permit({ account_attributes: [:username] }, :email, :password, :password_confirmation, :invite_code)
  29. end
  30. end
  31. def after_sign_up_path_for(_resource)
  32. new_user_session_path
  33. end
  34. def after_sign_in_path_for(_resource)
  35. set_invite
  36. if @invite&.autofollow?
  37. short_account_path(@invite.user.account)
  38. else
  39. super
  40. end
  41. end
  42. def after_inactive_sign_up_path_for(_resource)
  43. new_user_session_path
  44. end
  45. def after_update_path_for(_resource)
  46. edit_user_registration_path
  47. end
  48. def check_enabled_registrations
  49. redirect_to root_path if single_user_mode? || !allowed_registrations?
  50. end
  51. def allowed_registrations?
  52. Setting.open_registrations || @invite&.valid_for_use?
  53. end
  54. def invite_code
  55. if params[:user]
  56. params[:user][:invite_code]
  57. else
  58. params[:invite_code]
  59. end
  60. end
  61. private
  62. def set_pack
  63. use_pack %w(edit update).include?(action_name) ? 'admin' : 'auth'
  64. end
  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. end
  74. def determine_layout
  75. %w(edit update).include?(action_name) ? 'admin' : 'auth'
  76. end
  77. def set_sessions
  78. @sessions = current_user.session_activations
  79. end
  80. end