2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-08-23 14:20:43 +08:00
|
|
|
class Auth::Result
|
2019-06-11 09:28:42 +08:00
|
|
|
attr_accessor :user, :name, :username, :email,
|
2013-08-23 14:20:43 +08:00
|
|
|
:email_valid, :extra_data, :awaiting_activation,
|
2013-08-28 15:18:31 +08:00
|
|
|
:awaiting_approval, :authenticated, :authenticator_name,
|
2015-03-03 01:13:10 +08:00
|
|
|
:requires_invite, :not_allowed_from_ip_address,
|
2016-09-08 02:05:46 +08:00
|
|
|
:admin_not_allowed_from_ip_address, :omit_username,
|
2018-03-01 15:47:07 +08:00
|
|
|
:skip_email_validation, :destination_url, :omniauth_disallow_totp
|
2013-08-23 14:20:43 +08:00
|
|
|
|
2017-11-09 23:49:12 +08:00
|
|
|
attr_accessor(
|
|
|
|
:failed,
|
|
|
|
:failed_reason,
|
|
|
|
:failed_code
|
|
|
|
)
|
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
|
|
|
|
|
2015-06-25 00:12:43 +08:00
|
|
|
def failed?
|
|
|
|
!!@failed
|
|
|
|
end
|
|
|
|
|
2013-08-23 14:20:43 +08:00
|
|
|
def session_data
|
2015-06-25 00:12:43 +08:00
|
|
|
{ email: email,
|
2013-08-23 14:20:43 +08:00
|
|
|
username: username,
|
|
|
|
email_valid: email_valid,
|
2015-06-27 03:52:05 +08:00
|
|
|
omit_username: omit_username,
|
2013-08-23 14:20:43 +08:00
|
|
|
name: name,
|
|
|
|
authenticator_name: authenticator_name,
|
2016-09-08 02:05:46 +08:00
|
|
|
extra_data: extra_data,
|
|
|
|
skip_email_validation: !!skip_email_validation }
|
2013-08-23 14:20:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_client_hash
|
2013-08-28 15:18:31 +08:00
|
|
|
if requires_invite
|
|
|
|
{ requires_invite: true }
|
|
|
|
elsif user
|
2015-01-20 01:36:56 +08:00
|
|
|
if user.suspended?
|
|
|
|
{
|
|
|
|
suspended: true,
|
2017-07-28 09:20:09 +08:00
|
|
|
suspended_message: I18n.t(user.suspend_reason ? "login.suspended_with_reason" : "login.suspended",
|
|
|
|
date: I18n.l(user.suspended_till, format: :date_only), reason: user.suspend_reason)
|
2015-01-20 01:36:56 +08:00
|
|
|
}
|
|
|
|
else
|
2018-03-01 15:47:07 +08:00
|
|
|
result =
|
|
|
|
if omniauth_disallow_totp
|
|
|
|
{
|
|
|
|
omniauth_disallow_totp: !!omniauth_disallow_totp,
|
|
|
|
email: email
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-01-27 01:52:27 +08:00
|
|
|
result[:destination_url] = destination_url if authenticated && destination_url.present?
|
|
|
|
result
|
2015-01-20 01:36:56 +08:00
|
|
|
end
|
2013-08-23 14:20:43 +08:00
|
|
|
else
|
2015-10-30 00:19:09 +08:00
|
|
|
result = { email: email,
|
|
|
|
username: UserNameSuggester.suggest(username || name || email),
|
2018-11-23 03:12:04 +08:00
|
|
|
auth_provider: authenticator_name,
|
2015-10-30 00:19:09 +08:00
|
|
|
email_valid: !!email_valid,
|
|
|
|
omit_username: !!omit_username }
|
|
|
|
|
2018-09-05 03:16:54 +08:00
|
|
|
result[:destination_url] = destination_url if destination_url.present?
|
|
|
|
|
2015-10-30 00:19:09 +08:00
|
|
|
if SiteSetting.enable_names?
|
2019-05-13 14:45:24 +08:00
|
|
|
result[:name] = name.presence || User.suggest_name(username || email)
|
2015-10-30 00:19:09 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
result
|
2013-08-23 14:20:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|