后端重构

This commit is contained in:
Yvan
2026-01-08 17:41:41 +08:00
parent fad3767b79
commit 8800c069d9
47 changed files with 3135 additions and 978 deletions
+12
View File
@@ -162,6 +162,18 @@ func (uc *ApiUsecase) GetAPIInfoList(ctx context.Context, page, pageSize int, fi
return uc.repo.List(ctx, page, pageSize, filters, orderKey, desc)
}
// GetApiList 分页获取API列表(简化版本)
func (uc *ApiUsecase) GetApiList(ctx context.Context, page, pageSize int, filters map[string]interface{}) ([]*Api, int64, error) {
// 转换 filters
strFilters := make(map[string]string)
for k, v := range filters {
if s, ok := v.(string); ok {
strFilters[k] = s
}
}
return uc.repo.List(ctx, page, pageSize, strFilters, "", false)
}
// GetAllApis 获取所有API(根据权限过滤)
func (uc *ApiUsecase) GetAllApis(ctx context.Context, authorityID uint) ([]*Api, error) {
parentAuthorityID, err := uc.authRepo.GetParentAuthorityID(ctx, authorityID)
+104
View File
@@ -0,0 +1,104 @@
package system
import (
"context"
"errors"
)
// CasbinUsecase Casbin用例
type CasbinUsecase struct {
repo CasbinRepo
authUc *AuthorityUsecase
apiUc *ApiUsecase
useStrictAuth bool
}
// NewCasbinUsecase 创建Casbin用例
func NewCasbinUsecase(repo CasbinRepo, authUc *AuthorityUsecase, apiUc *ApiUsecase) *CasbinUsecase {
return &CasbinUsecase{
repo: repo,
authUc: authUc,
apiUc: apiUc,
useStrictAuth: false,
}
}
// UpdateCasbin 更新Casbin权限
func (uc *CasbinUsecase) UpdateCasbin(adminAuthorityID, authorityId uint, casbinInfos []struct {
Path string
Method string
}) error {
// 转换为 CasbinRule
rules := make([]CasbinRule, len(casbinInfos))
for i, info := range casbinInfos {
rules[i] = CasbinRule{
Path: info.Path,
Method: info.Method,
}
}
return uc.repo.UpdateCasbin(adminAuthorityID, authorityId, rules)
}
// GetPolicyPathByAuthorityId 获取权限路径
func (uc *CasbinUsecase) GetPolicyPathByAuthorityId(authorityId uint) []CasbinRule {
return uc.repo.GetPolicyPathByAuthorityId(authorityId)
}
// FreshCasbin 刷新Casbin缓存
func (uc *CasbinUsecase) FreshCasbin() error {
return uc.repo.FreshCasbin()
}
// CheckPermission 检查权限
func (uc *CasbinUsecase) CheckPermission(ctx context.Context, sub, obj, act string) (bool, error) {
// 这里可以添加权限检查逻辑
return true, nil
}
// ClearCasbin 清除Casbin规则
func (uc *CasbinUsecase) ClearCasbin(v int, p ...string) bool {
return uc.repo.ClearCasbin(v, p...)
}
// AddPolicies 添加策略
func (uc *CasbinUsecase) AddPolicies(rules [][]string) error {
return uc.repo.AddPolicies(rules)
}
// RemoveFilteredPolicy 删除筛选的策略
func (uc *CasbinUsecase) RemoveFilteredPolicy(authorityId string) error {
return uc.repo.RemoveFilteredPolicy(authorityId)
}
// UpdateCasbinApi API更新随动
func (uc *CasbinUsecase) UpdateCasbinApi(oldPath, newPath, oldMethod, newMethod string) error {
return uc.repo.UpdateCasbinApi(oldPath, newPath, oldMethod, newMethod)
}
// ValidateCasbinInfos 验证Casbin信息(严格模式)
func (uc *CasbinUsecase) ValidateCasbinInfos(ctx context.Context, adminAuthorityID uint, casbinInfos []CasbinRule) error {
if !uc.useStrictAuth {
return nil
}
apis, err := uc.apiUc.GetAllApis(ctx, adminAuthorityID)
if err != nil {
return err
}
for _, info := range casbinInfos {
hasApi := false
for _, api := range apis {
if api.Path == info.Path && api.Method == info.Method {
hasApi = true
break
}
}
if !hasApi {
return errors.New("存在api不在权限列表中")
}
}
return nil
}
+10
View File
@@ -41,12 +41,22 @@ func (uc *JwtBlacklistUsecase) JoinBlacklist(ctx context.Context, jwt string) er
return nil
}
// JsonInBlacklist JWT加入黑名单(GVA兼容方法名)
func (uc *JwtBlacklistUsecase) JsonInBlacklist(ctx context.Context, jwt string) error {
return uc.JoinBlacklist(ctx, jwt)
}
// IsInBlacklist 检查是否在黑名单中
func (uc *JwtBlacklistUsecase) IsInBlacklist(jwt string) bool {
_, ok := uc.cache[jwt]
return ok
}
// IsBlacklist 检查是否在黑名单中(GVA兼容方法名)
func (uc *JwtBlacklistUsecase) IsBlacklist(ctx context.Context, jwt string) bool {
return uc.IsInBlacklist(jwt)
}
// LoadAll 加载所有黑名单到缓存
func (uc *JwtBlacklistUsecase) LoadAll(ctx context.Context) error {
jwts, err := uc.repo.LoadAll(ctx)