2022-05-18 17:57:03 +08:00
|
|
|
|
package logic
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2022-07-15 17:56:45 +08:00
|
|
|
|
"strconv"
|
2022-05-18 17:57:03 +08:00
|
|
|
|
"strings"
|
|
|
|
|
|
2022-05-29 10:06:21 +08:00
|
|
|
|
"github.com/eryajf/go-ldap-admin/config"
|
|
|
|
|
|
|
|
|
|
"github.com/eryajf/go-ldap-admin/model"
|
2022-06-14 11:17:38 +08:00
|
|
|
|
"github.com/eryajf/go-ldap-admin/model/request"
|
|
|
|
|
"github.com/eryajf/go-ldap-admin/model/response"
|
2022-05-29 10:06:21 +08:00
|
|
|
|
"github.com/eryajf/go-ldap-admin/public/tools"
|
|
|
|
|
"github.com/eryajf/go-ldap-admin/service/ildap"
|
|
|
|
|
"github.com/eryajf/go-ldap-admin/service/isql"
|
2022-05-18 17:57:03 +08:00
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type GroupLogic struct{}
|
|
|
|
|
|
|
|
|
|
// Add 添加数据
|
|
|
|
|
func (l GroupLogic) Add(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
|
|
|
|
r, ok := req.(*request.GroupAddReq)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
|
|
|
|
|
|
|
|
|
if isql.Group.Exist(tools.H{"group_name": r.GroupName}) {
|
|
|
|
|
return nil, tools.NewValidatorError(fmt.Errorf("组名已存在"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前用户
|
|
|
|
|
ctxUser, err := isql.User.GetCurrentLoginUser(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取当前登陆用户信息失败"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group := model.Group{
|
2022-06-14 11:17:38 +08:00
|
|
|
|
GroupType: r.GroupType,
|
|
|
|
|
ParentId: r.ParentId,
|
|
|
|
|
GroupName: r.GroupName,
|
|
|
|
|
Remark: r.Remark,
|
|
|
|
|
Creator: ctxUser.Username,
|
|
|
|
|
Source: "platform", //默认是平台添加
|
|
|
|
|
}
|
|
|
|
|
if r.ParentId == 0 {
|
|
|
|
|
group.SourceDeptId = "platform_0"
|
|
|
|
|
group.SourceDeptParentId = "platform_0"
|
2022-06-14 12:08:16 +08:00
|
|
|
|
group.GroupDN = fmt.Sprintf("%s=%s,%s", r.GroupType, r.GroupName, config.Conf.Ldap.BaseDN)
|
2022-06-14 11:17:38 +08:00
|
|
|
|
} else {
|
|
|
|
|
parentGroup := new(model.Group)
|
|
|
|
|
err := isql.Group.Find(tools.H{"id": r.ParentId}, parentGroup)
|
2022-05-28 22:22:36 +08:00
|
|
|
|
if err != nil {
|
2022-06-14 11:17:38 +08:00
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取父级组信息失败"))
|
2022-05-28 22:22:36 +08:00
|
|
|
|
}
|
2022-06-14 11:17:38 +08:00
|
|
|
|
group.SourceDeptId = "platform_0"
|
|
|
|
|
group.SourceDeptParentId = fmt.Sprintf("%s_%d", parentGroup.Source, r.ParentId)
|
|
|
|
|
group.GroupDN = fmt.Sprintf("%s=%s,%s", r.GroupType, r.GroupName, parentGroup.GroupDN)
|
2022-05-28 22:22:36 +08:00
|
|
|
|
}
|
2022-06-14 11:17:38 +08:00
|
|
|
|
|
|
|
|
|
// 先在ldap中创建组
|
|
|
|
|
err = ildap.Group.Add(&group)
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewLdapError(fmt.Errorf("向LDAP创建分组失败" + err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 11:17:38 +08:00
|
|
|
|
// 然后在数据库中创建组
|
2022-05-18 17:57:03 +08:00
|
|
|
|
err = isql.Group.Add(&group)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewLdapError(fmt.Errorf("向MySQL创建分组失败"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 默认创建分组之后,需要将admin添加到分组中
|
|
|
|
|
adminInfo := new(model.User)
|
|
|
|
|
err = isql.User.Find(tools.H{"id": 1}, adminInfo)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = isql.Group.AddUserToGroup(&group, []model.User{*adminInfo})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("添加用户到分组失败: %s", err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// List 数据列表
|
|
|
|
|
func (l GroupLogic) List(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
|
|
|
|
r, ok := req.(*request.GroupListReq)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
|
|
|
|
|
|
|
|
|
// 获取数据列表
|
|
|
|
|
groups, err := isql.Group.List(r)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取分组列表失败: %s", err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rets := make([]model.Group, 0)
|
|
|
|
|
for _, group := range groups {
|
|
|
|
|
rets = append(rets, *group)
|
|
|
|
|
}
|
|
|
|
|
count, err := isql.Group.Count()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取分组总数失败"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.GroupListRsp{
|
|
|
|
|
Total: count,
|
|
|
|
|
Groups: rets,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 22:22:36 +08:00
|
|
|
|
// GetTree 数据树
|
|
|
|
|
func (l GroupLogic) GetTree(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
2022-05-29 17:53:31 +08:00
|
|
|
|
r, ok := req.(*request.GroupListReq)
|
2022-05-28 22:22:36 +08:00
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
2022-05-29 17:53:31 +08:00
|
|
|
|
|
2022-05-28 22:22:36 +08:00
|
|
|
|
var groups []*model.Group
|
2022-06-02 11:05:55 +08:00
|
|
|
|
groups, err := isql.Group.ListTree(r)
|
2022-05-28 22:22:36 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取资源列表失败: " + err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tree := isql.GenGroupTree(0, groups)
|
|
|
|
|
|
|
|
|
|
return tree, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-18 17:57:03 +08:00
|
|
|
|
// Update 更新数据
|
|
|
|
|
func (l GroupLogic) Update(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
|
|
|
|
r, ok := req.(*request.GroupUpdateReq)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
|
|
|
|
|
|
|
|
|
filter := tools.H{"id": int(r.ID)}
|
|
|
|
|
if !isql.Group.Exist(filter) {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("分组不存在"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 获取当前登陆用户
|
|
|
|
|
ctxUser, err := isql.User.GetCurrentLoginUser(c)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取当前登陆用户失败"))
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 11:17:38 +08:00
|
|
|
|
oldGroup := new(model.Group)
|
|
|
|
|
err = isql.Group.Find(filter, oldGroup)
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(err)
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 11:17:38 +08:00
|
|
|
|
newGroup := model.Group{
|
|
|
|
|
Model: oldGroup.Model,
|
2022-05-28 22:22:36 +08:00
|
|
|
|
GroupName: r.GroupName,
|
2022-05-18 17:57:03 +08:00
|
|
|
|
Remark: r.Remark,
|
|
|
|
|
Creator: ctxUser.Username,
|
2022-06-14 11:17:38 +08:00
|
|
|
|
GroupType: oldGroup.GroupType,
|
2022-05-18 17:57:03 +08:00
|
|
|
|
}
|
2022-05-28 22:22:36 +08:00
|
|
|
|
|
2022-06-14 11:17:38 +08:00
|
|
|
|
//若配置了不允许修改分组名称,则不更新分组名称
|
2022-06-14 12:08:16 +08:00
|
|
|
|
if !config.Conf.Ldap.GroupNameModify {
|
2022-06-14 11:17:38 +08:00
|
|
|
|
newGroup.GroupName = oldGroup.GroupName
|
2022-05-28 22:22:36 +08:00
|
|
|
|
}
|
2022-06-14 11:17:38 +08:00
|
|
|
|
|
|
|
|
|
err = ildap.Group.Update(oldGroup, &newGroup)
|
2022-05-28 22:22:36 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewLdapError(fmt.Errorf("向LDAP更新分组失败:" + err.Error()))
|
|
|
|
|
}
|
2022-06-14 11:17:38 +08:00
|
|
|
|
err = isql.Group.Update(&newGroup)
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewLdapError(fmt.Errorf("向MySQL更新分组失败"))
|
|
|
|
|
}
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Delete 删除数据
|
|
|
|
|
func (l GroupLogic) Delete(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
|
|
|
|
r, ok := req.(*request.GroupDeleteReq)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
|
|
|
|
|
|
|
|
|
for _, id := range r.GroupIds {
|
|
|
|
|
filter := tools.H{"id": int(id)}
|
|
|
|
|
if !isql.Group.Exist(filter) {
|
2022-06-14 11:17:38 +08:00
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("有分组不存在"))
|
2022-05-18 17:57:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
groups, err := isql.Group.GetGroupByIds(r.GroupIds)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取分组列表失败: %s", err.Error()))
|
|
|
|
|
}
|
2022-06-14 11:17:38 +08:00
|
|
|
|
|
2022-05-18 17:57:03 +08:00
|
|
|
|
for _, group := range groups {
|
2022-05-28 22:22:36 +08:00
|
|
|
|
// 判断存在子分组,不允许删除
|
|
|
|
|
filter := tools.H{"parent_id": int(group.ID)}
|
|
|
|
|
if isql.Group.Exist(filter) {
|
2022-06-14 11:17:38 +08:00
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("存在子分组,请先删除子分组,再执行该分组的删除操作!"))
|
2022-05-28 22:22:36 +08:00
|
|
|
|
}
|
2022-06-14 11:17:38 +08:00
|
|
|
|
|
|
|
|
|
// 删除的时候先从ldap进行删除
|
|
|
|
|
err = ildap.Group.Delete(group.GroupDN)
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if err != nil {
|
2022-05-28 22:22:36 +08:00
|
|
|
|
return nil, tools.NewLdapError(fmt.Errorf("向LDAP删除分组失败:" + err.Error()))
|
2022-05-18 17:57:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-14 11:17:38 +08:00
|
|
|
|
|
|
|
|
|
// 从MySQL中删除
|
|
|
|
|
err = isql.Group.Delete(groups)
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("删除接口失败: %s", err.Error()))
|
|
|
|
|
}
|
2022-06-02 11:05:55 +08:00
|
|
|
|
|
2022-05-18 17:57:03 +08:00
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddUser 添加用户到分组
|
|
|
|
|
func (l GroupLogic) AddUser(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
|
|
|
|
r, ok := req.(*request.GroupAddUserReq)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
|
|
|
|
|
|
|
|
|
filter := tools.H{"id": r.GroupID}
|
|
|
|
|
|
|
|
|
|
if !isql.Group.Exist(filter) {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("分组不存在"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
users, err := isql.User.GetUserByIds(r.UserIds)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取用户列表失败: %s", err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group := new(model.Group)
|
|
|
|
|
err = isql.Group.Find(filter, group)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取分组失败: %s", err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 11:17:38 +08:00
|
|
|
|
if group.GroupDN[:3] == "ou=" {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("ou类型的分组不能添加用户"))
|
2022-05-18 17:57:03 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 11:17:38 +08:00
|
|
|
|
// 先添加到MySQL
|
2022-05-18 17:57:03 +08:00
|
|
|
|
err = isql.Group.AddUserToGroup(group, users)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("添加用户到分组失败: %s", err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-14 11:17:38 +08:00
|
|
|
|
// 再往ldap添加
|
|
|
|
|
for _, user := range users {
|
|
|
|
|
err = ildap.Group.AddUserToGroup(group.GroupDN, user.UserDN)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewLdapError(fmt.Errorf("向LDAP添加用户到分组失败" + err.Error()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 17:56:45 +08:00
|
|
|
|
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()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-18 17:57:03 +08:00
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 17:56:45 +08:00
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-18 17:57:03 +08:00
|
|
|
|
// RemoveUser 移除用户
|
|
|
|
|
func (l GroupLogic) RemoveUser(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
|
|
|
|
r, ok := req.(*request.GroupRemoveUserReq)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
|
|
|
|
|
|
|
|
|
filter := tools.H{"id": r.GroupID}
|
|
|
|
|
|
|
|
|
|
if !isql.Group.Exist(filter) {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("分组不存在"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
users, err := isql.User.GetUserByIds(r.UserIds)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取用户列表失败: %s", err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group := new(model.Group)
|
|
|
|
|
err = isql.Group.Find(filter, group)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取分组失败: %s", err.Error()))
|
|
|
|
|
}
|
2022-06-14 11:17:38 +08:00
|
|
|
|
|
|
|
|
|
if group.GroupDN[:3] == "ou=" {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("ou类型的分组内没有用户"))
|
2022-05-28 22:22:36 +08:00
|
|
|
|
}
|
2022-06-14 11:17:38 +08:00
|
|
|
|
|
|
|
|
|
// 先操作ldap
|
2022-05-18 17:57:03 +08:00
|
|
|
|
for _, user := range users {
|
2022-06-14 11:17:38 +08:00
|
|
|
|
err := ildap.Group.RemoveUserFromGroup(group.GroupDN, user.UserDN)
|
2022-05-18 17:57:03 +08:00
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewLdapError(fmt.Errorf("将用户从ldap移除失败" + err.Error()))
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-06-14 11:17:38 +08:00
|
|
|
|
|
|
|
|
|
// 再操作MySQL
|
2022-05-18 17:57:03 +08:00
|
|
|
|
err = isql.Group.RemoveUserFromGroup(group, users)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("将用户从MySQL移除失败: %s", err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-15 17:56:45 +08:00
|
|
|
|
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()))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-18 17:57:03 +08:00
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UserInGroup 在分组内的用户
|
|
|
|
|
func (l GroupLogic) UserInGroup(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
|
|
|
|
r, ok := req.(*request.UserInGroupReq)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
|
|
|
|
|
|
|
|
|
filter := tools.H{"id": r.GroupID}
|
|
|
|
|
|
|
|
|
|
if !isql.Group.Exist(filter) {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("分组不存在"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group := new(model.Group)
|
|
|
|
|
err := isql.Group.Find(filter, group)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取分组失败: %s", err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rets := make([]response.Guser, 0)
|
|
|
|
|
|
|
|
|
|
for _, user := range group.Users {
|
|
|
|
|
if r.Nickname != "" && !strings.Contains(user.Nickname, r.Nickname) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
rets = append(rets, response.Guser{
|
|
|
|
|
UserId: int64(user.ID),
|
|
|
|
|
UserName: user.Username,
|
|
|
|
|
NickName: user.Nickname,
|
|
|
|
|
Mail: user.Mail,
|
|
|
|
|
JobNumber: user.JobNumber,
|
|
|
|
|
Mobile: user.Mobile,
|
|
|
|
|
Introduction: user.Introduction,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.GroupUsers{
|
|
|
|
|
GroupId: int64(group.ID),
|
|
|
|
|
GroupName: group.GroupName,
|
|
|
|
|
GroupRemark: group.Remark,
|
|
|
|
|
UserList: rets,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// UserNoInGroup 不在分组内的用户
|
|
|
|
|
func (l GroupLogic) UserNoInGroup(c *gin.Context, req interface{}) (data interface{}, rspError interface{}) {
|
|
|
|
|
r, ok := req.(*request.UserNoInGroupReq)
|
|
|
|
|
if !ok {
|
|
|
|
|
return nil, ReqAssertErr
|
|
|
|
|
}
|
|
|
|
|
_ = c
|
|
|
|
|
|
|
|
|
|
filter := tools.H{"id": r.GroupID}
|
|
|
|
|
|
|
|
|
|
if !isql.Group.Exist(filter) {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("分组不存在"))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
group := new(model.Group)
|
|
|
|
|
err := isql.Group.Find(filter, group)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取分组失败: %s", err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var userList []*model.User
|
|
|
|
|
userList, err = isql.User.ListAll()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, tools.NewMySqlError(fmt.Errorf("获取资源列表失败: " + err.Error()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rets := make([]response.Guser, 0)
|
|
|
|
|
for _, user := range userList {
|
|
|
|
|
in := true
|
|
|
|
|
for _, groupUser := range group.Users {
|
|
|
|
|
if user.Username == groupUser.Username {
|
|
|
|
|
in = false
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if in {
|
|
|
|
|
if r.Nickname != "" && !strings.Contains(user.Nickname, r.Nickname) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
rets = append(rets, response.Guser{
|
|
|
|
|
UserId: int64(user.ID),
|
|
|
|
|
UserName: user.Username,
|
|
|
|
|
NickName: user.Nickname,
|
|
|
|
|
Mail: user.Mail,
|
|
|
|
|
JobNumber: user.JobNumber,
|
|
|
|
|
Mobile: user.Mobile,
|
|
|
|
|
Introduction: user.Introduction,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response.GroupUsers{
|
|
|
|
|
GroupId: int64(group.ID),
|
|
|
|
|
GroupName: group.GroupName,
|
|
|
|
|
GroupRemark: group.Remark,
|
|
|
|
|
UserList: rets,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|