Prechádzať zdrojové kódy

fix: user表新增字段

tuonian 3 týždňov pred
rodič
commit
edc8863ea1

+ 20 - 0
server/modules/ldap/extends.go

@@ -0,0 +1,20 @@
+package ldap
+
+import (
+	"errors"
+	"github.com/astaxie/beego/orm"
+)
+
+func InsertOrUpdate(o orm.Ormer, md interface{}, colConflitAndArgs ...string) (int64, error) {
+	if o == nil {
+		o = orm.NewOrm()
+	}
+
+	err := o.Read(md, colConflitAndArgs...)
+	if err != nil && errors.Is(err, orm.ErrNoRows) {
+		return o.Insert(md)
+	} else if err != nil {
+		return 0, err
+	}
+	return o.Update(md)
+}

+ 17 - 4
server/modules/ldap/user_service.go

@@ -24,7 +24,20 @@ func (c *UserService) Add(body *models.LdapUser) (*models.LdapUser, error) {
 		return nil, err
 	}
 	body.Uid = server.Uid
-	_, err = o.InsertOrUpdate(body)
+
+	exist := models.LdapUser{
+		Account: body.Account,
+	}
+	err = o.Read(&exist, "Account")
+	if err != nil && errors.Is(err, orm.ErrNoRows) {
+		_, err = o.Insert(&body)
+	} else if err != nil {
+		return nil, err
+	} else {
+		body.Id = exist.Id
+		_, err = o.Update(body)
+	}
+
 	if err != nil {
 		return nil, err
 	}
@@ -151,13 +164,13 @@ func (c *UserService) SyncUsers(current *models.User, req *LDAPUserSyncReq) (int
 	}
 	for _, user := range users {
 		user.Uid = string(rune(current.Id))
-		_, err := o.InsertOrUpdate(user, "DN")
+		_, err := InsertOrUpdate(o, user, "DN")
 		if err != nil {
 			logs.Error("save user fail: %v", err)
 		}
 	}
 	for _, organize := range organizeList {
-		_, err = o.InsertOrUpdate(organize, "DN")
+		_, err = InsertOrUpdate(o, organize, "DN")
 		if err != nil {
 			logs.Error("save organize fail: %v", err)
 		}
@@ -182,7 +195,7 @@ func (c *UserService) Authentication(server *models.LdapServer, account string,
 			return nil, errors.New("您输入的账号或者密码错误!")
 		}
 		ldapUser = users[0]
-		_, err = o.InsertOrUpdate(ldapUser, "DN")
+		_, err = InsertOrUpdate(o, ldapUser, "DN")
 		if err != nil {
 			return nil, err
 		}