mirror of
https://github.com/casdoor/casdoor.git
synced 2025-07-03 04:10:20 +08:00
feat: return most backend API errors to frontend (#1836)
* feat: return most backend API errros to frontend Signed-off-by: yehong <239859435@qq.com> * refactor: reduce int type change Signed-off-by: yehong <239859435@qq.com> * feat: return err backend in token.go Signed-off-by: yehong <239859435@qq.com> --------- Signed-off-by: yehong <239859435@qq.com>
This commit is contained in:
@ -18,7 +18,10 @@ var globalLdapAutoSynchronizer *LdapAutoSynchronizer
|
||||
|
||||
func InitLdapAutoSynchronizer() {
|
||||
globalLdapAutoSynchronizer = NewLdapAutoSynchronizer()
|
||||
globalLdapAutoSynchronizer.LdapAutoSynchronizerStartUpAll()
|
||||
err := globalLdapAutoSynchronizer.LdapAutoSynchronizerStartUpAll()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
func NewLdapAutoSynchronizer() *LdapAutoSynchronizer {
|
||||
@ -37,7 +40,11 @@ func (l *LdapAutoSynchronizer) StartAutoSync(ldapId string) error {
|
||||
l.Lock()
|
||||
defer l.Unlock()
|
||||
|
||||
ldap := GetLdap(ldapId)
|
||||
ldap, err := GetLdap(ldapId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ldap == nil {
|
||||
return fmt.Errorf("ldap %s doesn't exist", ldapId)
|
||||
}
|
||||
@ -49,7 +56,12 @@ func (l *LdapAutoSynchronizer) StartAutoSync(ldapId string) error {
|
||||
stopChan := make(chan struct{})
|
||||
l.ldapIdToStopChan[ldapId] = stopChan
|
||||
logs.Info(fmt.Sprintf("autoSync started for %s", ldap.Id))
|
||||
util.SafeGoroutine(func() { l.syncRoutine(ldap, stopChan) })
|
||||
util.SafeGoroutine(func() {
|
||||
err := l.syncRoutine(ldap, stopChan)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -63,18 +75,22 @@ func (l *LdapAutoSynchronizer) StopAutoSync(ldapId string) {
|
||||
}
|
||||
|
||||
// autosync goroutine
|
||||
func (l *LdapAutoSynchronizer) syncRoutine(ldap *Ldap, stopChan chan struct{}) {
|
||||
func (l *LdapAutoSynchronizer) syncRoutine(ldap *Ldap, stopChan chan struct{}) error {
|
||||
ticker := time.NewTicker(time.Duration(ldap.AutoSync) * time.Minute)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-stopChan:
|
||||
logs.Info(fmt.Sprintf("autoSync goroutine for %s stopped", ldap.Id))
|
||||
return
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
}
|
||||
|
||||
UpdateLdapSyncTime(ldap.Id)
|
||||
err := UpdateLdapSyncTime(ldap.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// fetch all users
|
||||
conn, err := ldap.GetLdapConn()
|
||||
if err != nil {
|
||||
@ -100,24 +116,35 @@ func (l *LdapAutoSynchronizer) syncRoutine(ldap *Ldap, stopChan chan struct{}) {
|
||||
|
||||
// LdapAutoSynchronizerStartUpAll
|
||||
// start all autosync goroutine for existing ldap servers in each organizations
|
||||
func (l *LdapAutoSynchronizer) LdapAutoSynchronizerStartUpAll() {
|
||||
func (l *LdapAutoSynchronizer) LdapAutoSynchronizerStartUpAll() error {
|
||||
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) {
|
||||
ldaps, err := GetLdaps(org.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, ldap := range ldaps {
|
||||
if ldap.AutoSync != 0 {
|
||||
l.StartAutoSync(ldap.Id)
|
||||
err = l.StartAutoSync(ldap.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func UpdateLdapSyncTime(ldapId string) {
|
||||
func UpdateLdapSyncTime(ldapId string) error {
|
||||
_, err := adapter.Engine.ID(ldapId).Update(&Ldap{LastSync: util.GetCurrentTime()})
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user