fix: 调整原ldap同步逻辑,同步之后与原来完全对齐 (#61)
This commit is contained in:
parent
637d6b2bbe
commit
f46c19510d
|
@ -25,7 +25,7 @@ func (d *OpenLdapLogic) SyncOpenLdapDepts(c *gin.Context, req interface{}) (data
|
||||||
var firstDepts []*openldap.Dept // 父ID为根的部门
|
var firstDepts []*openldap.Dept // 父ID为根的部门
|
||||||
var otherDepts []*openldap.Dept // 父ID不为根的部门
|
var otherDepts []*openldap.Dept // 父ID不为根的部门
|
||||||
for _, dept := range depts {
|
for _, dept := range depts {
|
||||||
if dept.ParentId == "1" {
|
if dept.ParentId == "openldap_0" {
|
||||||
firstDepts = append(firstDepts, dept)
|
firstDepts = append(firstDepts, dept)
|
||||||
} else {
|
} else {
|
||||||
otherDepts = append(otherDepts, dept)
|
otherDepts = append(otherDepts, dept)
|
||||||
|
@ -41,6 +41,7 @@ func (d *OpenLdapLogic) SyncOpenLdapDepts(c *gin.Context, req interface{}) (data
|
||||||
SourceDeptId: dept.Id,
|
SourceDeptId: dept.Id,
|
||||||
Source: "openldap",
|
Source: "openldap",
|
||||||
SourceDeptParentId: dept.ParentId,
|
SourceDeptParentId: dept.ParentId,
|
||||||
|
ParentId: 0,
|
||||||
GroupDN: dept.DN,
|
GroupDN: dept.DN,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -49,7 +50,13 @@ func (d *OpenLdapLogic) SyncOpenLdapDepts(c *gin.Context, req interface{}) (data
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, dept := range otherDepts {
|
for _, dept := range otherDepts {
|
||||||
err := d.AddDepts(&model.Group{
|
// 判断部门名称是否存在
|
||||||
|
parentGroup := new(model.Group)
|
||||||
|
err := isql.Group.Find(tools.H{"source_dept_id": dept.ParentId}, parentGroup)
|
||||||
|
if err != nil {
|
||||||
|
return nil, tools.NewMySqlError(fmt.Errorf("查询父级部门失败:%s", err.Error()))
|
||||||
|
}
|
||||||
|
err = d.AddDepts(&model.Group{
|
||||||
GroupName: dept.Name,
|
GroupName: dept.Name,
|
||||||
Remark: dept.Remark,
|
Remark: dept.Remark,
|
||||||
Creator: "system",
|
Creator: "system",
|
||||||
|
@ -58,6 +65,7 @@ func (d *OpenLdapLogic) SyncOpenLdapDepts(c *gin.Context, req interface{}) (data
|
||||||
Source: "openldap",
|
Source: "openldap",
|
||||||
SourceDeptParentId: dept.ParentId,
|
SourceDeptParentId: dept.ParentId,
|
||||||
GroupDN: dept.DN,
|
GroupDN: dept.DN,
|
||||||
|
ParentId: parentGroup.ID,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, tools.NewOperationError(fmt.Errorf("SyncOpenLdapDepts添加其他部门失败:%s", err.Error()))
|
return nil, tools.NewOperationError(fmt.Errorf("SyncOpenLdapDepts添加其他部门失败:%s", err.Error()))
|
||||||
|
@ -68,16 +76,9 @@ func (d *OpenLdapLogic) SyncOpenLdapDepts(c *gin.Context, req interface{}) (data
|
||||||
|
|
||||||
// AddGroup 添加部门数据
|
// AddGroup 添加部门数据
|
||||||
func (d OpenLdapLogic) AddDepts(group *model.Group) error {
|
func (d OpenLdapLogic) AddDepts(group *model.Group) error {
|
||||||
// 判断部门名称是否存在
|
|
||||||
parentGroup := new(model.Group)
|
|
||||||
err := isql.Group.Find(tools.H{"source_dept_id": group.SourceDeptParentId}, parentGroup)
|
|
||||||
if err != nil {
|
|
||||||
return tools.NewMySqlError(fmt.Errorf("查询父级部门失败:%s", err.Error()))
|
|
||||||
}
|
|
||||||
if !isql.Group.Exist(tools.H{"source_dept_id": group.SourceDeptId}) {
|
|
||||||
group.ParentId = parentGroup.ID
|
|
||||||
// 在数据库中创建组
|
// 在数据库中创建组
|
||||||
err = isql.Group.Add(group)
|
if !isql.Group.Exist(tools.H{"source_dept_id": group.SourceDeptId}) {
|
||||||
|
err := isql.Group.Add(group)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,7 +61,7 @@ func GetAllDepts() (ret []*Dept, err error) {
|
||||||
ele.Id = strings.Split(strings.Split(v.DN, ",")[0], "=")[1]
|
ele.Id = strings.Split(strings.Split(v.DN, ",")[0], "=")[1]
|
||||||
ele.Remark = v.GetAttributeValue("description")
|
ele.Remark = v.GetAttributeValue("description")
|
||||||
if len(strings.Split(v.DN, ","))-len(strings.Split(config.Conf.Ldap.BaseDN, ",")) == 1 {
|
if len(strings.Split(v.DN, ","))-len(strings.Split(config.Conf.Ldap.BaseDN, ",")) == 1 {
|
||||||
ele.ParentId = "openldap_1"
|
ele.ParentId = "openldap_0"
|
||||||
} else {
|
} else {
|
||||||
ele.ParentId = strings.Split(strings.Split(v.DN, ",")[1], "=")[1]
|
ele.ParentId = strings.Split(strings.Split(v.DN, ",")[1], "=")[1]
|
||||||
}
|
}
|
||||||
|
|
|
@ -702,18 +702,6 @@ func InitData() {
|
||||||
groups := []model.Group{
|
groups := []model.Group{
|
||||||
{
|
{
|
||||||
Model: gorm.Model{ID: 1},
|
Model: gorm.Model{ID: 1},
|
||||||
GroupName: "openldaproot",
|
|
||||||
Remark: "ldap根部门",
|
|
||||||
Creator: "system",
|
|
||||||
GroupType: "ou",
|
|
||||||
ParentId: 0,
|
|
||||||
SourceDeptId: "openldap_1",
|
|
||||||
Source: "openldap",
|
|
||||||
SourceDeptParentId: "openldap_0",
|
|
||||||
GroupDN: fmt.Sprintf("ou=%s,%s", "openldaproot", config.Conf.Ldap.BaseDN),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
Model: gorm.Model{ID: 2},
|
|
||||||
GroupName: config.Conf.DingTalk.Flag + "root",
|
GroupName: config.Conf.DingTalk.Flag + "root",
|
||||||
Remark: "钉钉根部门",
|
Remark: "钉钉根部门",
|
||||||
Creator: "system",
|
Creator: "system",
|
||||||
|
@ -725,7 +713,7 @@ func InitData() {
|
||||||
GroupDN: fmt.Sprintf("ou=%s,%s", config.Conf.DingTalk.Flag+"root", config.Conf.Ldap.BaseDN),
|
GroupDN: fmt.Sprintf("ou=%s,%s", config.Conf.DingTalk.Flag+"root", config.Conf.Ldap.BaseDN),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Model: gorm.Model{ID: 3},
|
Model: gorm.Model{ID: 2},
|
||||||
GroupName: "wecomroot",
|
GroupName: "wecomroot",
|
||||||
Remark: "企业微信根部门",
|
Remark: "企业微信根部门",
|
||||||
Creator: "system",
|
Creator: "system",
|
||||||
|
@ -737,7 +725,7 @@ func InitData() {
|
||||||
GroupDN: fmt.Sprintf("ou=%s,%s", config.Conf.WeCom.Flag+"root", config.Conf.Ldap.BaseDN),
|
GroupDN: fmt.Sprintf("ou=%s,%s", config.Conf.WeCom.Flag+"root", config.Conf.Ldap.BaseDN),
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Model: gorm.Model{ID: 4},
|
Model: gorm.Model{ID: 3},
|
||||||
GroupName: config.Conf.FeiShu.Flag + "root",
|
GroupName: config.Conf.FeiShu.Flag + "root",
|
||||||
Remark: "飞书根部门",
|
Remark: "飞书根部门",
|
||||||
Creator: "system",
|
Creator: "system",
|
||||||
|
|
Loading…
Reference in New Issue