fix
This commit is contained in:
parent
4aef652b20
commit
1cc0cc8d72
|
@ -1,29 +0,0 @@
|
|||
package gmodel
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
"time"
|
||||
)
|
||||
|
||||
// ldap_department 部门表
|
||||
type LdapDepartment struct {
|
||||
Id int64 `gorm:"primary_key;default:0;auto_increment;" json:"id"` //
|
||||
Name *string `gorm:"unique_key;default:'';" json:"name"` //
|
||||
Remark *string `gorm:"unique_key;default:'';" json:"remark"` //
|
||||
Creator *string `gorm:"default:'';" json:"creator"` //
|
||||
Type *string `gorm:"default:'';" json:"type"` //
|
||||
ParentId *int64 `gorm:"default:0;" json:"parent_id"` // 层级如 10/20/30
|
||||
Dn *string `gorm:"default:'';" json:"dn"` //
|
||||
SyncState *int64 `gorm:"default:1;" json:"sync_state"` // 同步状态:1已同步, 2未同步
|
||||
Sort *int64 `gorm:"default:999;" json:"sort"` // 排序
|
||||
Ctime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"ctime"` //
|
||||
Utime *time.Time `gorm:"default:'0000-00-00 00:00:00';" json:"utime"` //
|
||||
}
|
||||
type LdapDepartmentModel struct {
|
||||
db *gorm.DB
|
||||
name string
|
||||
}
|
||||
|
||||
func NewLdapDepartmentModel(db *gorm.DB) *LdapDepartmentModel {
|
||||
return &LdapDepartmentModel{db: db, name: "ldap_department"}
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
package gmodel
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// 获取列表
|
||||
func (d *LdapDepartmentModel) GetAll(ctx context.Context, sort string) (resp []LdapDepartment, total int64, err error) {
|
||||
db := d.db.WithContext(ctx).Model(&LdapDepartment{})
|
||||
if sort != "" {
|
||||
db = db.Order(sort)
|
||||
}
|
||||
if err = db.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
err = db.Find(&resp).Error
|
||||
return resp, total, err
|
||||
}
|
||||
|
||||
func (d *LdapDepartmentModel) FindOne(ctx context.Context, id int64) (resp *LdapDepartment, err error) {
|
||||
err = d.db.WithContext(ctx).Model(&LdapDepartment{}).Where("id = ?", id).Take(&resp).Error
|
||||
return resp, err
|
||||
}
|
||||
|
||||
// 更新
|
||||
func (d *LdapDepartmentModel) Update(ctx context.Context, id int64, data *LdapDepartment) error {
|
||||
return d.db.WithContext(ctx).Model(&LdapDepartment{}).Where("id = ?", id).Updates(&data).Error
|
||||
}
|
||||
|
||||
// 创建
|
||||
func (d *LdapDepartmentModel) Create(ctx context.Context, data *LdapDepartment) error {
|
||||
return d.db.WithContext(ctx).Model(&LdapDepartment{}).Create(&data).Error
|
||||
}
|
||||
|
||||
func (d *LdapDepartmentModel) CreateOrUpdate(ctx context.Context, id int64, data *LdapDepartment) error {
|
||||
_, err := d.FindOne(ctx, id)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return d.Create(ctx, data)
|
||||
}
|
||||
return err
|
||||
}
|
||||
return d.Update(ctx, id, data)
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package gmodel
|
||||
|
||||
import (
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// ldap_department_users 部门用户表
|
||||
type LdapDepartmentUsers struct {
|
||||
DepartmentId *int64 `gorm:"default:0;" json:"department_id"` //
|
||||
UserId *int64 `gorm:"default:0;" json:"user_id"` //
|
||||
}
|
||||
type LdapDepartmentUsersModel struct {
|
||||
db *gorm.DB
|
||||
name string
|
||||
}
|
||||
|
||||
func NewLdapDepartmentUsersModel(db *gorm.DB) *LdapDepartmentUsersModel {
|
||||
return &LdapDepartmentUsersModel{db: db, name: "ldap_department_users"}
|
||||
}
|
|
@ -1,2 +0,0 @@
|
|||
package gmodel
|
||||
// TODO: 使用model的属性做你想做的
|
|
@ -1,16 +1 @@
|
|||
package gmodel
|
||||
|
||||
import "context"
|
||||
|
||||
// TODO: 使用model的属性做你想做的
|
||||
type GetAllUserWithDepartmentRsp struct {
|
||||
LdapUsers
|
||||
LdapDepartmentUsers
|
||||
}
|
||||
|
||||
func (u *LdapUsersModel) GetAllUserWithDepartment(ctx context.Context) (resp []GetAllUserWithDepartmentRsp, err error) {
|
||||
err = u.db.WithContext(ctx).Table(u.name + " as u").
|
||||
Joins("inner join `ldap_department_users` as du on u.id = du.user_id ").
|
||||
Select("u.*,du.*").Find(&resp).Error
|
||||
return resp, err
|
||||
}
|
||||
|
|
|
@ -111,8 +111,6 @@ type AllModelsGen struct {
|
|||
FsZipCode *FsZipCodeModel // fs_zip_code 邮编表
|
||||
LdapApis *LdapApisModel // ldap_apis api表
|
||||
LdapCasbinRule *LdapCasbinRuleModel // ldap_casbin_rule 权限表
|
||||
LdapDepartment *LdapDepartmentModel // ldap_department 部门表
|
||||
LdapDepartmentUsers *LdapDepartmentUsersModel // ldap_department_users 部门用户表
|
||||
LdapMenus *LdapMenusModel // ldap_menus 菜单表
|
||||
LdapRoleMenus *LdapRoleMenusModel // ldap_role_menus 角色菜单表
|
||||
LdapRoles *LdapRolesModel // ldap_roles 角色表
|
||||
|
@ -230,8 +228,6 @@ func NewAllModels(gdb *gorm.DB) *AllModelsGen {
|
|||
FsZipCode: NewFsZipCodeModel(gdb),
|
||||
LdapApis: NewLdapApisModel(gdb),
|
||||
LdapCasbinRule: NewLdapCasbinRuleModel(gdb),
|
||||
LdapDepartment: NewLdapDepartmentModel(gdb),
|
||||
LdapDepartmentUsers: NewLdapDepartmentUsersModel(gdb),
|
||||
LdapMenus: NewLdapMenusModel(gdb),
|
||||
LdapRoleMenus: NewLdapRoleMenusModel(gdb),
|
||||
LdapRoles: NewLdapRolesModel(gdb),
|
||||
|
|
|
@ -15,7 +15,6 @@ type Config struct {
|
|||
Host string
|
||||
BindDN string
|
||||
Password string
|
||||
AdminDN string
|
||||
UserDn string
|
||||
RootDN string
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,11 +17,6 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) {
|
|||
Path: "/api/ldap-admin/get_departments",
|
||||
Handler: GetDepartmentsHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodPost,
|
||||
Path: "/api/ldap-admin/save_department",
|
||||
Handler: SaveDepartmentHandler(serverCtx),
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/api/ldap-admin/get_apis",
|
||||
|
|
|
@ -1,35 +0,0 @@
|
|||
package handler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"reflect"
|
||||
|
||||
"fusenapi/utils/basic"
|
||||
|
||||
"fusenapi/server/ldap-admin/internal/logic"
|
||||
"fusenapi/server/ldap-admin/internal/svc"
|
||||
"fusenapi/server/ldap-admin/internal/types"
|
||||
)
|
||||
|
||||
func SaveDepartmentHandler(svcCtx *svc.ServiceContext) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var req types.SaveDepartmentReq
|
||||
userinfo, err := basic.RequestParse(w, r, svcCtx, &req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// 创建一个业务逻辑层实例
|
||||
l := logic.NewSaveDepartmentLogic(r.Context(), svcCtx)
|
||||
|
||||
rl := reflect.ValueOf(l)
|
||||
basic.BeforeLogic(w, r, rl)
|
||||
|
||||
resp := l.SaveDepartment(&req, userinfo)
|
||||
|
||||
if !basic.AfterLogic(w, r, rl, resp) {
|
||||
basic.NormalAfterLogic(w, r, resp)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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)
|
||||
// }
|
|
@ -44,16 +44,6 @@ type Member struct {
|
|||
Email string `json:"email"`
|
||||
}
|
||||
|
||||
type SaveDepartmentReq struct {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Remark string `json:"remark"`
|
||||
Type string `json:"type"`
|
||||
ParentId int64 `json:"parent_id"`
|
||||
Dn string `json:"dn"`
|
||||
Sort int64 `json:"sort"`
|
||||
}
|
||||
|
||||
type Request struct {
|
||||
}
|
||||
|
||||
|
|
|
@ -13,14 +13,10 @@ service ldap-admin {
|
|||
//获取部门列表
|
||||
@handler GetDepartmentsHandler
|
||||
get /api/ldap-admin/get_departments(request) returns (response);
|
||||
//保存部门信息
|
||||
@handler SaveDepartmentHandler
|
||||
post /api/ldap-admin/save_department(SaveDepartmentReq) returns (response);
|
||||
|
||||
//获取API列表
|
||||
@handler GetApisHandler
|
||||
get /api/ldap-admin/get_apis(GetApisReq) returns (response);
|
||||
|
||||
|
||||
//保存API
|
||||
@handler SaveApiHandler
|
||||
post /api/ldap-admin/save_api(SaveApiReq) returns (response);
|
||||
|
@ -62,14 +58,4 @@ type Member {
|
|||
Name string `json:"name"`
|
||||
Nickname string `json:"nickname"`
|
||||
Email string `json:"email"`
|
||||
}
|
||||
//保存部门信息
|
||||
type SaveDepartmentReq {
|
||||
Id int64 `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Remark string `json:"remark"`
|
||||
Type string `json:"type"`
|
||||
ParentId int64 `json:"parent_id"`
|
||||
Dn string `json:"dn"`
|
||||
Sort int64 `json:"sort"`
|
||||
}
|
Loading…
Reference in New Issue
Block a user