fix
This commit is contained in:
@@ -1,14 +1,9 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"fusenapi/model/gmodel"
|
||||
"context"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"sort"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/ldap-admin/internal/svc"
|
||||
"fusenapi/server/ldap-admin/internal/types"
|
||||
@@ -35,139 +30,76 @@ func NewGetDepartmentsLogic(ctx context.Context, svcCtx *svc.ServiceContext) *Ge
|
||||
// }
|
||||
|
||||
func (l *GetDepartmentsLogic) GetDepartments(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
//todo 鉴权 。。。。
|
||||
departList, _, err := l.svcCtx.AllModels.LdapDepartment.GetAll(l.ctx, "sort ASC")
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "获取部门列表失败")
|
||||
}
|
||||
//变成树形结构
|
||||
list, err := l.DepartmentListToTree(departList, false)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeServiceErr, err.Error())
|
||||
}
|
||||
fmt.Println(l.SyncDepartmentToLdap())
|
||||
return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetDepartmentsRsp{
|
||||
List: list,
|
||||
})
|
||||
}
|
||||
|
||||
// 把列表变成树形结构
|
||||
func (l *GetDepartmentsLogic) DepartmentListToTree(deps []gmodel.LdapDepartment, withDepMember bool) ([]*types.DepartmentsItem, error) {
|
||||
var (
|
||||
ldapUserList []gmodel.GetAllUserWithDepartmentRsp
|
||||
err error
|
||||
)
|
||||
if withDepMember {
|
||||
ldapUserList, err = l.svcCtx.AllModels.LdapUsers.GetAllUserWithDepartment(l.ctx)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return nil, errors.New("获取全部部门用户失败")
|
||||
}
|
||||
}
|
||||
//存入map
|
||||
mapDepartment := make(map[int64]*types.DepartmentsItem)
|
||||
for _, v := range deps {
|
||||
data := &types.DepartmentsItem{
|
||||
Id: v.Id,
|
||||
Name: *v.Name,
|
||||
Remark: *v.Remark,
|
||||
Type: *v.Type,
|
||||
ParentId: *v.ParentId,
|
||||
Dn: *v.Dn,
|
||||
SyncState: *v.SyncState,
|
||||
Sort: *v.Sort,
|
||||
Child: make([]*types.DepartmentsItem, 0, 50),
|
||||
Members: nil,
|
||||
}
|
||||
members := make([]types.Member, 0, 100)
|
||||
for _, user := range ldapUserList {
|
||||
if *user.DepartmentId != v.Id {
|
||||
continue
|
||||
}
|
||||
members = append(members, types.Member{
|
||||
Id: user.Id,
|
||||
Name: *user.Username,
|
||||
Nickname: *user.Nickname,
|
||||
Email: *user.Email,
|
||||
})
|
||||
}
|
||||
data.Members = members
|
||||
mapDepartment[v.Id] = data
|
||||
}
|
||||
//组织从属关系
|
||||
for _, v := range mapDepartment {
|
||||
//如果有父级
|
||||
if parent, ok := mapDepartment[v.ParentId]; ok {
|
||||
parent.Child = append(parent.Child, v)
|
||||
sort.Slice(parent.Child, func(i, j int) bool {
|
||||
return parent.Child[i].Sort < parent.Child[j].Sort //升序
|
||||
})
|
||||
}
|
||||
}
|
||||
//排序
|
||||
list := make([]*types.DepartmentsItem, 0, len(deps))
|
||||
for _, v := range deps {
|
||||
if *v.ParentId == 0 {
|
||||
list = append(list, mapDepartment[v.Id])
|
||||
}
|
||||
}
|
||||
return list, nil
|
||||
}
|
||||
|
||||
// 同步到ldap
|
||||
func (l *GetDepartmentsLogic) SyncDepartmentToLdap() error {
|
||||
/* departList, _, err := l.svcCtx.AllModels.LdapDepartment.GetAll(l.ctx, "sort ASC")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
//获取所有部门用户
|
||||
ldapUserList, err := l.svcCtx.AllModels.LdapUsers.GetAllUserWithDepartment(l.ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, v := range departList {
|
||||
err = ildap.Department.Add(l.svcCtx.Ldap, l.svcCtx.Config.Ldap.AdminDN, ildap.DepartmentData{
|
||||
Id: v.Id,
|
||||
Name: *v.Name,
|
||||
Remark: *v.Remark,
|
||||
Type: *v.Type,
|
||||
ParentId: *v.ParentId,
|
||||
Dn: *v.Dn,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return errors.New("向LDAP同步分组失败")
|
||||
}
|
||||
userList := make([]gmodel.LdapUsers, 0, 100)
|
||||
for _, user := range ldapUserList {
|
||||
if *user.DepartmentId != v.Id {
|
||||
continue
|
||||
}
|
||||
userList = append(userList, user.LdapUsers)
|
||||
}
|
||||
if len(userList) > 0 {
|
||||
for _, user := range userList {
|
||||
if *user.UserDn == l.svcCtx.Config.Ldap.AdminDN {
|
||||
continue
|
||||
}
|
||||
err = ildap.Department.AddUserToGroup(l.svcCtx.Ldap, *v.Dn, *user.UserDn)
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return errors.New("把用户添加到ldap分组失败")
|
||||
}
|
||||
}
|
||||
}
|
||||
//更新的更新状态
|
||||
syncState := int64(1)
|
||||
err = l.svcCtx.AllModels.LdapDepartment.Update(l.ctx, v.Id, &gmodel.LdapDepartment{
|
||||
SyncState: &syncState,
|
||||
})
|
||||
if err != nil {
|
||||
logx.Error(err)
|
||||
return errors.New("更新分组同步状态失败")
|
||||
}
|
||||
}*/
|
||||
// todo 从ldap获取组织架构数据
|
||||
return nil
|
||||
}
|
||||
|
||||
/*
|
||||
dn: dc=fusen,dc=com
|
||||
o : [fusen com]
|
||||
dc : [fusen]
|
||||
objectClass : [top dcObject organization]
|
||||
|
||||
dn: cn=root,dc=fusen,dc=com
|
||||
cn : [root]
|
||||
objectClass : [organizationalRole]
|
||||
description : [Directory Manager]
|
||||
|
||||
dn: ou=People,dc=fusen,dc=com
|
||||
ou : [People]
|
||||
objectClass : [top organizationalUnit]
|
||||
telephoneNumber : [aabb]
|
||||
|
||||
dn: ou=Group,dc=fusen,dc=com
|
||||
ou : [Group]
|
||||
objectClass : [top organizationalUnit]
|
||||
dn: uid=fsuser,ou=People,dc=fusen,dc=com
|
||||
uid : [fsuser]
|
||||
cn : [fsuser]
|
||||
sn : [fsuser]
|
||||
mail : [fsuser@fusen.com]
|
||||
objectClass : [person organizationalPerson inetOrgPerson posixAccount top shadowAccount]
|
||||
userPassword : [{crypt}$6$fpyhuX7q$XBAA2URfWZCYZRLQpzzqucuFymrYvJL/NXztQ6/ug89GHvfN.OCUOGOZzyoKZwgF/LXToY5Wmqf9Yb0JoQbkL1]
|
||||
shadowLastChange : [19674]
|
||||
shadowMin : [0]
|
||||
shadowMax : [99999]
|
||||
shadowWarning : [7]
|
||||
loginShell : [/bin/bash]
|
||||
uidNumber : [1000]
|
||||
gidNumber : [1000]
|
||||
homeDirectory : [/home/fsuser]
|
||||
|
||||
dn: uid=fstest,ou=People,dc=fusen,dc=com
|
||||
uid : [fstest]
|
||||
cn : [fstest]
|
||||
sn : [fstest]
|
||||
mail : [fstest@fusen.com]
|
||||
objectClass : [person organizationalPerson inetOrgPerson posixAccount top shadowAccount]
|
||||
userPassword : [{crypt}$6$SYX2T3.y$OvYYU08PG8g.4SvI3A4MHSzfK5qSZeqB/2StqXxtd0E/RAENXFXBQH4bvkjLsbcDQMMobkzoyRyVJZv5xcK3r0]
|
||||
shadowLastChange : [19674]
|
||||
shadowMin : [0]
|
||||
shadowMax : [99999]
|
||||
shadowWarning : [7]
|
||||
loginShell : [/bin/bash]
|
||||
uidNumber : [1001]
|
||||
gidNumber : [1001]
|
||||
homeDirectory : [/home/fstest]
|
||||
mobile : [17557283677]
|
||||
|
||||
dn: cn=fusen,ou=Group,dc=fusen,dc=com
|
||||
objectClass : [posixGroup top]
|
||||
cn : [fusen]
|
||||
userPassword : [{crypt}x]
|
||||
gidNumber : [1000]
|
||||
|
||||
dn: cn=fusen2,ou=Group,dc=fusen,dc=com
|
||||
objectClass : [posixGroup top]
|
||||
cn : [fusen2]
|
||||
userPassword : [{crypt}x]
|
||||
gidNumber : [1001]
|
||||
|
||||
dn: cn=gitlab,ou=Group,dc=fusen,dc=com
|
||||
objectClass : [top groupOfNames]
|
||||
cn : [gitlab]
|
||||
member : [uid=testuser,ou=People,dc=funsen,dc=com]
|
||||
*/
|
||||
|
||||
@@ -1,65 +0,0 @@
|
||||
package logic
|
||||
|
||||
import (
|
||||
"fusenapi/model/gmodel"
|
||||
"fusenapi/utils/auth"
|
||||
"fusenapi/utils/basic"
|
||||
"time"
|
||||
|
||||
"context"
|
||||
|
||||
"fusenapi/server/ldap-admin/internal/svc"
|
||||
"fusenapi/server/ldap-admin/internal/types"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type SaveDepartmentLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewSaveDepartmentLogic(ctx context.Context, svcCtx *svc.ServiceContext) *SaveDepartmentLogic {
|
||||
return &SaveDepartmentLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
// 处理进入前逻辑w,r
|
||||
// func (l *SaveDepartmentLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
|
||||
// }
|
||||
|
||||
func (l *SaveDepartmentLogic) SaveDepartment(req *types.SaveDepartmentReq, userinfo *auth.UserInfo) (resp *basic.Response) {
|
||||
now := time.Now().UTC()
|
||||
data := &gmodel.LdapDepartment{
|
||||
Name: &req.Name,
|
||||
Remark: &req.Remark,
|
||||
Type: &req.Type,
|
||||
ParentId: &req.ParentId,
|
||||
Dn: &req.Dn,
|
||||
Sort: &req.Sort,
|
||||
Utime: &now,
|
||||
}
|
||||
if req.Id > 0{//更新
|
||||
if err := l.svcCtx.AllModels.LdapDepartment.Update(l.ctx,req.Id,data);err != nil{
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr,"更新失败")
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK,"更新成功")
|
||||
}
|
||||
//添加
|
||||
data.Ctime = &now
|
||||
if err := l.svcCtx.AllModels.LdapDepartment.Create(l.ctx,data);err != nil{
|
||||
logx.Error(err)
|
||||
return resp.SetStatusWithMessage(basic.CodeDbSqlErr,"添加失败")
|
||||
}
|
||||
return resp.SetStatusWithMessage(basic.CodeOK,"添加成功")
|
||||
}
|
||||
|
||||
// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
|
||||
// func (l *SaveDepartmentLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
|
||||
// // httpx.OkJsonCtx(r.Context(), w, resp)
|
||||
// }
|
||||
Reference in New Issue
Block a user