Skip to content

Commit

Permalink
fix: 修复自动任务会删除中间关系的问题 (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
eryajf authored Feb 25, 2023
1 parent a026d93 commit 4e8c57a
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
17 changes: 17 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.1.0
hooks:
- id: check-yaml
- id: trailing-whitespace
- id: check-added-large-files
- repo: https://github.com/golangci/golangci-lint # golangci-lint hook repo
rev: v1.47.3 # golangci-lint hook repo revision
hooks:
- id: golangci-lint
name: golangci-lint
description: Fast linters runner for Go.
entry: golangci-lint run --fix
types: [go]
language: golang
pass_filenames: false
40 changes: 21 additions & 19 deletions logic/sqlToLdap_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

type SqlLogic struct{}

// 同步sql的用户信息到ldap
// SyncSqlUsers 同步sql的用户信息到ldap
func (d *SqlLogic) SyncSqlUsers(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
r, ok := req.(*request.SyncSqlUserReq)
if !ok {
Expand Down Expand Up @@ -50,8 +50,7 @@ func (d *SqlLogic) SyncSqlUsers(c *gin.Context, req interface{}) (data interface
return nil, tools.NewMySqlError(fmt.Errorf("向Ldap添加用户到分组关系失败:" + err.Error()))
}
}
user.SyncState = 1
err = isql.User.Update(&user)
err = isql.User.ChangeSyncState(int(user.ID), 1)
if err != nil {
return nil, tools.NewLdapError(fmt.Errorf("用户同步完毕之后更新状态失败:" + err.Error()))
}
Expand All @@ -60,7 +59,7 @@ func (d *SqlLogic) SyncSqlUsers(c *gin.Context, req interface{}) (data interface
return nil, nil
}

// 同步sql中的分组信息到ldap
// SyncSqlGroups 同步sql中的分组信息到ldap
func (d *SqlLogic) SyncSqlGroups(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
r, ok := req.(*request.SyncSqlGrooupsReq)
if !ok {
Expand Down Expand Up @@ -95,8 +94,7 @@ func (d *SqlLogic) SyncSqlGroups(c *gin.Context, req interface{}) (data interfac
}
}
}
group.SyncState = 1
err = isql.Group.Update(group)
err = isql.Group.ChangeSyncState(int(group.ID), 1)
if err != nil {
return nil, tools.NewLdapError(fmt.Errorf("分组同步完毕之后更新状态失败:" + err.Error()))
}
Expand All @@ -105,7 +103,7 @@ func (d *SqlLogic) SyncSqlGroups(c *gin.Context, req interface{}) (data interfac
return nil, nil
}

// 检索未同步到ldap中的分组
// SearchGroupDiff 检索未同步到ldap中的分组
func SearchGroupDiff() (err error) {
// 获取sql中的数据
var sqlGroupList []*model.Group
Expand All @@ -125,13 +123,12 @@ func SearchGroupDiff() (err error) {
if group.GroupDN == config.Conf.Ldap.BaseDN {
continue
}
group.SyncState = 2
err = isql.Group.Update(group)
err = isql.Group.ChangeSyncState(int(group.ID), 2)
}
return
}

// 检索未同步到ldap中的用户
// SearchUserDiff 检索未同步到ldap中的用户
func SearchUserDiff() (err error) {
// 获取sql中的数据
var sqlUserList []*model.User
Expand All @@ -148,34 +145,39 @@ func SearchUserDiff() (err error) {
// 比对两个系统中的数据
users := diffUser(sqlUserList, ldapUserList)
for _, user := range users {
user.SyncState = 2
err = isql.User.Update(user)
if user.UserDN == config.Conf.Ldap.AdminDN {
continue
}
err = isql.User.ChangeSyncState(int(user.ID), 2)
}
return
}

func diffGroup(a, b []*model.Group) (rst []*model.Group) {
// diffGroup 比较出sql中有但ldap中没有的group列表
func diffGroup(sqlGroup, ldapGroup []*model.Group) (rst []*model.Group) {
var tmp = make(map[string]struct{}, 0)

for _, v := range b {
for _, v := range ldapGroup {
tmp[v.GroupDN] = struct{}{}
}

for _, v := range a {
for _, v := range sqlGroup {
if _, ok := tmp[v.GroupDN]; !ok {
rst = append(rst, v)
}
}
return
}
func diffUser(a, b []*model.User) (rst []*model.User) {
var tmp = make(map[string]struct{}, len(a))

for _, v := range b {
// diffUser 比较出sql中有但ldap中没有的user列表
func diffUser(sqlUser, ldapUser []*model.User) (rst []*model.User) {
var tmp = make(map[string]struct{}, len(sqlUser))

for _, v := range ldapUser {
tmp[v.UserDN] = struct{}{}
}

for _, v := range a {
for _, v := range sqlUser {
if _, ok := tmp[v.UserDN]; !ok {
rst = append(rst, v)
}
Expand Down
5 changes: 5 additions & 0 deletions service/isql/group_isql.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ func (s GroupService) Update(dataObj *model.Group) error {
return common.DB.Model(dataObj).Where("id = ?", dataObj.ID).Updates(dataObj).Error
}

// ChangeSyncState 更新分组的同步状态
func (s GroupService) ChangeSyncState(id, status int) error {
return common.DB.Model(&model.Group{}).Where("id = ?", id).Update("sync_state", status).Error
}

// Find 获取单个资源
func (s GroupService) Find(filter map[string]interface{}, data *model.Group, args ...interface{}) error {
return common.DB.Where(filter, args).Preload("Users").First(&data).Error
Expand Down
5 changes: 5 additions & 0 deletions service/isql/user_isql.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ func (s UserService) ChangeStatus(id, status int) error {
return common.DB.Model(&model.User{}).Where("id = ?", id).Update("status", status).Error
}

// ChangeSyncState 更新用户的同步状态
func (s UserService) ChangeSyncState(id, status int) error {
return common.DB.Model(&model.User{}).Where("id = ?", id).Update("sync_state", status).Error
}

// GetCurrentLoginUser 获取当前登录用户信息
// 需要缓存,减少数据库访问
func (s UserService) GetCurrentLoginUser(c *gin.Context) (model.User, error) {
Expand Down

0 comments on commit 4e8c57a

Please sign in to comment.