2021-07-24 04:16:34 -06:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-27 11:20:29 -07:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-07-24 04:16:34 -06:00
|
|
|
|
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-01-02 06:12:35 -07:00
|
|
|
"code.gitea.io/gitea/models/auth"
|
2021-11-09 22:13:16 -07:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-07-24 04:16:34 -06:00
|
|
|
"code.gitea.io/gitea/modules/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SyncExternalUsers is used to synchronize users with external authorization source
|
|
|
|
func SyncExternalUsers(ctx context.Context, updateExisting bool) error {
|
|
|
|
log.Trace("Doing: SyncExternalUsers")
|
|
|
|
|
2023-11-02 19:41:00 -06:00
|
|
|
ls, err := auth.FindSources(ctx, auth.FindSourcesOptions{})
|
2021-07-24 04:16:34 -06:00
|
|
|
if err != nil {
|
|
|
|
log.Error("SyncExternalUsers: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range ls {
|
|
|
|
if !s.IsActive || !s.IsSyncEnabled {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
log.Warn("SyncExternalUsers: Cancelled before update of %s", s.Name)
|
2021-11-09 22:13:16 -07:00
|
|
|
return db.ErrCancelledf("Before update of %s", s.Name)
|
2021-07-24 04:16:34 -06:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if syncable, ok := s.Cfg.(SynchronizableSource); ok {
|
|
|
|
err := syncable.Sync(ctx, updateExisting)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|