Skip to content

[feature]添加按部门ID同步钉钉指定部门 #252

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 25, 2023
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
4 changes: 4 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ dingtalk:
enable-sync: false # 是否开启定时同步钉钉的任务
dept-sync-time: "0 30 2 * * *" # 部门同步任务的时间点 * * * * * * 秒 分 时 日 月 周, 请把时间设置在凌晨 1 ~ 5 点
user-sync-time: "0 30 3 * * *" # 用户同步任务的时间点 * * * * * * 秒 分 时 日 月 周, 请把时间设置在凌晨 1 ~ 5 点,注意请把用户同步的任务滞后于部门同步时间,比如部门为2点,则用户为3点
dept-list: #部门列表,不设置则使用公司根部门,在开头加^表示不同步此部门,只需配置需要同步的部门ID
#- "1" #根组织ID
- "48456726" #需要同步的部门ID
#- "^61213417" #不同步的ID,不配置即只同步上一ID
wecom:
# 配置获取详细文档参考:http://ldapdoc.eryajf.net/pages/cf1698/
flag: "wecom" # 作为微信在平台的标识
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ type DingTalkConfig struct {
EnableSync bool `mapstructure:"enable-sync" json:"enableSync"`
DeptSyncTime string `mapstructure:"dept-sync-time" json:"deptSyncTime"`
UserSyncTime string `mapstructure:"user-sync-time" json:"userSyncTime"`
DeptList []string `mapstructure:"dept-list" json:"deptList"`
}

type WeComConfig struct {
Expand Down
74 changes: 64 additions & 10 deletions public/client/dingtalk/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package dingtalk

import (
"fmt"
"strconv"
"strings"

"github.com/eryajf/go-ldap-admin/config"
Expand All @@ -14,16 +15,69 @@ import (
func GetAllDepts() (ret []map[string]interface{}, err error) {
depts, err := InitDingTalkClient().FetchDeptList(1, true, "zh_CN")
if err != nil {
return ret, err
}
ret = make([]map[string]interface{}, 0)
for _, dept := range depts.Dept {
ele := make(map[string]interface{})
ele["id"] = dept.Id
ele["name"] = dept.Name
ele["parentid"] = dept.ParentId
ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name)
ret = append(ret, ele)
return ret, err
}
if len(config.Conf.DingTalk.DeptList) == 0 {

ret = make([]map[string]interface{}, 0)
for _, dept := range depts.Dept {
ele := make(map[string]interface{})
ele["id"] = dept.Id
ele["name"] = dept.Name
ele["parentid"] = dept.ParentId
ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name)
ret = append(ret, ele)
}
} else {

// 遍历配置的部门ID列表获取数据进行处理
// 从取得的所有部门列表中将配置的部门ID筛选出来再去请求其子部门过滤为1和为配置值的部门ID
ret = make([]map[string]interface{}, 0)

for _, dept := range depts.Dept {
inset := false
for _, dep_s := range config.Conf.DingTalk.DeptList {
if strings.HasPrefix(dep_s, "^") {
continue
}
setdepid, _ := strconv.Atoi(dep_s)
if dept.Id == setdepid {
inset = true
break
}
}
if dept.Id == 1 || inset {
ele := make(map[string]interface{})
ele["id"] = dept.Id
ele["name"] = dept.Name
ele["parentid"] = dept.ParentId
ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name)
ret = append(ret, ele)
}
}

for _, dep_s := range config.Conf.DingTalk.DeptList {
dept_id := dep_s

if strings.HasPrefix(dep_s, "^") || dept_id == "1" {
continue
}
depid, _ := strconv.Atoi(dept_id)
depts, err := InitDingTalkClient().FetchDeptList(depid, true, "zh_CN")

if err != nil {
return ret, err
}

for _, dept := range depts.Dept {
ele := make(map[string]interface{})
ele["id"] = dept.Id
ele["name"] = dept.Name
ele["parentid"] = dept.ParentId
ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name)
ret = append(ret, ele)
}
}
}
return
}
Expand Down