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.

83 lines
2.0 KiB

7 years ago
  1. # frozen_string_literal: true
  2. class Auth::RegistrationsController < Devise::RegistrationsController
  3. layout :determine_layout
  4. before_action :check_enabled_registrations, only: [:new, :create]
  5. before_action :configure_sign_up_params, only: [:create]
  6. before_action :set_pack
  7. before_action :set_sessions, only: [:edit, :update]
  8. before_action :set_instance_presenter, only: [:new, :create, :update]
  9. def destroy
  10. not_found
  11. end
  12. protected
  13. def update_resource(resource, params)
  14. params[:password] = nil if Devise.pam_authentication && resource.encrypted_password.blank?
  15. super
  16. end
  17. def build_resource(hash = nil)
  18. super(hash)
  19. resource.locale = I18n.locale
  20. resource.invite_code = params[:invite_code] if resource.invite_code.blank?
  21. resource.build_account if resource.account.nil?
  22. end
  23. def configure_sign_up_params
  24. devise_parameter_sanitizer.permit(:sign_up) do |u|
  25. u.permit({ account_attributes: [:username] }, :email, :password, :password_confirmation, :invite_code)
  26. end
  27. end
  28. def after_sign_up_path_for(_resource)
  29. new_user_session_path
  30. end
  31. def after_inactive_sign_up_path_for(_resource)
  32. new_user_session_path
  33. end
  34. def after_update_path_for(_resource)
  35. edit_user_registration_path
  36. end
  37. def check_enabled_registrations
  38. redirect_to root_path if single_user_mode? || !allowed_registrations?
  39. end
  40. def allowed_registrations?
  41. Setting.open_registrations || (invite_code.present? && Invite.find_by(code: invite_code)&.valid_for_use?)
  42. end
  43. def invite_code
  44. if params[:user]
  45. params[:user][:invite_code]
  46. else
  47. params[:invite_code]
  48. end
  49. end
  50. private
  51. def set_pack
  52. use_pack %w(edit update).include?(action_name) ? 'admin' : 'auth'
  53. end
  54. def set_instance_presenter
  55. @instance_presenter = InstancePresenter.new
  56. end
  57. def determine_layout
  58. %w(edit update).include?(action_name) ? 'admin' : 'auth'
  59. end
  60. def set_sessions
  61. @sessions = current_user.session_activations
  62. end
  63. end