Skip to content

Commit

Permalink
fix: 部门名称有中英文混合的情况&解决部门添加移除用户时用户信息未更新的问题 (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
eryajf authored Jul 15, 2022
1 parent 1d0df89 commit efb2fd9
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 20 deletions.
9 changes: 3 additions & 6 deletions logic/a_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func CommonUpdateGroup(oldGroup, newGroup *model.Group) error {
}

// CommonAddUser 标准创建用户
func CommonAddUser(user *model.User, groupId []uint) error {
func CommonAddUser(user *model.User, groups []*model.Group) error {
if user.Departments == "" {
user.Departments = "默认:研发中心"
}
Expand Down Expand Up @@ -110,11 +110,8 @@ func CommonAddUser(user *model.User, groupId []uint) error {
if err != nil {
return tools.NewLdapError(fmt.Errorf("AddUser向LDAP创建用户失败:" + err.Error()))
}
// 获取用户将要添加的分组
groups, err := isql.Group.GetGroupByIds(groupId)
if err != nil {
return tools.NewMySqlError(fmt.Errorf("根据部门ID获取部门信息失败" + err.Error()))
}

// 处理用户归属的组
for _, group := range groups {
if group.GroupDN[:3] == "ou=" {
continue
Expand Down
14 changes: 13 additions & 1 deletion logic/dingtalk_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logic

import (
"fmt"
"strings"

"github.com/eryajf/go-ldap-admin/config"
"github.com/eryajf/go-ldap-admin/model"
Expand Down Expand Up @@ -68,6 +69,7 @@ func (d DingTalkLogic) AddDepts(group *model.Group) error {
group.ParentId = parentGroup.ID
group.Source = config.Conf.DingTalk.Flag
group.GroupDN = fmt.Sprintf("cn=%s,%s", group.GroupName, parentGroup.GroupDN)
fmt.Println(group.GroupName, group.Remark, group.GroupDN)

err = CommonAddGroup(group)
if err != nil {
Expand Down Expand Up @@ -140,7 +142,17 @@ func (d DingTalkLogic) AddUsers(user *model.User) error {
user.Password = config.Conf.Ldap.UserInitPassword
user.Source = config.Conf.DingTalk.Flag
user.UserDN = fmt.Sprintf("uid=%s,%s", user.Username, config.Conf.Ldap.UserDN)
err = CommonAddUser(user, tools.StringToSlice(user.DepartmentId, ","))
// 获取用户将要添加的分组
groups, err := isql.Group.GetGroupByIds(tools.StringToSlice(user.DepartmentId, ","))
if err != nil {
return tools.NewMySqlError(fmt.Errorf("根据部门ID获取部门信息失败" + err.Error()))
}
var deptTmp string
for _, group := range groups {
deptTmp = deptTmp + group.GroupName + ","
}
user.Departments = strings.TrimRight(deptTmp, ",")
err = CommonAddUser(user, groups)
if err != nil {
return err
}
Expand Down
13 changes: 12 additions & 1 deletion logic/feishu_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logic

import (
"fmt"
"strings"

"github.com/eryajf/go-ldap-admin/config"
"github.com/eryajf/go-ldap-admin/model"
Expand Down Expand Up @@ -140,7 +141,17 @@ func (d FeiShuLogic) AddUsers(user *model.User) error {
user.Password = config.Conf.Ldap.UserInitPassword
user.Source = config.Conf.FeiShu.Flag
user.UserDN = fmt.Sprintf("uid=%s,%s", user.Username, config.Conf.Ldap.UserDN)
err = CommonAddUser(user, tools.StringToSlice(user.DepartmentId, ","))
// 获取用户将要添加的分组
groups, err := isql.Group.GetGroupByIds(tools.StringToSlice(user.DepartmentId, ","))
if err != nil {
return tools.NewMySqlError(fmt.Errorf("根据部门ID获取部门信息失败" + err.Error()))
}
var deptTmp string
for _, group := range groups {
deptTmp = deptTmp + group.GroupName + ","
}
user.Departments = strings.TrimRight(deptTmp, ",")
err = CommonAddUser(user, groups)
if err != nil {
return err
}
Expand Down
56 changes: 56 additions & 0 deletions logic/group_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logic

import (
"fmt"
"strconv"
"strings"

"github.com/eryajf/go-ldap-admin/config"
Expand Down Expand Up @@ -269,9 +270,33 @@ func (l GroupLogic) AddUser(c *gin.Context, req interface{}) (data interface{},
}
}

for _, user := range users {
oldData := new(model.User)
err = isql.User.Find(tools.H{"id": user.ID}, oldData)
if err != nil {
return nil, tools.NewMySqlError(err)
}
newData := oldData
// 添加新增的分组ID与部门
newData.DepartmentId = oldData.DepartmentId + "," + strconv.Itoa(int(r.GroupID))
newData.Departments = oldData.Departments + "," + group.GroupName
err = l.updataUser(newData)
if err != nil {
return nil, tools.NewOperationError(fmt.Errorf("处理用户的部门数据失败:" + err.Error()))
}
}

return nil, nil
}

func (l GroupLogic) updataUser(newUser *model.User) error {
err := isql.User.Update(newUser)
if err != nil {
return tools.NewMySqlError(fmt.Errorf("在MySQL更新用户失败:" + err.Error()))
}
return nil
}

// RemoveUser 移除用户
func (l GroupLogic) RemoveUser(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
r, ok := req.(*request.GroupRemoveUserReq)
Expand Down Expand Up @@ -315,6 +340,37 @@ func (l GroupLogic) RemoveUser(c *gin.Context, req interface{}) (data interface{
return nil, tools.NewMySqlError(fmt.Errorf("将用户从MySQL移除失败: %s", err.Error()))
}

for _, user := range users {
oldData := new(model.User)
err = isql.User.Find(tools.H{"id": user.ID}, oldData)
if err != nil {
return nil, tools.NewMySqlError(err)
}
newData := oldData

var newDepts []string
var newDeptIds []string
// 删掉移除的分组名字
for _, v := range strings.Split(oldData.Departments, ",") {
if v != group.GroupName {
newDepts = append(newDepts, v)
}
}
// 删掉移除的分组id
for _, v := range strings.Split(oldData.DepartmentId, ",") {
if v != strconv.Itoa(int(r.GroupID)) {
newDeptIds = append(newDeptIds, v)
}
}

newData.Departments = strings.Join(newDepts, ",")
newData.DepartmentId = strings.Join(newDeptIds, ",")
err = l.updataUser(newData)
if err != nil {
return nil, tools.NewOperationError(fmt.Errorf("处理用户的部门数据失败:" + err.Error()))
}
}

return nil, nil
}

Expand Down
11 changes: 9 additions & 2 deletions logic/user_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,15 @@ func (l UserLogic) Add(c *gin.Context, req interface{}) (data interface{}, rspEr
if user.Source == "" {
user.Source = "platform"
}

err = CommonAddUser(&user, r.DepartmentId)
// 获取用户将要添加的分组
groups, err := isql.Group.GetGroupByIds(tools.StringToSlice(user.DepartmentId, ","))
if err != nil {
return nil, tools.NewMySqlError(fmt.Errorf("根据部门ID获取部门信息失败" + err.Error()))
}
for _, group := range groups {
user.Departments = user.Departments + group.GroupName
}
err = CommonAddUser(&user, groups)
if err != nil {
return nil, tools.NewOperationError(fmt.Errorf("添加用户失败" + err.Error()))
}
Expand Down
13 changes: 12 additions & 1 deletion logic/wecom_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package logic

import (
"fmt"
"strings"

"github.com/eryajf/go-ldap-admin/config"
"github.com/eryajf/go-ldap-admin/model"
Expand Down Expand Up @@ -155,7 +156,17 @@ func (d WeComLogic) AddUsers(user *model.User) error {
user.Password = config.Conf.Ldap.UserInitPassword
user.Source = config.Conf.WeCom.Flag
user.UserDN = fmt.Sprintf("uid=%s,%s", user.Username, config.Conf.Ldap.UserDN)
err = CommonAddUser(user, tools.StringToSlice(user.DepartmentId, ","))
// 获取用户将要添加的分组
groups, err := isql.Group.GetGroupByIds(tools.StringToSlice(user.DepartmentId, ","))
if err != nil {
return tools.NewMySqlError(fmt.Errorf("根据部门ID获取部门信息失败" + err.Error()))
}
var deptTmp string
for _, group := range groups {
deptTmp = deptTmp + group.GroupName + ","
}
user.Departments = strings.TrimRight(deptTmp, ",")
err = CommonAddUser(user, groups)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions public/client/dingtalk/dingtalk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/eryajf/go-ldap-admin/config"
"github.com/mozillazg/go-pinyin"
"github.com/eryajf/go-ldap-admin/public/tools"
"github.com/zhaoyunxing92/dingtalk/v2/request"
)

Expand All @@ -22,7 +22,7 @@ func GetAllDepts() (ret []map[string]interface{}, err error) {
ele["id"] = dept.Id
ele["name"] = dept.Name
ele["parentid"] = dept.ParentId
ele["custom_name_pinyin"] = strings.Join(pinyin.LazyConvert(dept.Name, nil), "")
ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name)
ret = append(ret, ele)
}
return
Expand Down Expand Up @@ -52,7 +52,7 @@ func GetAllUsers() (ret []map[string]interface{}, err error) {
ele := make(map[string]interface{})
ele["userid"] = user.UserId
ele["unionid"] = user.UnionId
ele["custom_name_pinyin"] = strings.Join(pinyin.LazyConvert(user.Name, nil), "")
ele["custom_name_pinyin"] = tools.ConvertToPinYin(user.Name)
ele["name"] = user.Name
ele["avatar"] = user.Avatar
ele["mobile"] = user.Mobile
Expand Down
6 changes: 3 additions & 3 deletions public/client/feishu/feishu.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"github.com/chyroc/lark"
"github.com/eryajf/go-ldap-admin/config"
"github.com/mozillazg/go-pinyin"
"github.com/eryajf/go-ldap-admin/public/tools"
)

// 官方文档: https://open.feishu.cn/document/uAjLw4CM/ukTMukTMukTM/reference/contact-v3/department/children
Expand All @@ -31,7 +31,7 @@ func GetAllDepts() (ret []map[string]interface{}, err error) {
for _, dept := range res.Items {
ele := make(map[string]interface{})
ele["name"] = dept.Name
ele["custom_name_pinyin"] = strings.Join(pinyin.LazyConvert(dept.Name, nil), "")
ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name)
ele["parent_department_id"] = dept.ParentDepartmentID
ele["department_id"] = dept.DepartmentID
ele["open_department_id"] = dept.OpenDepartmentID
Expand Down Expand Up @@ -71,7 +71,7 @@ func GetAllUsers() (ret []map[string]interface{}, err error) {
for _, user := range res.Items {
ele := make(map[string]interface{})
ele["name"] = user.Name
ele["custom_name_pinyin"] = strings.Join(pinyin.LazyConvert(user.Name, nil), "")
ele["custom_name_pinyin"] = tools.ConvertToPinYin(user.Name)
ele["union_id"] = user.UnionID
ele["user_id"] = user.UserID
ele["open_id"] = user.OpenID
Expand Down
6 changes: 3 additions & 3 deletions public/client/wechat/wecom.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"

"github.com/eryajf/go-ldap-admin/config"
"github.com/mozillazg/go-pinyin"
"github.com/eryajf/go-ldap-admin/public/tools"
"github.com/wenerme/go-wecom/wecom"
)

Expand All @@ -21,7 +21,7 @@ func GetAllDepts() (ret []map[string]interface{}, err error) {
for _, dept := range depts.Department {
ele := make(map[string]interface{})
ele["name"] = dept.Name
ele["custom_name_pinyin"] = strings.Join(pinyin.LazyConvert(dept.Name, nil), "")
ele["custom_name_pinyin"] = tools.ConvertToPinYin(dept.Name)
ele["id"] = dept.ID
ele["name_en"] = dept.NameEn
ele["parentid"] = dept.ParentID
Expand Down Expand Up @@ -50,7 +50,7 @@ func GetAllUsers() (ret []map[string]interface{}, err error) {
for _, user := range users.UserList {
ele := make(map[string]interface{})
ele["name"] = user.Name
ele["custom_name_pinyin"] = strings.Join(pinyin.LazyConvert(user.Name, nil), "")
ele["custom_name_pinyin"] = tools.ConvertToPinYin(user.Name)
ele["userid"] = user.UserID
ele["mobile"] = user.Mobile
ele["position"] = user.Position
Expand Down
27 changes: 27 additions & 0 deletions public/tools/type.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,31 @@
package tools

import (
"strings"
"unicode"

"github.com/mozillazg/go-pinyin"
)

// H is a shortcut for map[string]interface{}
type H map[string]interface{}

// 是否全为中文
func isChinese(s string) bool {
for _, r := range s {
if !unicode.Is(unicode.Han, r) {
return false
}
}
return true
}

// 将中文内容转成拼音
func ConvertToPinYin(s string) (ret string) {
if isChinese(s) {
ret = strings.Join(pinyin.LazyConvert(s, nil), "")
} else {
ret = s
}
return
}

0 comments on commit efb2fd9

Please sign in to comment.