Class: Auth::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/auth/result.rb

Constant Summary collapse

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
  failed
  failed_reason
  failed_code
  associated_groups
  overrides_email
  overrides_username
  overrides_name
]
SESSION_ATTRIBUTES =

These are stored in the session during account creation. The user cannot read or modify them

%i[
  email
  username
  email_valid
  name
  authenticator_name
  extra_data
  skip_email_validation
  associated_groups
  overrides_email
  overrides_username
  overrides_name
]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResult

Returns a new instance of Result.



53
54
55
# File 'lib/auth/result.rb', line 53

def initialize
  @failed = false
end

Class Method Details

.from_session_data(data, user:) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/auth/result.rb', line 74

def self.from_session_data(data, user:)
  result = new
  data = data.with_indifferent_access
  SESSION_ATTRIBUTES.each { |att| result.public_send("#{att}=", data[att]) }
  result.user = user
  result
end

Instance Method Details

#[](key) ⇒ Object



48
49
50
51
# File 'lib/auth/result.rb', line 48

def [](key)
  key = key.to_sym
  public_send(key) if ATTRIBUTES.include?(key)
end

#apply_associated_attributes!Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/auth/result.rb', line 104

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

#apply_user_attributes!Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/auth/result.rb', line 82

def apply_user_attributes!
  change_made = false
  if (SiteSetting.auth_overrides_username? || overrides_username) &&
       (resolved_username = resolve_username).present?
    change_made = UsernameChanger.override(user, resolved_username)
  end

  if (
       SiteSetting.auth_overrides_email || overrides_email || user&.email&.ends_with?(".invalid")
     ) && email_valid && email.present? && user.email != Email.downcase(email)
    user.email = email
    change_made = true
  end

  if (SiteSetting.auth_overrides_name || overrides_name) && name.present? && user.name != name
    user.name = name
    change_made = true
  end

  change_made
end

#can_edit_nameObject



128
129
130
# File 'lib/auth/result.rb', line 128

def can_edit_name
  !(SiteSetting.auth_overrides_name || overrides_name)
end

#can_edit_usernameObject



132
133
134
# File 'lib/auth/result.rb', line 132

def can_edit_username
  !(SiteSetting.auth_overrides_username || overrides_username)
end

#emailObject



57
58
59
# File 'lib/auth/result.rb', line 57

def email
  @email&.downcase
end

#email_valid=(val) ⇒ Object

Raises:

  • (ArgumentError)


61
62
63
64
# File 'lib/auth/result.rb', line 61

def email_valid=(val)
  raise ArgumentError, "email_valid should be boolean or nil" if !val.in? [true, false, nil]
  @email_valid = !!val
end

#failed?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/auth/result.rb', line 66

def failed?
  !!@failed
end

#session_dataObject



70
71
72
# File 'lib/auth/result.rb', line 70

def session_data
  SESSION_ATTRIBUTES.map { |att| [att, public_send(att)] }.to_h
end

#to_client_hashObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/auth/result.rb', line 136

def to_client_hash
  return { requires_invite: true } if requires_invite

  return { suspended: true, suspended_message: user.suspended_message } if user&.suspended?

  if omniauth_disallow_totp
    return { omniauth_disallow_totp: !!omniauth_disallow_totp, email: email }
  end

  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,
    username: resolve_username,
    auth_provider: authenticator_name,
    email_valid: !!email_valid,
    can_edit_username: can_edit_username,
    can_edit_name: can_edit_name,
  }

  result[:destination_url] = destination_url if destination_url.present?

  if SiteSetting.enable_names?
    result[:name] = name.presence
    result[:name] ||= User.suggest_name(username || email) if can_edit_name
  end

  result
end