2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
class Auth::Result
|
2020-06-17 06:15:53 -04:00
|
|
|
ATTRIBUTES = [
|
|
|
|
:user,
|
|
|
|
:name,
|
|
|
|
:username,
|
|
|
|
:email,
|
|
|
|
:email_valid,
|
|
|
|
:extra_data,
|
|
|
|
:awaiting_activation,
|
|
|
|
:awaiting_approval,
|
|
|
|
:authenticated,
|
|
|
|
:authenticator_name,
|
|
|
|
:requires_invite,
|
|
|
|
:not_allowed_from_ip_address,
|
|
|
|
:admin_not_allowed_from_ip_address,
|
2020-06-18 06:01:02 -04:00
|
|
|
:omit_username, # Used by plugins to prevent username edits
|
2020-06-17 06:15:53 -04:00
|
|
|
:skip_email_validation,
|
|
|
|
:destination_url,
|
|
|
|
:omniauth_disallow_totp,
|
2017-11-09 10:49:12 -05:00
|
|
|
:failed,
|
|
|
|
:failed_reason,
|
|
|
|
:failed_code
|
2020-06-17 06:15:53 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
attr_accessor *ATTRIBUTES
|
|
|
|
|
|
|
|
# These are stored in the session during
|
|
|
|
# account creation. The user cannot read or modify them
|
|
|
|
SESSION_ATTRIBUTES = [
|
|
|
|
:email,
|
|
|
|
:username,
|
|
|
|
:email_valid,
|
|
|
|
:omit_username,
|
|
|
|
:name,
|
|
|
|
:authenticator_name,
|
|
|
|
:extra_data,
|
|
|
|
:skip_email_validation
|
|
|
|
]
|
|
|
|
|
|
|
|
def [](key)
|
|
|
|
key = key.to_sym
|
|
|
|
public_send(key) if ATTRIBUTES.include?(key)
|
|
|
|
end
|
2015-06-24 12:12:43 -04:00
|
|
|
|
|
|
|
def initialize
|
|
|
|
@failed = false
|
|
|
|
end
|
|
|
|
|
2019-08-13 12:53:38 -04:00
|
|
|
def email
|
2019-08-14 07:08:59 -04:00
|
|
|
@email&.downcase
|
2019-08-13 12:53:38 -04:00
|
|
|
end
|
|
|
|
|
2021-02-22 07:05:36 -05:00
|
|
|
def email_valid=(val)
|
|
|
|
if !val.in? [true, false, nil]
|
|
|
|
raise ArgumentError, "email_valid should be boolean or nil"
|
|
|
|
end
|
|
|
|
@email_valid = !!val
|
|
|
|
end
|
|
|
|
|
2015-06-24 12:12:43 -04:00
|
|
|
def failed?
|
|
|
|
!!@failed
|
|
|
|
end
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
def session_data
|
2020-06-17 06:15:53 -04:00
|
|
|
SESSION_ATTRIBUTES.map { |att| [att, public_send(att)] }.to_h
|
|
|
|
end
|
|
|
|
|
2020-06-18 06:01:02 -04:00
|
|
|
def self.from_session_data(data, user:)
|
2020-06-17 06:15:53 -04:00
|
|
|
result = new
|
|
|
|
data = data.symbolize_keys
|
|
|
|
SESSION_ATTRIBUTES.each { |att| result.public_send("#{att}=", data[att]) }
|
2020-06-18 06:01:02 -04:00
|
|
|
result.user = user
|
2020-06-17 06:15:53 -04:00
|
|
|
result
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|
|
|
|
|
2020-06-18 06:01:02 -04:00
|
|
|
def apply_user_attributes!
|
|
|
|
change_made = false
|
2021-02-08 05:04:33 -05:00
|
|
|
if SiteSetting.auth_overrides_username? && username.present? && username != user.username
|
2021-02-25 20:33:20 -05:00
|
|
|
user.username = UserNameSuggester.suggest(username_suggester_attributes, user.username)
|
2020-06-18 06:01:02 -04:00
|
|
|
change_made = true
|
|
|
|
end
|
|
|
|
|
2021-02-08 05:04:33 -05:00
|
|
|
if SiteSetting.auth_overrides_email && email_valid && email.present? && user.email != Email.downcase(email)
|
2020-06-18 06:01:02 -04:00
|
|
|
user.email = email
|
|
|
|
change_made = true
|
|
|
|
end
|
|
|
|
|
2021-02-08 05:04:33 -05:00
|
|
|
if SiteSetting.auth_overrides_name && name.present? && user.name != name
|
2020-06-18 06:01:02 -04:00
|
|
|
user.name = name
|
|
|
|
change_made = true
|
|
|
|
end
|
|
|
|
|
|
|
|
change_made
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_edit_name
|
2021-02-08 05:04:33 -05:00
|
|
|
!SiteSetting.auth_overrides_name
|
2020-06-18 06:01:02 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_edit_username
|
2021-02-08 05:04:33 -05:00
|
|
|
!(SiteSetting.auth_overrides_username || omit_username)
|
2020-06-18 06:01:02 -04:00
|
|
|
end
|
|
|
|
|
2013-08-23 02:20:43 -04:00
|
|
|
def to_client_hash
|
2013-08-28 03:18:31 -04:00
|
|
|
if requires_invite
|
2020-06-17 06:15:53 -04:00
|
|
|
return { requires_invite: true }
|
|
|
|
end
|
|
|
|
|
|
|
|
if user&.suspended?
|
|
|
|
return {
|
|
|
|
suspended: true,
|
2021-07-20 06:42:08 -04:00
|
|
|
suspended_message: user.suspended_message
|
2020-06-17 06:15:53 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
if omniauth_disallow_totp
|
|
|
|
return {
|
|
|
|
omniauth_disallow_totp: !!omniauth_disallow_totp,
|
|
|
|
email: email
|
|
|
|
}
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|
2020-06-17 06:15:53 -04:00
|
|
|
|
|
|
|
if user
|
|
|
|
result = {
|
|
|
|
authenticated: !!authenticated,
|
|
|
|
awaiting_activation: !!awaiting_activation,
|
|
|
|
awaiting_approval: !!awaiting_approval,
|
|
|
|
not_allowed_from_ip_address: !!not_allowed_from_ip_address,
|
|
|
|
admin_not_allowed_from_ip_address: !!admin_not_allowed_from_ip_address
|
|
|
|
}
|
|
|
|
|
|
|
|
result[:destination_url] = destination_url if authenticated && destination_url.present?
|
|
|
|
|
|
|
|
return result
|
|
|
|
end
|
|
|
|
|
2021-07-12 19:15:06 -04:00
|
|
|
suggested_username = UserNameSuggester.suggest(username_suggester_attributes)
|
|
|
|
if email_valid && email.present?
|
|
|
|
if username.present? && User.username_available?(username, email)
|
|
|
|
suggested_username = username
|
|
|
|
elsif staged_user = User.where(staged: true).find_by_email(email)
|
|
|
|
suggested_username = staged_user.username
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-17 06:15:53 -04:00
|
|
|
result = {
|
|
|
|
email: email,
|
2021-07-12 19:15:06 -04:00
|
|
|
username: suggested_username,
|
2020-06-17 06:15:53 -04:00
|
|
|
auth_provider: authenticator_name,
|
|
|
|
email_valid: !!email_valid,
|
2020-06-18 06:01:02 -04:00
|
|
|
can_edit_username: can_edit_username,
|
|
|
|
can_edit_name: can_edit_name
|
2020-06-17 06:15:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
result[:destination_url] = destination_url if destination_url.present?
|
|
|
|
|
|
|
|
if SiteSetting.enable_names?
|
2020-06-18 06:01:02 -04:00
|
|
|
result[:name] = name.presence
|
|
|
|
result[:name] ||= User.suggest_name(username || email) if can_edit_name
|
2020-06-17 06:15:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|
2021-02-25 20:33:20 -05:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def username_suggester_attributes
|
|
|
|
username || name || email
|
|
|
|
end
|
2013-08-23 02:20:43 -04:00
|
|
|
end
|