This commit is contained in:
laodaming 2023-11-17 10:22:23 +08:00
parent 62222bb781
commit 0bec257730
8 changed files with 202 additions and 3 deletions

View File

@ -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)
}
}
}

View File

@ -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)
}
}
}

View File

@ -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),
},
},
)
}

View File

@ -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)
// }

View File

@ -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)
// }

View File

@ -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 {
}

View File

@ -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
}

View File

@ -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)