新增:权限分组模块

This commit is contained in:
momo 2023-11-17 15:36:17 +08:00
parent 4cda7f0d3c
commit 4097d00f80
8 changed files with 147 additions and 7 deletions

View File

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

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

View File

@ -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",

View File

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

View File

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

View File

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

View File

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

View File

@ -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"`