@@ -364,10 +364,7 @@ func (a *Auth) applySignUpExternal(context storage.TransactionContext, supported
364364 return nil , errors .WrapErrorAction (logutils .ActionRegister , model .TypeAccount , nil , err )
365365 }
366366
367- externalAuthTypeID := account .AuthTypes [0 ].ID
368367 for i , id := range account .Identifiers {
369- account .Identifiers [i ].AccountAuthTypeID = & externalAuthTypeID
370-
371368 primary := (id .Identifier == externalUser .Identifier )
372369 account .Identifiers [i ].Primary = & primary
373370 }
@@ -391,10 +388,7 @@ func (a *Auth) applySignUpAdminExternal(context storage.TransactionContext, auth
391388 return nil , errors .WrapErrorAction (logutils .ActionRegister , "admin account" , nil , err )
392389 }
393390
394- externalAuthTypeID := account .AuthTypes [0 ].ID
395391 for i , id := range account .Identifiers {
396- account .Identifiers [i ].AccountAuthTypeID = & externalAuthTypeID
397-
398392 primary := (id .Identifier == externalUser .Identifier )
399393 account .Identifiers [i ].Primary = & primary
400394 }
@@ -440,12 +434,17 @@ func (a *Auth) prepareExternalUserData(authType model.AuthType, appOrg model.App
440434 params := map [string ]interface {}{"user" : externalUser }
441435
442436 //3. create the account identifiers
437+ now := time .Now ().UTC ()
443438 accountID := uuid .NewString ()
444439 accountIdentifiers := make ([]model.AccountIdentifier , 0 )
445440 for k , v := range externalUser .ExternalIDs {
446441 primary := (v == externalUser .Identifier )
447442 accountIdentifiers = append (accountIdentifiers , model.AccountIdentifier {ID : uuid .NewString (), Code : k , Identifier : v ,
448- Verified : true , Primary : & primary , Account : model.Account {ID : accountID }, DateCreated : time .Now ().UTC ()})
443+ Verified : true , Primary : & primary , Account : model.Account {ID : accountID }, DateCreated : now })
444+ }
445+ if externalUser .Email != "" {
446+ accountIdentifiers = append (accountIdentifiers , model.AccountIdentifier {ID : uuid .NewString (), Code : IdentifierTypeEmail , Identifier : externalUser .Email ,
447+ Account : model.Account {ID : accountID }, DateCreated : now })
449448 }
450449
451450 return accountIdentifiers , params , & profile , preferences , nil
@@ -515,6 +514,7 @@ func (a *Auth) updateExternalUserIfNeeded(accountAuthType model.AccountAuthType,
515514 now := time .Now ().UTC ()
516515 accountAuthType .DateUpdated = & now
517516
517+ //TODO: make sure external identifiers get updated in storage and in account memory
518518 transaction := func (context storage.TransactionContext ) error {
519519 //1. first find the account record
520520 account , err := a .storage .FindAccountByAuthTypeID (context , accountAuthType .ID )
@@ -2559,12 +2559,13 @@ func (a *Auth) updateExternalAccountGroups(account *model.Account, newExternalGr
25592559
25602560func (a * Auth ) updateExternalIdentifiers (account * model.Account , accountAuthTypeID string , externalUser * model.ExternalSystemUser , linked bool ) bool {
25612561 updated := false
2562+ now := time .Now ().UTC ()
25622563 for k , v := range externalUser .ExternalIDs {
25632564 accountIdentifier := account .GetAccountIdentifier (k , "" )
25642565 if accountIdentifier == nil {
25652566 primary := (v == externalUser .Identifier )
25662567 newIdentifier := model.AccountIdentifier {ID : uuid .NewString (), Code : k , Identifier : v , Verified : true , Linked : linked , AccountAuthTypeID : & accountAuthTypeID ,
2567- Primary : & primary , Account : model.Account {ID : account .ID }, DateCreated : time . Now (). UTC () }
2568+ Primary : & primary , Account : model.Account {ID : account .ID }, DateCreated : now }
25682569 account .Identifiers = append (account .Identifiers , newIdentifier )
25692570 updated = true
25702571 } else if accountIdentifier .Identifier != v {
@@ -2575,6 +2576,25 @@ func (a *Auth) updateExternalIdentifiers(account *model.Account, accountAuthType
25752576 }
25762577 }
25772578
2579+ if externalUser .Email != "" {
2580+ hasExternalEmail := false
2581+ for i , identifier := range account .Identifiers {
2582+ if identifier .AccountAuthTypeID != nil && * identifier .AccountAuthTypeID == accountAuthTypeID && identifier .Code == IdentifierTypeEmail {
2583+ hasExternalEmail = true
2584+ if identifier .Identifier != externalUser .Email {
2585+ account .Identifiers [i ].Identifier = externalUser .Email
2586+ updated = true
2587+ break
2588+ }
2589+ }
2590+ }
2591+ if ! hasExternalEmail {
2592+ primary := (externalUser .Email == externalUser .Identifier )
2593+ account .Identifiers = append (account .Identifiers , model.AccountIdentifier {ID : uuid .NewString (), Code : IdentifierTypeEmail , Identifier : externalUser .Email ,
2594+ Linked : linked , Sensitive : true , AccountAuthTypeID : & accountAuthTypeID , Primary : & primary , Account : model.Account {ID : account .ID }, DateCreated : now })
2595+ }
2596+ }
2597+
25782598 return updated
25792599}
25802600
0 commit comments