From 4097d00f8098cd44953b0904e86a7c986c88599a Mon Sep 17 00:00:00 2001 From: momo <1012651275@qq.com> Date: Fri, 17 Nov 2023 15:36:17 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E:=E6=9D=83=E9=99=90=E5=88=86?= =?UTF-8?q?=E7=BB=84=E6=A8=A1=E5=9D=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/gmodel/ldap_group_logic.go | 14 ++++ .../internal/handler/deleteapihandler.go | 35 ++++++++++ server/ldap-admin/internal/handler/routes.go | 5 ++ .../internal/logic/deleteapilogic.go | 70 +++++++++++++++++++ .../internal/logic/deleteldapgrouplogic.go | 12 ++-- .../internal/logic/setldapcasbinrulelogic.go | 2 + server/ldap-admin/internal/types/types.go | 6 +- server_api/ldap-admin.api | 10 ++- 8 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 server/ldap-admin/internal/handler/deleteapihandler.go create mode 100644 server/ldap-admin/internal/logic/deleteapilogic.go diff --git a/model/gmodel/ldap_group_logic.go b/model/gmodel/ldap_group_logic.go index c37c2fcf..2234e12e 100644 --- a/model/gmodel/ldap_group_logic.go +++ b/model/gmodel/ldap_group_logic.go @@ -3,10 +3,24 @@ package gmodel import ( "context" "time" + + "gorm.io/gorm" ) // TODO: 使用model的属性做你想做的 +// FindAll 全部查询 +func (s *LdapGroupModel) FindAll(ctx context.Context, gorm *gorm.DB) (resp []LdapGroup, err error) { + var db = gorm + if gorm == nil { + db = s.db.WithContext(ctx).Model(&LdapGroup{}) + } else { + db = db.WithContext(ctx).Model(&LdapGroup{}) + } + err = db.Find(&resp).Error + return resp, err +} + // FindPage 分页查询 func (s *LdapGroupModel) FindPage(ctx context.Context, req FindPageReq) (resp []LdapGroup, total int64, err error) { db := s.db.WithContext(ctx).Model(&LdapGroup{}) diff --git a/server/ldap-admin/internal/handler/deleteapihandler.go b/server/ldap-admin/internal/handler/deleteapihandler.go new file mode 100644 index 00000000..d3f490ba --- /dev/null +++ b/server/ldap-admin/internal/handler/deleteapihandler.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 DeleteApiHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + + var req types.DeleteApiReq + userinfo, err := basic.RequestParse(w, r, svcCtx, &req) + if err != nil { + return + } + + // 创建一个业务逻辑层实例 + l := logic.NewDeleteApiLogic(r.Context(), svcCtx) + + rl := reflect.ValueOf(l) + basic.BeforeLogic(w, r, rl) + + resp := l.DeleteApi(&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 3e124f42..1c857bb9 100644 --- a/server/ldap-admin/internal/handler/routes.go +++ b/server/ldap-admin/internal/handler/routes.go @@ -52,6 +52,11 @@ func RegisterHandlers(server *rest.Server, serverCtx *svc.ServiceContext) { Path: "/api/ldap-admin/save_api", Handler: SaveApiHandler(serverCtx), }, + { + Method: http.MethodPost, + Path: "/api/ldap-admin/delete_api", + Handler: DeleteApiHandler(serverCtx), + }, { Method: http.MethodPost, Path: "/api/ldap-admin/save_menu", diff --git a/server/ldap-admin/internal/logic/deleteapilogic.go b/server/ldap-admin/internal/logic/deleteapilogic.go new file mode 100644 index 00000000..da55a0c3 --- /dev/null +++ b/server/ldap-admin/internal/logic/deleteapilogic.go @@ -0,0 +1,70 @@ +package logic + +import ( + "errors" + "fusenapi/model/gmodel" + "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" + "gorm.io/gorm" +) + +type DeleteApiLogic struct { + logx.Logger + ctx context.Context + svcCtx *svc.ServiceContext +} + +func NewDeleteApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeleteApiLogic { + return &DeleteApiLogic{ + Logger: logx.WithContext(ctx), + ctx: ctx, + svcCtx: svcCtx, + } +} + +// 处理进入前逻辑w,r +// func (l *DeleteApiLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) { +// } + +func (l *DeleteApiLogic) DeleteApi(req *types.DeleteApiReq, userinfo *auth.UserInfo) (resp *basic.Response) { + // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) + // userinfo 传入值时, 一定不为null + _, err := l.svcCtx.AllModels.LdapApis.FindAll(l.ctx, l.svcCtx.MysqlConn.Where("id In ?", req.Ids)) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + basic.CodeServiceErr.Message = "记录不存在" + } else { + basic.CodeServiceErr.Message = "系统出错" + } + return resp.SetStatus(basic.CodeServiceErr) + } + txErr := l.svcCtx.MysqlConn.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error { + resLdapApiDel := tx.Where("id IN ?", req.Ids).Delete(&gmodel.LdapApis{}) + if resLdapApiDel.Error != nil { + return resLdapApiDel.Error + } + resLdapCasbinRulesDel := tx.Where("v3 IN ?", req.Ids).Delete(&gmodel.LdapCasbinRule{}) + if resLdapCasbinRulesDel.Error != nil { + return resLdapCasbinRulesDel.Error + } + return nil + }) + if txErr != nil { + l.Logger.Errorf("delete ldap group error: %v", txErr) + basic.CodeServiceErr.Message = "删除失败" + return resp.SetStatus(basic.CodeServiceErr) + } + return resp.SetStatus(basic.CodeOK) +} + +// 处理逻辑后 w,r 如:重定向, resp 必须重新处理 +// func (l *DeleteApiLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) { +// // httpx.OkJsonCtx(r.Context(), w, resp) +// } diff --git a/server/ldap-admin/internal/logic/deleteldapgrouplogic.go b/server/ldap-admin/internal/logic/deleteldapgrouplogic.go index 1d565110..d50857fc 100644 --- a/server/ldap-admin/internal/logic/deleteldapgrouplogic.go +++ b/server/ldap-admin/internal/logic/deleteldapgrouplogic.go @@ -36,7 +36,7 @@ func NewDeleteLdapGroupLogic(ctx context.Context, svcCtx *svc.ServiceContext) *D func (l *DeleteLdapGroupLogic) DeleteLdapGroup(req *types.DeleteLdapGroupReq, userinfo *auth.UserInfo) (resp *basic.Response) { // 返回值必须调用Set重新返回, resp可以空指针调用 resp.SetStatus(basic.CodeOK, data) // userinfo 传入值时, 一定不为null - resLdapGroupInfo, err := l.svcCtx.AllModels.LdapGroup.FindOneById(l.ctx, req.Id) + _, err := l.svcCtx.AllModels.LdapGroup.FindAll(l.ctx, l.svcCtx.MysqlConn.Where("id In ?", req.Ids)) if err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { basic.CodeServiceErr.Message = "记录不存在" @@ -47,18 +47,22 @@ func (l *DeleteLdapGroupLogic) DeleteLdapGroup(req *types.DeleteLdapGroupReq, us } txErr := l.svcCtx.MysqlConn.WithContext(l.ctx).Transaction(func(tx *gorm.DB) error { //删除权限组、权限组相关 - resLdapGroupDel := tx.Delete(&resLdapGroupInfo) + resLdapGroupDel := tx.Where("id IN ?", req.Ids).Delete(&gmodel.LdapGroup{}) if resLdapGroupDel.Error != nil { return resLdapGroupDel.Error } - resLdapUserGroupDel := tx.Where("group_id = ?", resLdapGroupInfo.Id).Delete(&gmodel.LdapUserGroup{}) + resLdapUserGroupDel := tx.Where("group_id IN ?", req.Ids).Delete(&gmodel.LdapUserGroup{}) if resLdapUserGroupDel.Error != nil { return resLdapUserGroupDel.Error } - resLdapGroupMenusDel := tx.Where("group_id = ?", resLdapGroupInfo.Id).Delete(&gmodel.LdapGroupMenus{}) + resLdapGroupMenusDel := tx.Where("group_id IN ?", req.Ids).Delete(&gmodel.LdapGroupMenus{}) if resLdapGroupMenusDel.Error != nil { return resLdapGroupMenusDel.Error } + resLdapCasbinRulesDel := tx.Where("v0 IN ?", req.Ids).Delete(&gmodel.LdapCasbinRule{}) + if resLdapCasbinRulesDel.Error != nil { + return resLdapCasbinRulesDel.Error + } return nil }) if txErr != nil { diff --git a/server/ldap-admin/internal/logic/setldapcasbinrulelogic.go b/server/ldap-admin/internal/logic/setldapcasbinrulelogic.go index 04dac6c3..47f74d49 100644 --- a/server/ldap-admin/internal/logic/setldapcasbinrulelogic.go +++ b/server/ldap-admin/internal/logic/setldapcasbinrulelogic.go @@ -54,10 +54,12 @@ func (l *SetLdapCasbinRuleLogic) SetLdapCasbinRule(req *types.SetLdapCasbinRuleR var groupIdStr = strconv.Itoa(int(resLdapGroupInfo.Id)) var ldapCasbinRules []gmodel.LdapCasbinRule for _, ldapApi := range resLdapApiList { + var apiIdStr = strconv.Itoa(int(ldapApi.Id)) ldapCasbinRules = append(ldapCasbinRules, gmodel.LdapCasbinRule{ V0: &groupIdStr, V1: ldapApi.Path, V2: ldapApi.Method, + V3: &apiIdStr, }) } resCreateInBatches := l.svcCtx.MysqlConn.WithContext(l.ctx).CreateInBatches(ldapCasbinRules, 100) diff --git a/server/ldap-admin/internal/types/types.go b/server/ldap-admin/internal/types/types.go index 07986686..53bd87db 100644 --- a/server/ldap-admin/internal/types/types.go +++ b/server/ldap-admin/internal/types/types.go @@ -25,7 +25,11 @@ type SaveLdapGroupReq struct { } type DeleteLdapGroupReq struct { - Id int64 `json:"id"` //id + Ids []int64 `json:"ids"` //id +} + +type DeleteApiReq struct { + Ids []int64 `json:"ids"` //id } type SetLdapGroupMenusReq struct { diff --git a/server_api/ldap-admin.api b/server_api/ldap-admin.api index ff526c9c..3d8776d5 100644 --- a/server_api/ldap-admin.api +++ b/server_api/ldap-admin.api @@ -34,7 +34,10 @@ service ldap-admin { //保存API @handler SaveApiHandler post /api/ldap-admin/save_api(SaveApiReq) returns (response); - + //删除API + @handler DeleteApiHandler + post /api/ldap-admin/delete_api(DeleteApiReq) returns (response); + //保存菜单 @handler SaveMenuHandler post /api/ldap-admin/save_menu(SaveMenuReq) returns (response); @@ -101,7 +104,10 @@ type ( Sort int64 `json:"sort"` // 排序 } DeleteLdapGroupReq { - Id int64 `json:"id"` //id + Ids []int64 `json:"ids"` //id + } + DeleteApiReq { + Ids []int64 `json:"ids"` //id } SetLdapGroupMenusReq { GroupId int64 `json:"group_id"`