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