registrations_controller.rb 3.07 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]
ThibG's avatar
ThibG committed
11
  before_action :set_body_classes, only: [:new, :create, :edit, :update]
12
  before_action :require_not_suspended!, only: [:update]
13
  before_action :set_cache_headers, only: [:edit, :update]
14 15

  skip_before_action :require_functional!, only: [:edit, :update]
16

17 18 19 20
  def new
    super(&:build_invite_request)
  end

21 22 23 24
  def destroy
    not_found
  end

25 26 27 28 29 30
  def update
    super do |resource|
      resource.clear_other_sessions(current_session.session_id) if resource.saved_change_to_encrypted_password?
    end
  end

31 32
  protected

Alexander's avatar
Alexander committed
33 34
  def update_resource(resource, params)
    params[:password] = nil if Devise.pam_authentication && resource.encrypted_password.blank?
35

Alexander's avatar
Alexander committed
36 37 38
    super
  end

39 40
  def build_resource(hash = nil)
    super(hash)
Eugen Rochko's avatar
Eugen Rochko committed
41

42 43 44 45
    resource.locale             = I18n.locale
    resource.invite_code        = params[:invite_code] if resource.invite_code.blank?
    resource.agreement          = true
    resource.current_sign_in_ip = request.remote_ip
Eugen Rochko's avatar
Eugen Rochko committed
46

Eugen Rochko's avatar
Eugen Rochko committed
47
    resource.build_account if resource.account.nil?
48 49 50
  end

  def configure_sign_up_params
Eugen Rochko's avatar
Eugen Rochko committed
51
    devise_parameter_sanitizer.permit(:sign_up) do |u|
52
      u.permit({ account_attributes: [:username], invite_request_attributes: [:text] }, :email, :password, :password_confirmation, :invite_code)
53 54 55
    end
  end

56
  def after_sign_up_path_for(_resource)
57
    auth_setup_path
58
  end
Eugen Rochko's avatar
Eugen Rochko committed
59

60 61 62 63 64 65 66 67 68 69
  def after_sign_in_path_for(_resource)
    set_invite

    if @invite&.autofollow?
      short_account_path(@invite.user.account)
    else
      super
    end
  end

70 71 72 73
  def after_inactive_sign_up_path_for(_resource)
    new_user_session_path
  end

74 75 76 77
  def after_update_path_for(_resource)
    edit_user_registration_path
  end

78
  def check_enabled_registrations
Eugen Rochko's avatar
Eugen Rochko committed
79 80 81 82
    redirect_to root_path if single_user_mode? || !allowed_registrations?
  end

  def allowed_registrations?
83
    Setting.registrations_mode != 'none' || @invite&.valid_for_use?
Eugen Rochko's avatar
Eugen Rochko committed
84 85 86 87 88 89 90 91
  end

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

94
  private
95

96 97 98 99
  def set_instance_presenter
    @instance_presenter = InstancePresenter.new
  end

100
  def set_body_classes
ThibG's avatar
ThibG committed
101
    @body_classes = %w(edit update).include?(action_name) ? 'admin' : 'lighter'
102 103
  end

104
  def set_invite
105 106
    invite = invite_code.present? ? Invite.find_by(code: invite_code) : nil
    @invite = invite&.valid_for_use? ? invite : nil
107 108
  end

109 110 111
  def determine_layout
    %w(edit update).include?(action_name) ? 'admin' : 'auth'
  end
112 113 114 115

  def set_sessions
    @sessions = current_user.session_activations
  end
116 117 118 119

  def require_not_suspended!
    forbidden if current_account.suspended?
  end
120 121 122 123

  def set_cache_headers
    response.headers['Cache-Control'] = 'no-cache, no-store, max-age=0, must-revalidate'
  end
124
end