2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-08-23 14:20:43 +08:00
|
|
|
class Auth::Result
|
2020-06-17 18:15:53 +08:00
|
|
|
ATTRIBUTES = %i[
|
|
|
|
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
|
|
|
|
skip_email_validation
|
|
|
|
destination_url
|
|
|
|
omniauth_disallow_totp
|
2017-11-09 23:49:12 +08:00
|
|
|
failed
|
|
|
|
failed_reason
|
2021-12-09 20:30:27 +08:00
|
|
|
failed_code
|
2021-12-23 18:53:17 +08:00
|
|
|
associated_groups
|
|
|
|
overrides_email
|
|
|
|
overrides_username
|
|
|
|
overrides_name
|
2020-06-17 18:15:53 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
attr_accessor *ATTRIBUTES
|
|
|
|
|
|
|
|
# These are stored in the session during
|
|
|
|
# account creation. The user cannot read or modify them
|
|
|
|
SESSION_ATTRIBUTES = %i[
|
|
|
|
email
|
|
|
|
username
|
|
|
|
email_valid
|
|
|
|
name
|
|
|
|
authenticator_name
|
|
|
|
extra_data
|
2021-12-09 20:30:27 +08:00
|
|
|
skip_email_validation
|
2021-12-23 18:53:17 +08:00
|
|
|
associated_groups
|
|
|
|
overrides_email
|
|
|
|
overrides_username
|
|
|
|
overrides_name
|
2020-06-17 18:15:53 +08:00
|
|
|
]
|
|
|
|
|
|
|
|
def [](key)
|
|
|
|
key = key.to_sym
|
|
|
|
public_send(key) if ATTRIBUTES.include?(key)
|
|
|
|
end
|
2015-06-25 00:12:43 +08:00
|
|
|
|
|
|
|
def initialize
|
|
|
|
@failed = false
|
|
|
|
end
|
|
|
|
|
2019-08-14 00:53:38 +08:00
|
|
|
def email
|
2019-08-14 19:08:59 +08:00
|
|
|
@email&.downcase
|
2019-08-14 00:53:38 +08:00
|
|
|
end
|
|
|
|
|
2021-02-22 20:05:36 +08:00
|
|
|
def email_valid=(val)
|
|
|
|
raise ArgumentError, "email_valid should be boolean or nil" if !val.in? [true, false, nil]
|
|
|
|
@email_valid = !!val
|
|
|
|
end
|
|
|
|
|
2015-06-25 00:12:43 +08:00
|
|
|
def failed?
|
|
|
|
!!@failed
|
|
|
|
end
|
|
|
|
|
2013-08-23 14:20:43 +08:00
|
|
|
def session_data
|
2020-06-17 18:15:53 +08:00
|
|
|
SESSION_ATTRIBUTES.map { |att| [att, public_send(att)] }.to_h
|
|
|
|
end
|
|
|
|
|
2020-06-18 18:01:02 +08:00
|
|
|
def self.from_session_data(data, user:)
|
2020-06-17 18:15:53 +08:00
|
|
|
result = new
|
2022-03-21 22:28:52 +08:00
|
|
|
data = data.with_indifferent_access
|
2020-06-17 18:15:53 +08:00
|
|
|
SESSION_ATTRIBUTES.each { |att| result.public_send("#{att}=", data[att]) }
|
2020-06-18 18:01:02 +08:00
|
|
|
result.user = user
|
2020-06-17 18:15:53 +08:00
|
|
|
result
|
2013-08-23 14:20:43 +08:00
|
|
|
end
|
|
|
|
|
2020-06-18 18:01:02 +08:00
|
|
|
def apply_user_attributes!
|
|
|
|
change_made = false
|
2022-05-04 18:22:09 +08:00
|
|
|
if (SiteSetting.auth_overrides_username? || overrides_username) &&
|
|
|
|
(resolved_username = resolve_username).present?
|
|
|
|
change_made = UsernameChanger.override(user, resolved_username)
|
2020-06-18 18:01:02 +08:00
|
|
|
end
|
|
|
|
|
2021-12-23 18:53:17 +08:00
|
|
|
if (
|
|
|
|
SiteSetting.auth_overrides_email || overrides_email || user&.email&.ends_with?(".invalid")
|
|
|
|
) && email_valid && email.present? && user.email != Email.downcase(email)
|
2020-06-18 18:01:02 +08:00
|
|
|
user.email = email
|
|
|
|
change_made = true
|
|
|
|
end
|
|
|
|
|
2021-12-23 18:53:17 +08:00
|
|
|
if (SiteSetting.auth_overrides_name || overrides_name) && name.present? && user.name != name
|
2020-06-18 18:01:02 +08:00
|
|
|
user.name = name
|
|
|
|
change_made = true
|
|
|
|
end
|
|
|
|
|
|
|
|
change_made
|
|
|
|
end
|
|
|
|
|
2021-12-09 20:30:27 +08:00
|
|
|
def apply_associated_attributes!
|
|
|
|
if authenticator&.provides_groups? && !associated_groups.nil?
|
|
|
|
associated_group_ids = []
|
|
|
|
|
|
|
|
associated_groups.uniq.each do |associated_group|
|
|
|
|
begin
|
|
|
|
associated_group =
|
|
|
|
AssociatedGroup.find_or_create_by(
|
|
|
|
name: associated_group[:name],
|
|
|
|
provider_id: associated_group[:id],
|
|
|
|
provider_name: extra_data[:provider],
|
|
|
|
)
|
|
|
|
rescue ActiveRecord::RecordNotUnique
|
|
|
|
retry
|
|
|
|
end
|
|
|
|
|
|
|
|
associated_group_ids.push(associated_group.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
user.update(associated_group_ids: associated_group_ids)
|
|
|
|
AssociatedGroup.where(id: associated_group_ids).update_all("last_used = CURRENT_TIMESTAMP")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-18 18:01:02 +08:00
|
|
|
def can_edit_name
|
2021-12-23 18:53:17 +08:00
|
|
|
!(SiteSetting.auth_overrides_name || overrides_name)
|
2020-06-18 18:01:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_edit_username
|
2021-12-23 18:53:17 +08:00
|
|
|
!(SiteSetting.auth_overrides_username || overrides_username)
|
2020-06-18 18:01:02 +08:00
|
|
|
end
|
|
|
|
|
2013-08-23 14:20:43 +08:00
|
|
|
def to_client_hash
|
2020-06-17 18:15:53 +08:00
|
|
|
return { requires_invite: true } if requires_invite
|
|
|
|
|
2021-07-20 18:42:08 +08:00
|
|
|
return { suspended: true, suspended_message: user.suspended_message } if user&.suspended?
|
2020-06-17 18:15:53 +08:00
|
|
|
|
|
|
|
if omniauth_disallow_totp
|
|
|
|
return { omniauth_disallow_totp: !!omniauth_disallow_totp, email: email }
|
2013-08-23 14:20:43 +08:00
|
|
|
end
|
2020-06-17 18:15:53 +08: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
|
|
|
|
|
|
|
|
result = {
|
|
|
|
email: email,
|
2021-12-06 19:06:35 +08:00
|
|
|
username: resolve_username,
|
2020-06-17 18:15:53 +08:00
|
|
|
auth_provider: authenticator_name,
|
|
|
|
email_valid: !!email_valid,
|
2020-06-18 18:01:02 +08:00
|
|
|
can_edit_username: can_edit_username,
|
|
|
|
can_edit_name: can_edit_name,
|
2020-06-17 18:15:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
result[:destination_url] = destination_url if destination_url.present?
|
|
|
|
|
|
|
|
if SiteSetting.enable_names?
|
2020-06-18 18:01:02 +08:00
|
|
|
result[:name] = name.presence
|
|
|
|
result[:name] ||= User.suggest_name(username || email) if can_edit_name
|
2020-06-17 18:15:53 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2013-08-23 14:20:43 +08:00
|
|
|
end
|
2021-02-26 09:33:20 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-12-06 19:06:35 +08:00
|
|
|
def staged_user
|
|
|
|
return @staged_user if defined?(@staged_user)
|
|
|
|
@staged_user = User.where(staged: true).find_by_email(email) if email.present? && email_valid
|
|
|
|
end
|
|
|
|
|
2021-02-26 09:33:20 +08:00
|
|
|
def username_suggester_attributes
|
2022-04-29 21:00:13 +08:00
|
|
|
attributes = [username]
|
|
|
|
attributes << name if SiteSetting.use_name_for_username_suggestions
|
2022-02-07 20:02:26 +08:00
|
|
|
attributes << email if SiteSetting.use_email_for_username_and_name_suggestions
|
|
|
|
attributes
|
2021-02-26 09:33:20 +08:00
|
|
|
end
|
2021-12-06 19:06:35 +08:00
|
|
|
|
2021-12-09 20:30:27 +08:00
|
|
|
def authenticator
|
|
|
|
@authenticator ||= Discourse.enabled_authenticators.find { |a| a.name == authenticator_name }
|
|
|
|
end
|
|
|
|
|
2021-12-06 19:06:35 +08:00
|
|
|
def resolve_username
|
|
|
|
if staged_user
|
|
|
|
if !username.present? || UserNameSuggester.fix_username(username) == staged_user.username
|
|
|
|
return staged_user.username
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-04 18:22:09 +08:00
|
|
|
UserNameSuggester.suggest(*username_suggester_attributes, current_username: user&.username)
|
2021-12-06 19:06:35 +08:00
|
|
|
end
|
2013-08-23 14:20:43 +08:00
|
|
|
end
|