Skip to content

Commit eeb0c87

Browse files
authored
ldap-sync: fix creation of only one user per LDAP sync (#375)
Before this fix, a too early `return` statement terminated the `updateLdapUsers()` function, whenever one not already existing user was created. Therefore, in each LDAP sync a maximum of one new user could be created (i.e., it took x LDAP sync cycles until x new LDAP users are registered in wg-portal). Depending on the LDAP `sync_interval` this can take a long time and produces unecessary long waiting times until users are available in wg-portal. Removing the early return statement, and move the remainder of the function into an `else` statement, so that all new users can be added in a single LDAP sync. Also adding a debug statement to better trace the behavior. Signed-off-by: klmmr <[email protected]>
1 parent 67f076e commit eeb0c87

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

internal/app/users/user_manager.go

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -506,50 +506,49 @@ func (m Manager) updateLdapUsers(
506506
tctx, cancel := context.WithTimeout(ctx, 30*time.Second)
507507
tctx = domain.SetUserInfo(tctx, domain.SystemAdminContextUserInfo())
508508

509-
// create new user
510509
if existingUser == nil {
510+
// create new user
511+
logrus.Tracef("creating new user %s from provider %s...", user.Identifier, provider.ProviderName)
512+
511513
err := m.NewUser(tctx, user)
512514
if err != nil {
513515
cancel()
514516
return fmt.Errorf("create error for user id %s: %w", user.Identifier, err)
515517
}
516-
517-
cancel()
518-
return nil
519-
}
520-
521-
// update existing user
522-
if provider.AutoReEnable && existingUser.DisabledReason == domain.DisabledReasonLdapMissing {
523-
user.Disabled = nil
524-
user.DisabledReason = ""
525518
} else {
526-
user.Disabled = existingUser.Disabled
527-
user.DisabledReason = existingUser.DisabledReason
528-
}
529-
if existingUser.Source == domain.UserSourceLdap && userChangedInLdap(existingUser, user) {
530-
err := m.users.SaveUser(tctx, user.Identifier, func(u *domain.User) (*domain.User, error) {
531-
u.UpdatedAt = time.Now()
532-
u.UpdatedBy = domain.CtxSystemLdapSyncer
533-
u.Source = user.Source
534-
u.ProviderName = user.ProviderName
535-
u.Email = user.Email
536-
u.Firstname = user.Firstname
537-
u.Lastname = user.Lastname
538-
u.Phone = user.Phone
539-
u.Department = user.Department
540-
u.IsAdmin = user.IsAdmin
541-
u.Disabled = nil
542-
u.DisabledReason = ""
543-
544-
return u, nil
545-
})
546-
if err != nil {
547-
cancel()
548-
return fmt.Errorf("update error for user id %s: %w", user.Identifier, err)
519+
// update existing user
520+
if provider.AutoReEnable && existingUser.DisabledReason == domain.DisabledReasonLdapMissing {
521+
user.Disabled = nil
522+
user.DisabledReason = ""
523+
} else {
524+
user.Disabled = existingUser.Disabled
525+
user.DisabledReason = existingUser.DisabledReason
549526
}
527+
if existingUser.Source == domain.UserSourceLdap && userChangedInLdap(existingUser, user) {
528+
err := m.users.SaveUser(tctx, user.Identifier, func(u *domain.User) (*domain.User, error) {
529+
u.UpdatedAt = time.Now()
530+
u.UpdatedBy = domain.CtxSystemLdapSyncer
531+
u.Source = user.Source
532+
u.ProviderName = user.ProviderName
533+
u.Email = user.Email
534+
u.Firstname = user.Firstname
535+
u.Lastname = user.Lastname
536+
u.Phone = user.Phone
537+
u.Department = user.Department
538+
u.IsAdmin = user.IsAdmin
539+
u.Disabled = nil
540+
u.DisabledReason = ""
541+
542+
return u, nil
543+
})
544+
if err != nil {
545+
cancel()
546+
return fmt.Errorf("update error for user id %s: %w", user.Identifier, err)
547+
}
550548

551-
if existingUser.IsDisabled() && !user.IsDisabled() {
552-
m.bus.Publish(app.TopicUserEnabled, *user)
549+
if existingUser.IsDisabled() && !user.IsDisabled() {
550+
m.bus.Publish(app.TopicUserEnabled, *user)
551+
}
553552
}
554553
}
555554

0 commit comments

Comments
 (0)