2021-12-15 17:45:11 +08:00
|
|
|
package object
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2022-09-29 19:44:08 +08:00
|
|
|
"github.com/beego/beego/logs"
|
2022-04-22 21:59:06 +08:00
|
|
|
"github.com/casdoor/casdoor/util"
|
2021-12-15 17:45:11 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type LdapAutoSynchronizer struct {
|
|
|
|
sync.Mutex
|
|
|
|
ldapIdToStopChan map[string]chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
var globalLdapAutoSynchronizer *LdapAutoSynchronizer
|
|
|
|
|
|
|
|
func InitLdapAutoSynchronizer() {
|
|
|
|
globalLdapAutoSynchronizer = NewLdapAutoSynchronizer()
|
|
|
|
globalLdapAutoSynchronizer.LdapAutoSynchronizerStartUpAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLdapAutoSynchronizer() *LdapAutoSynchronizer {
|
|
|
|
return &LdapAutoSynchronizer{
|
|
|
|
ldapIdToStopChan: make(map[string]chan struct{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetLdapAutoSynchronizer() *LdapAutoSynchronizer {
|
|
|
|
return globalLdapAutoSynchronizer
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// StartAutoSync
|
2022-08-07 12:26:14 +08:00
|
|
|
// start autosync for specified ldap, old existing autosync goroutine will be ceased
|
2021-12-15 17:45:11 +08:00
|
|
|
func (l *LdapAutoSynchronizer) StartAutoSync(ldapId string) error {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
|
|
|
|
ldap := GetLdap(ldapId)
|
|
|
|
if ldap == nil {
|
|
|
|
return fmt.Errorf("ldap %s doesn't exist", ldapId)
|
|
|
|
}
|
|
|
|
if res, ok := l.ldapIdToStopChan[ldapId]; ok {
|
|
|
|
res <- struct{}{}
|
|
|
|
delete(l.ldapIdToStopChan, ldapId)
|
|
|
|
}
|
|
|
|
|
|
|
|
stopChan := make(chan struct{})
|
|
|
|
l.ldapIdToStopChan[ldapId] = stopChan
|
|
|
|
logs.Info(fmt.Sprintf("autoSync started for %s", ldap.Id))
|
2022-08-07 12:26:14 +08:00
|
|
|
util.SafeGoroutine(func() { l.syncRoutine(ldap, stopChan) })
|
2021-12-15 17:45:11 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *LdapAutoSynchronizer) StopAutoSync(ldapId string) {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
if res, ok := l.ldapIdToStopChan[ldapId]; ok {
|
|
|
|
res <- struct{}{}
|
|
|
|
delete(l.ldapIdToStopChan, ldapId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-07 12:26:14 +08:00
|
|
|
// autosync goroutine
|
2021-12-15 17:45:11 +08:00
|
|
|
func (l *LdapAutoSynchronizer) syncRoutine(ldap *Ldap, stopChan chan struct{}) {
|
|
|
|
ticker := time.NewTicker(time.Duration(ldap.AutoSync) * time.Minute)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
2022-02-15 23:03:53 +08:00
|
|
|
select {
|
|
|
|
case <-stopChan:
|
|
|
|
logs.Info(fmt.Sprintf("autoSync goroutine for %s stopped", ldap.Id))
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
}
|
|
|
|
|
2021-12-15 17:45:11 +08:00
|
|
|
UpdateLdapSyncTime(ldap.Id)
|
2022-08-07 12:26:14 +08:00
|
|
|
// fetch all users
|
2023-03-15 11:12:31 +08:00
|
|
|
conn, err := ldap.GetLdapConn()
|
2021-12-15 17:45:11 +08:00
|
|
|
if err != nil {
|
|
|
|
logs.Warning(fmt.Sprintf("autoSync failed for %s, error %s", ldap.Id, err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-13 14:12:31 +08:00
|
|
|
users, err := conn.GetLdapUsers(ldap)
|
2021-12-15 17:45:11 +08:00
|
|
|
if err != nil {
|
|
|
|
logs.Warning(fmt.Sprintf("autoSync failed for %s, error %s", ldap.Id, err))
|
|
|
|
continue
|
|
|
|
}
|
2023-05-18 22:03:53 +08:00
|
|
|
|
|
|
|
existed, failed, err := SyncLdapUsers(ldap.Owner, LdapUsersToLdapRespUsers(users), ldap.Id)
|
|
|
|
if len(failed) != 0 {
|
|
|
|
logs.Warning(fmt.Sprintf("ldap autosync,%d new users,but %d user failed during :", len(users)-len(existed)-len(failed), len(failed)), failed)
|
|
|
|
logs.Warning(err.Error())
|
2021-12-15 17:45:11 +08:00
|
|
|
} else {
|
2023-05-18 22:03:53 +08:00
|
|
|
logs.Info(fmt.Sprintf("ldap autosync success, %d new users, %d existing users", len(users)-len(existed), len(existed)))
|
2021-12-15 17:45:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-09 16:50:49 +08:00
|
|
|
// LdapAutoSynchronizerStartUpAll
|
2022-08-07 12:26:14 +08:00
|
|
|
// start all autosync goroutine for existing ldap servers in each organizations
|
2021-12-15 17:45:11 +08:00
|
|
|
func (l *LdapAutoSynchronizer) LdapAutoSynchronizerStartUpAll() {
|
|
|
|
organizations := []*Organization{}
|
|
|
|
err := adapter.Engine.Desc("created_time").Find(&organizations)
|
|
|
|
if err != nil {
|
|
|
|
logs.Info("failed to Star up LdapAutoSynchronizer; ")
|
|
|
|
}
|
|
|
|
for _, org := range organizations {
|
|
|
|
for _, ldap := range GetLdaps(org.Name) {
|
|
|
|
if ldap.AutoSync != 0 {
|
|
|
|
l.StartAutoSync(ldap.Id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-13 14:12:31 +08:00
|
|
|
|
|
|
|
func UpdateLdapSyncTime(ldapId string) {
|
|
|
|
_, err := adapter.Engine.ID(ldapId).Update(&Ldap{LastSync: util.GetCurrentTime()})
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|