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.

73 lines
1.7 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 after_update_path_for(_resource)
  30. edit_user_registration_path
  31. end
  32. def check_enabled_registrations
  33. redirect_to root_path if single_user_mode? || !allowed_registrations?
  34. end
  35. def allowed_registrations?
  36. Setting.open_registrations || (invite_code.present? && Invite.find_by(code: invite_code)&.valid_for_use?)
  37. end
  38. def invite_code
  39. if params[:user]
  40. params[:user][:invite_code]
  41. else
  42. params[:invite_code]
  43. end
  44. end
  45. private
  46. def set_instance_presenter
  47. @instance_presenter = InstancePresenter.new
  48. end
  49. def determine_layout
  50. %w(edit update).include?(action_name) ? 'admin' : 'auth'
  51. end
  52. def set_sessions
  53. @sessions = current_user.session_activations
  54. end
  55. end