Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions logic/openldap_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func (d *OpenLdapLogic) SyncOpenLdapDepts(c *gin.Context, req interface{}) (data
var firstDepts []*openldap.Dept // 父ID为根的部门
var otherDepts []*openldap.Dept // 父ID不为根的部门
for _, dept := range depts {
if dept.ParentId == "1" {
if dept.ParentId == "openldap_0" {
firstDepts = append(firstDepts, dept)
} else {
otherDepts = append(otherDepts, dept)
Expand All @@ -41,6 +41,7 @@ func (d *OpenLdapLogic) SyncOpenLdapDepts(c *gin.Context, req interface{}) (data
SourceDeptId: dept.Id,
Source: "openldap",
SourceDeptParentId: dept.ParentId,
ParentId: 0,
GroupDN: dept.DN,
})
if err != nil {
Expand All @@ -49,7 +50,13 @@ func (d *OpenLdapLogic) SyncOpenLdapDepts(c *gin.Context, req interface{}) (data
}

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,
Remark: dept.Remark,
Creator: "system",
Expand All @@ -58,6 +65,7 @@ func (d *OpenLdapLogic) SyncOpenLdapDepts(c *gin.Context, req interface{}) (data
Source: "openldap",
SourceDeptParentId: dept.ParentId,
GroupDN: dept.DN,
ParentId: parentGroup.ID,
})
if err != nil {
return nil, tools.NewOperationError(fmt.Errorf("SyncOpenLdapDepts添加其他部门失败:%s", err.Error()))
Expand All @@ -68,16 +76,9 @@ func (d *OpenLdapLogic) SyncOpenLdapDepts(c *gin.Context, req interface{}) (data

// AddGroup 添加部门数据
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)
err := isql.Group.Add(group)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion public/client/openldap/openldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func GetAllDepts() (ret []*Dept, err error) {
ele.Id = strings.Split(strings.Split(v.DN, ",")[0], "=")[1]
ele.Remark = v.GetAttributeValue("description")
if len(strings.Split(v.DN, ","))-len(strings.Split(config.Conf.Ldap.BaseDN, ",")) == 1 {
ele.ParentId = "openldap_1"
ele.ParentId = "openldap_0"
} else {
ele.ParentId = strings.Split(strings.Split(v.DN, ",")[1], "=")[1]
}
Expand Down
16 changes: 2 additions & 14 deletions public/common/init_mysql_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -702,18 +702,6 @@ func InitData() {
groups := []model.Group{
{
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",
Remark: "钉钉根部门",
Creator: "system",
Expand All @@ -725,7 +713,7 @@ func InitData() {
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",
Remark: "企业微信根部门",
Creator: "system",
Expand All @@ -737,7 +725,7 @@ func InitData() {
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",
Remark: "飞书根部门",
Creator: "system",
Expand Down