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