Skip to content

Commit

Permalink
fix: 单用户 SyncDepartments 缺失逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Mmx233 committed Nov 9, 2023
1 parent 012b168 commit 8abe41d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/api/controllers/public/login/thirdPartyLogin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/api/controllers/public/login/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions internal/db/dao/UserGroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 15 additions & 1 deletion internal/pkg/feishu/User.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,28 @@ 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
}

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
Expand Down
6 changes: 5 additions & 1 deletion internal/service/UserGroups.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 8abe41d

Please sign in to comment.