From e3cbc1f165e175539e159179b30d27121f3174d0 Mon Sep 17 00:00:00 2001 From: Karl Persson Date: Tue, 28 Feb 2023 13:34:15 +0100 Subject: [PATCH] AuthN: Fix issue with duplicated auth connection (#63836) AuthN: Fix issue with duplicated auth connection when user signed in first time --- .../authn/authnimpl/sync/user_sync.go | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/pkg/services/authn/authnimpl/sync/user_sync.go b/pkg/services/authn/authnimpl/sync/user_sync.go index 1210274ac1b..5805b7deef0 100644 --- a/pkg/services/authn/authnimpl/sync/user_sync.go +++ b/pkg/services/authn/authnimpl/sync/user_sync.go @@ -83,16 +83,12 @@ func (s *UserSync) SyncUserHook(ctx context.Context, id *authn.Identity, _ *auth s.log.FromContext(ctx).Error("Failed to create user", "error", errCreate, "auth_module", id.AuthModule, "auth_id", id.AuthID) return errSyncUserInternal.Errorf("unable to create user") } - } - - if errProtection := s.userProtectionService.AllowUserMapping(usr, id.AuthModule); errProtection != nil { - return errUserProtection.Errorf("user mapping not allowed: %w", errProtection) - } - - // update user - if errUpdate := s.updateUserAttributes(ctx, usr, id, userAuth); errUpdate != nil { - s.log.FromContext(ctx).Error("Failed to update user", "error", errUpdate, "auth_module", id.AuthModule, "auth_id", id.AuthID) - return errSyncUserInternal.Errorf("unable to update user") + } else { + // update user + if errUpdate := s.updateUserAttributes(ctx, usr, id, userAuth); errUpdate != nil { + s.log.FromContext(ctx).Error("Failed to update user", "error", errUpdate, "auth_module", id.AuthModule, "auth_id", id.AuthID) + return errSyncUserInternal.Errorf("unable to update user") + } } syncUserToIdentity(usr, id) @@ -191,6 +187,9 @@ func (s *UserSync) upsertAuthConnection(ctx context.Context, userID int64, ident } func (s *UserSync) updateUserAttributes(ctx context.Context, usr *user.User, id *authn.Identity, userAuth *login.UserAuth) error { + if errProtection := s.userProtectionService.AllowUserMapping(usr, id.AuthModule); errProtection != nil { + return errUserProtection.Errorf("user mapping not allowed: %w", errProtection) + } // sync user info updateCmd := &user.UpdateUserCommand{ UserID: usr.ID,