diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..5e36069 --- /dev/null +++ b/.pre-commit-config.yaml @@ -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 \ No newline at end of file diff --git a/logic/sqlToLdap_login.go b/logic/sqlToLdap_login.go index 1a2d0a6..7f2f77a 100644 --- a/logic/sqlToLdap_login.go +++ b/logic/sqlToLdap_login.go @@ -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 { @@ -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())) } @@ -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 { @@ -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())) } @@ -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 @@ -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 @@ -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) } diff --git a/service/isql/group_isql.go b/service/isql/group_isql.go index d3f530d..e16f945 100644 --- a/service/isql/group_isql.go +++ b/service/isql/group_isql.go @@ -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 diff --git a/service/isql/user_isql.go b/service/isql/user_isql.go index 1f18648..c0e024e 100644 --- a/service/isql/user_isql.go +++ b/service/isql/user_isql.go @@ -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) {