2021-07-24 18:16:34 +08:00
|
|
|
// Copyright 2021 The Gitea Authors. All rights reserved.
|
2022-11-28 02:20:29 +08:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-07-24 18:16:34 +08:00
|
|
|
|
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-01-02 21:12:35 +08:00
|
|
|
"code.gitea.io/gitea/models/auth"
|
2021-11-10 13:13:16 +08:00
|
|
|
"code.gitea.io/gitea/models/db"
|
2021-07-24 18:16:34 +08: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-03 09:41:00 +08:00
|
|
|
ls, err := auth.FindSources(ctx, auth.FindSourcesOptions{})
|
2021-07-24 18:16:34 +08: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-10 13:13:16 +08:00
|
|
|
return db.ErrCancelledf("Before update of %s", s.Name)
|
2021-07-24 18:16:34 +08:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if syncable, ok := s.Cfg.(SynchronizableSource); ok {
|
|
|
|
err := syncable.Sync(ctx, updateExisting)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|