UpdateUserEmail updates the user's email to one of the identity's email if the current email used doesn't match any of the identities email
(tx *storage.Connection)
| 274 | // UpdateUserEmail updates the user's email to one of the identity's email |
| 275 | // if the current email used doesn't match any of the identities email |
| 276 | func (u *User) UpdateUserEmailFromIdentities(tx *storage.Connection) error { |
| 277 | identities, terr := FindIdentitiesByUserID(tx, u.ID) |
| 278 | if terr != nil { |
| 279 | return terr |
| 280 | } |
| 281 | for _, i := range identities { |
| 282 | if u.GetEmail() == i.GetEmail() { |
| 283 | // there's an existing identity that uses the same email |
| 284 | // so the user's email can be kept |
| 285 | return nil |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | var primaryIdentity *Identity |
| 290 | for _, i := range identities { |
| 291 | if _, terr := FindUserByEmailAndAudience(tx, i.GetEmail(), u.Aud); terr != nil { |
| 292 | if IsNotFoundError(terr) { |
| 293 | // the identity's email is not used by another user |
| 294 | // so we can set it as the primary identity |
| 295 | primaryIdentity = i |
| 296 | break |
| 297 | } |
| 298 | return terr |
| 299 | } |
| 300 | } |
| 301 | if primaryIdentity == nil { |
| 302 | return UserEmailUniqueConflictError{} |
| 303 | } |
| 304 | // default to the first identity's email |
| 305 | if terr := u.SetEmail(tx, primaryIdentity.GetEmail()); terr != nil { |
| 306 | return terr |
| 307 | } |
| 308 | if primaryIdentity.GetEmail() == "" { |
| 309 | u.EmailConfirmedAt = nil |
| 310 | if terr := tx.UpdateOnly(u, "email_confirmed_at"); terr != nil { |
| 311 | return terr |
| 312 | } |
| 313 | } |
| 314 | return nil |
| 315 | } |
| 316 | |
| 317 | // SetEmail sets the user's email |
| 318 | func (u *User) SetEmail(tx *storage.Connection, email string) error { |