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.

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