From 0bec257730ccd6a475e79e06345f3e2dc02c7748 Mon Sep 17 00:00:00 2001 From: laodaming <11058467+laudamine@user.noreply.gitee.com> Date: Fri, 17 Nov 2023 10:22:23 +0800 Subject: [PATCH] fix --- .../handler/addldaporginationmemberhandler.go | 35 +++++++++++++ .../removeldaporginationmemberhandler.go | 35 +++++++++++++ server/ldap-admin/internal/handler/routes.go | 10 ++++ .../logic/addldaporginationmemberlogic.go | 50 +++++++++++++++++++ .../logic/removeldaporginationmemberlogic.go | 43 ++++++++++++++++ server/ldap-admin/internal/types/types.go | 10 ++++ server_api/ldap-admin.api | 16 ++++++ utils/ldap_lib/ldap_group.go | 6 +-- 8 files changed, 202 insertions(+), 3 deletions(-) create mode 100644 server/ldap-admin/internal/handler/addldaporginationmemberhandler.go create mode 100644 server/ldap-admin/internal/handler/removeldaporginationmemberhandler.go create mode 100644 server/ldap-admin/internal/logic/addldaporginationmemberlogic.go create mode 100644 server/ldap-admin/internal/logic/removeldaporginationmemberlogic.go diff --git a/server/ldap-admin/internal/handler/addldaporginationmemberhandler.go b/server/ldap-admin/internal/handler/addldaporginationmemberhandler.go new file mode 100644 index 00000000..76ccb71c --- /dev/null +++ b/server/ldap-admin/internal/handler/addldaporginationmemberhandler.go @@ -0,0 +1,35 @@ +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 AddLdapOrginationMemberHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.AddLdapOrginationMemberReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewAddLdapOrginationMemberLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.AddLdapOrginationMember(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/ldap-admin/internal/handler/removeldaporginationmemberhandler.go b/server/ldap-admin/internal/handler/removeldaporginationmemberhandler.go new file mode 100644 index 00000000..4e3a9ded --- /dev/null +++ b/server/ldap-admin/internal/handler/removeldaporginationmemberhandler.go @@ -0,0 +1,35 @@ +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 RemoveLdapOrginationMemberHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.RemoveLdapOrginationMemberReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewRemoveLdapOrginationMemberLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.RemoveLdapOrginationMember(&req, userinfo) + + if !basic.AfterLogic(w, r, rl, resp) { + basic.NormalAfterLogic(w, r, resp) + } + } +} diff --git a/server/ldap-admin/internal/handler/routes.go b/server/ldap-admin/internal/handler/routes.go index f17e1a45..773f3cd1 100644 --- a/server/ldap-admin/internal/handler/routes.go +++ b/server/ldap-admin/internal/handler/routes.go @@ -82,6 +82,16 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/ldap-admin/get_ldap_user_info", Handler: GetLdapUserInfoHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/ldap-admin/add_ldap_orgination_member", + Handler: AddLdapOrginationMemberHandler(serverCtx), + }, + { + Method: http.MethodPost, + Path: "/api/ldap-admin/remove_ldap_orgination_member", + Handler: RemoveLdapOrginationMemberHandler(serverCtx), + }, }, ) } diff --git a/server/ldap-admin/internal/logic/addldaporginationmemberlogic.go b/server/ldap-admin/internal/logic/addldaporginationmemberlogic.go new file mode 100644 index 00000000..72efb73a --- /dev/null +++ b/server/ldap-admin/internal/logic/addldaporginationmemberlogic.go @@ -0,0 +1,50 @@ +package logic + +import ( + "fusenapi/utils/auth" + "fusenapi/utils/basic" + "strings" + + "context" + + "fusenapi/server/ldap-admin/internal/svc" + "fusenapi/server/ldap-admin/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type AddLdapOrginationMemberLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewAddLdapOrginationMemberLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddLdapOrginationMemberLogic { + return &AddLdapOrginationMemberLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *AddLdapOrginationMemberLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *AddLdapOrginationMemberLogic) AddLdapOrginationMember(req *types.AddLdapOrginationMemberReq, userinfo *auth.UserInfo) (resp *basic.Response) { + req.OrginationDN = strings.Trim(req.OrginationDN, " ") + req.UserDN = strings.Trim(req.UserDN, " ") + if len(req.OrginationDN) <= 3 || req.OrginationDN[:3] != "ou=" { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "无效的目标组织DN") + } + if len(req.UserDN) <= 3 || req.UserDN[:3] != "cn=" { + return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "无效的用户DN") + } + //ldapServer := ldap_lib.NewLdap(l.svcCtx.Ldap, l.svcCtx.Config.Ldap.BaseDN, l.svcCtx.Config.Ldap.RootDN) + return resp.SetStatus(basic.CodeOK) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *AddLdapOrginationMemberLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/ldap-admin/internal/logic/removeldaporginationmemberlogic.go b/server/ldap-admin/internal/logic/removeldaporginationmemberlogic.go new file mode 100644 index 00000000..7d6e0f5e --- /dev/null +++ b/server/ldap-admin/internal/logic/removeldaporginationmemberlogic.go @@ -0,0 +1,43 @@ +package logic + +import ( + "fusenapi/utils/auth" + "fusenapi/utils/basic" + + "context" + + "fusenapi/server/ldap-admin/internal/svc" + "fusenapi/server/ldap-admin/internal/types" + + "github.com/zeromicro/go-zero/core/logx" +) + +type RemoveLdapOrginationMemberLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewRemoveLdapOrginationMemberLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RemoveLdapOrginationMemberLogic { + return &RemoveLdapOrginationMemberLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *RemoveLdapOrginationMemberLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *RemoveLdapOrginationMemberLogic) RemoveLdapOrginationMember(req *types.RemoveLdapOrginationMemberReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + + return resp.SetStatus(basic.CodeOK) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *RemoveLdapOrginationMemberLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/ldap-admin/internal/types/types.go b/server/ldap-admin/internal/types/types.go index 431f6ece..758a9c98 100644 --- a/server/ldap-admin/internal/types/types.go +++ b/server/ldap-admin/internal/types/types.go @@ -124,6 +124,16 @@ type GetLdapUserInfoRsp struct { Status int64 `json:"status,options=0|1"` //状态 1正常0离职 } +type AddLdapOrginationMemberReq struct { + OrginationDN string `json:"orgination_dn"` //目标组织DN + UserDN string `json:"user_dn"` //用户DN +} + +type RemoveLdapOrginationMemberReq struct { + OrginationDN string `json:"orgination_dn"` //目标组织DN + UserDN string `json:"user_dn"` //用户DN +} + type Request struct { } diff --git a/server_api/ldap-admin.api b/server_api/ldap-admin.api index 621bfce9..e1842022 100644 --- a/server_api/ldap-admin.api +++ b/server_api/ldap-admin.api @@ -54,6 +54,12 @@ service ldap-admin { //获取ldap用户信息 @handler GetLdapUserInfoHandler get /api/ldap-admin/get_ldap_user_info(GetLdapUserInfoReq) returns (response); + //ldap组织添加成员 + @handler AddLdapOrginationMemberHandler + post /api/ldap-admin/add_ldap_orgination_member(AddLdapOrginationMemberReq) returns (response); + //ldap组织移除成员 + @handler RemoveLdapOrginationMemberHandler + post /api/ldap-admin/remove_ldap_orgination_member(RemoveLdapOrginationMemberReq) returns (response); } type GetApisReq { @@ -170,4 +176,14 @@ type GetLdapUserInfoRsp { Mobile string `json:"mobile"` //手机号 Avatar string `json:"avatar"` //头像地址 Status int64 `json:"status,options=0|1"` //状态 1正常0离职 +} +//ldap组织添加成员 +type AddLdapOrginationMemberReq { + OrginationDN string `json:"orgination_dn"` //目标组织DN + UserDN string `json:"user_dn"` //用户DN +} +//ldap组织移除成员 +type RemoveLdapOrginationMemberReq { + OrginationDN string `json:"orgination_dn"` //目标组织DN + UserDN string `json:"user_dn"` //用户DN } \ No newline at end of file diff --git a/utils/ldap_lib/ldap_group.go b/utils/ldap_lib/ldap_group.go index f7290759..922cc303 100644 --- a/utils/ldap_lib/ldap_group.go +++ b/utils/ldap_lib/ldap_group.go @@ -65,8 +65,8 @@ func (l *Ldap) Search(DN string, scope int, filter string, attr []string, contro return l.conn.Search(searchRequest) } -// AddUserToGroup 添加用户到分组 -func (l *Ldap) AddUserToGroup(groupDN, userDN string) error { +// AddUserToGroup 添加用户到组织 +func (l *Ldap) AddUserToOrganization(groupDN, userDN string) error { //判断dn是否以ou开头 if groupDN[:3] == "ou=" { return errors.New("不能添加用户到OU组织单元") @@ -77,7 +77,7 @@ func (l *Ldap) AddUserToGroup(groupDN, userDN string) error { } // DelUserFromGroup 将用户从分组删除 -func (l *Ldap) RemoveUserFromGroup(groupDN, userDN string) error { +func (l *Ldap) RemoveUserFromOrganization(groupDN, userDN string) error { modify := ldap.NewModifyRequest(groupDN, nil) modify.Delete("uniqueMember", []string{userDN}) return l.conn.Modify(modify)