diff --git a/internal/api/controllers/public/login/thirdPartyLogin.go b/internal/api/controllers/public/login/thirdPartyLogin.go index ab019896..bb69bb73 100644 --- a/internal/api/controllers/public/login/thirdPartyLogin.go +++ b/internal/api/controllers/public/login/thirdPartyLogin.go @@ -82,7 +82,7 @@ func checkUserPermission(c *gin.Context, user *dao.User, appCode string, permitA callback.Error(c, callback.ErrDBOperation, err) return } else if isCenterMember { - groups, err = service.UserGroups.GetForUser(user.ID) + groups, err = service.UserGroups.GetNamesForUser(user.ID) if err != nil { callback.Error(c, callback.ErrDBOperation, err) return diff --git a/internal/api/controllers/public/login/verify.go b/internal/api/controllers/public/login/verify.go index e94f38e3..af213262 100644 --- a/internal/api/controllers/public/login/verify.go +++ b/internal/api/controllers/public/login/verify.go @@ -128,7 +128,7 @@ func Login(c *gin.Context) { } userGroupSrv := service.UserGroupsSrv{DB: tx} - groups, err := userGroupSrv.GetForUser(claims.UID) + groups, err := userGroupSrv.GetNamesForUser(claims.UID) if err != nil { callback.Error(c, callback.ErrDBOperation, err) return diff --git a/internal/db/dao/UserGroups.go b/internal/db/dao/UserGroups.go index 3db400af..d7cd1f7e 100644 --- a/internal/db/dao/UserGroups.go +++ b/internal/db/dao/UserGroups.go @@ -46,6 +46,11 @@ func (a *UserGroups) GetUserGroupNamesByUID(tx *gorm.DB) ([]string, error) { return t, a.sqlGetUserGroupsByUID(tx).Select("base_groups.name").Find(&t).Error } +func (a *UserGroups) GetGetUserGroupIdsByUID(tx *gorm.DB) ([]uint, error) { + var t []uint + return t, a.sqlGetUserGroupsByUID(tx).Select("base_groups.id").Find(&t).Error +} + func (a *UserGroups) GetUserGroupsForShowByUID(tx *gorm.DB) ([]dto.Group, error) { var t = make([]dto.Group, 0) return t, a.sqlGetUserGroupsByUID(tx).Find(&t).Error diff --git a/internal/pkg/feishu/User.go b/internal/pkg/feishu/User.go index dc4307ed..a9f45c29 100644 --- a/internal/pkg/feishu/User.go +++ b/internal/pkg/feishu/User.go @@ -67,7 +67,7 @@ func (u User) SyncDepartments(tx *gorm.DB, uid uint, groupMap map[string]uint) e return err } - existDepartments, err := userGroupSrv.GetForUser(uid) + existDepartments, err := userGroupSrv.GetIdsForUser(uid) if err != nil { return err } @@ -75,6 +75,20 @@ func (u User) SyncDepartments(tx *gorm.DB, uid uint, groupMap map[string]uint) e gidToAddLength := len(departments) - len(existDepartments) if gidToAddLength > 0 { var gidToAdd = make([]uint, gidToAddLength) + var gidToAddIndex int + for _, gid := range departments { + for _, existGid := range existDepartments { + if existGid == gid { + goto next + } + } + gidToAdd[gidToAddIndex] = gid + gidToAddIndex++ + if gidToAddIndex == gidToAddLength { + break + } + next: + } err = userGroupSrv.CreateAll(u.genDepartmentModels(uid, gidToAdd)) if err != nil { return err diff --git a/internal/service/UserGroups.go b/internal/service/UserGroups.go index 66565f34..939bdce1 100644 --- a/internal/service/UserGroups.go +++ b/internal/service/UserGroups.go @@ -24,7 +24,11 @@ func (a UserGroupsSrv) IsCenterMember(uid uint) (bool, error) { }).ExistByName(a.DB, departments.UCe) } -func (a UserGroupsSrv) GetForUser(uid uint) ([]string, error) { +func (a UserGroupsSrv) GetIdsForUser(uid uint) ([]uint, error) { + return (&dao.UserGroups{UID: uid}).GetGetUserGroupIdsByUID(a.DB) +} + +func (a UserGroupsSrv) GetNamesForUser(uid uint) ([]string, error) { return (&dao.UserGroups{UID: uid}).GetUserGroupNamesByUID(a.DB) }