registrations_controller.rb 1.96 KB
Newer Older
1 2
# frozen_string_literal: true

3
class Auth::RegistrationsController < Devise::RegistrationsController
4
  layout :determine_layout
5

6
  before_action :set_invite, only: [:new, :create]
7
  before_action :check_enabled_registrations, only: [:new, :create]
Eugen Rochko's avatar
Eugen Rochko committed
8
  before_action :configure_sign_up_params, only: [:create]
9
  before_action :set_sessions, only: [:edit, :update]
10
  before_action :set_instance_presenter, only: [:new, :create, :update]
11

12 13 14 15
  def destroy
    not_found
  end

16 17
  protected

Alexander's avatar
Alexander committed
18 19 20 21 22
  def update_resource(resource, params)
    params[:password] = nil if Devise.pam_authentication && resource.encrypted_password.blank?
    super
  end

23 24
  def build_resource(hash = nil)
    super(hash)
Eugen Rochko's avatar
Eugen Rochko committed
25 26 27 28

    resource.locale      = I18n.locale
    resource.invite_code = params[:invite_code] if resource.invite_code.blank?

Eugen Rochko's avatar
Eugen Rochko committed
29
    resource.build_account if resource.account.nil?
30 31 32
  end

  def configure_sign_up_params
Eugen Rochko's avatar
Eugen Rochko committed
33
    devise_parameter_sanitizer.permit(:sign_up) do |u|
Eugen Rochko's avatar
Eugen Rochko committed
34
      u.permit({ account_attributes: [:username] }, :email, :password, :password_confirmation, :invite_code)
35 36 37
    end
  end

38
  def after_sign_up_path_for(_resource)
39
    new_user_session_path
40
  end
Eugen Rochko's avatar
Eugen Rochko committed
41

42 43 44 45
  def after_inactive_sign_up_path_for(_resource)
    new_user_session_path
  end

46 47 48 49
  def after_update_path_for(_resource)
    edit_user_registration_path
  end

50
  def check_enabled_registrations
Eugen Rochko's avatar
Eugen Rochko committed
51 52 53 54
    redirect_to root_path if single_user_mode? || !allowed_registrations?
  end

  def allowed_registrations?
55
    Setting.open_registrations || @invite&.valid_for_use?
Eugen Rochko's avatar
Eugen Rochko committed
56 57 58 59 60 61 62 63
  end

  def invite_code
    if params[:user]
      params[:user][:invite_code]
    else
      params[:invite_code]
    end
Eugen Rochko's avatar
Eugen Rochko committed
64
  end
65

66
  private
67

68 69 70 71
  def set_instance_presenter
    @instance_presenter = InstancePresenter.new
  end

72 73 74 75
  def set_invite
    @invite = invite_code.present? ? Invite.find_by(code: invite_code) : nil
  end

76 77 78
  def determine_layout
    %w(edit update).include?(action_name) ? 'admin' : 'auth'
  end
79 80 81 82

  def set_sessions
    @sessions = current_user.session_activations
  end
83
end