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.

69 lines
1.6 KiB

  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_sessions, only: [:edit, :update]
  7. before_action :set_instance_presenter, only: [:new, :create, :update]
  8. def destroy
  9. not_found
  10. end
  11. protected
  12. def build_resource(hash = nil)
  13. super(hash)
  14. resource.locale = I18n.locale
  15. resource.invite_code = params[:invite_code] if resource.invite_code.blank?
  16. resource.build_account if resource.account.nil?
  17. end
  18. def configure_sign_up_params
  19. devise_parameter_sanitizer.permit(:sign_up) do |u|
  20. u.permit({ account_attributes: [:username] }, :email, :password, :password_confirmation, :invite_code)
  21. end
  22. end
  23. def after_sign_up_path_for(_resource)
  24. new_user_session_path
  25. end
  26. def after_inactive_sign_up_path_for(_resource)
  27. new_user_session_path
  28. end
  29. def check_enabled_registrations
  30. redirect_to root_path if single_user_mode? || !allowed_registrations?
  31. end
  32. def allowed_registrations?
  33. Setting.open_registrations || (invite_code.present? && Invite.find_by(code: invite_code)&.valid_for_use?)
  34. end
  35. def invite_code
  36. if params[:user]
  37. params[:user][:invite_code]
  38. else
  39. params[:invite_code]
  40. end
  41. end
  42. private
  43. def set_instance_presenter
  44. @instance_presenter = InstancePresenter.new
  45. end
  46. def determine_layout
  47. %w(edit update).include?(action_name) ? 'admin' : 'auth'
  48. end
  49. def set_sessions
  50. @sessions = current_user.session_activations
  51. end
  52. end