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

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

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

11 12 13 14
  def destroy
    not_found
  end

15 16 17 18
  protected

  def build_resource(hash = nil)
    super(hash)
Eugen Rochko's avatar
Eugen Rochko committed
19 20 21 22

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

Eugen Rochko's avatar
Eugen Rochko committed
23
    resource.build_account if resource.account.nil?
24 25 26
  end

  def configure_sign_up_params
Eugen Rochko's avatar
Eugen Rochko committed
27
    devise_parameter_sanitizer.permit(:sign_up) do |u|
Eugen Rochko's avatar
Eugen Rochko committed
28
      u.permit({ account_attributes: [:username] }, :email, :password, :password_confirmation, :invite_code)
29 30 31
    end
  end

32
  def after_sign_up_path_for(_resource)
33
    new_user_session_path
34
  end
Eugen Rochko's avatar
Eugen Rochko committed
35

36 37 38 39
  def after_inactive_sign_up_path_for(_resource)
    new_user_session_path
  end

40 41 42 43
  def after_update_path_for(_resource)
    edit_user_registration_path
  end

44
  def check_enabled_registrations
Eugen Rochko's avatar
Eugen Rochko committed
45 46 47 48 49 50 51 52 53 54 55 56 57
    redirect_to root_path if single_user_mode? || !allowed_registrations?
  end

  def allowed_registrations?
    Setting.open_registrations || (invite_code.present? && Invite.find_by(code: invite_code)&.valid_for_use?)
  end

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

60
  private
61

62 63 64 65
  def set_instance_presenter
    @instance_presenter = InstancePresenter.new
  end

66 67 68
  def determine_layout
    %w(edit update).include?(action_name) ? 'admin' : 'auth'
  end
69 70 71 72

  def set_sessions
    @sessions = current_user.session_activations
  end
73
end