diff --git a/logic/a_logic.go b/logic/a_logic.go index a85dfc6..bc6f710 100644 --- a/logic/a_logic.go +++ b/logic/a_logic.go @@ -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 = "默认:研发中心" } @@ -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 diff --git a/logic/dingtalk_logic.go b/logic/dingtalk_logic.go index 6903cf4..33f72ef 100644 --- a/logic/dingtalk_logic.go +++ b/logic/dingtalk_logic.go @@ -2,6 +2,7 @@ package logic import ( "fmt" + "strings" "github.com/eryajf/go-ldap-admin/config" "github.com/eryajf/go-ldap-admin/model" @@ -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 { @@ -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 } diff --git a/logic/feishu_logic.go b/logic/feishu_logic.go index 6ca5521..29c1ca3 100644 --- a/logic/feishu_logic.go +++ b/logic/feishu_logic.go @@ -2,6 +2,7 @@ package logic import ( "fmt" + "strings" "github.com/eryajf/go-ldap-admin/config" "github.com/eryajf/go-ldap-admin/model" @@ -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 } diff --git a/logic/group_logic.go b/logic/group_logic.go index f034f14..edad981 100644 --- a/logic/group_logic.go +++ b/logic/group_logic.go @@ -2,6 +2,7 @@ package logic import ( "fmt" + "strconv" "strings" "github.com/eryajf/go-ldap-admin/config" @@ -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) @@ -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 } diff --git a/logic/user_logic.go b/logic/user_logic.go index d76627a..07cffca 100644 --- a/logic/user_logic.go +++ b/logic/user_logic.go @@ -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())) } diff --git a/logic/wecom_logic.go b/logic/wecom_logic.go index 226382c..44a4ae1 100644 --- a/logic/wecom_logic.go +++ b/logic/wecom_logic.go @@ -2,6 +2,7 @@ package logic import ( "fmt" + "strings" "github.com/eryajf/go-ldap-admin/config" "github.com/eryajf/go-ldap-admin/model" @@ -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 } diff --git a/public/client/dingtalk/dingtalk.go b/public/client/dingtalk/dingtalk.go index 580ceb0..669c05a 100644 --- a/public/client/dingtalk/dingtalk.go +++ b/public/client/dingtalk/dingtalk.go @@ -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" ) @@ -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 @@ -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 diff --git a/public/client/feishu/feishu.go b/public/client/feishu/feishu.go index 98ad47a..7b31739 100644 --- a/public/client/feishu/feishu.go +++ b/public/client/feishu/feishu.go @@ -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 @@ -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 @@ -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 diff --git a/public/client/wechat/wecom.go b/public/client/wechat/wecom.go index 8c9d750..0776d40 100644 --- a/public/client/wechat/wecom.go +++ b/public/client/wechat/wecom.go @@ -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" ) @@ -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 @@ -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 diff --git a/public/tools/type.go b/public/tools/type.go index 0a2964b..79ac165 100644 --- a/public/tools/type.go +++ b/public/tools/type.go @@ -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 +}