Compare commits

...

2 Commits

Author SHA1 Message Date
tiger_zhou
c6fe82c7e6 Merge remote-tracking branch 'origin/develop' into develop
All checks were successful
local-test分支打包构建docker并发布运行 / Docker-Build (push) Successful in 19m32s
2024-10-09 18:05:20 +08:00
tiger_zhou
0dd0d5db67 权限不足添加对应的错误code,所有http get请求直接放过 2024-10-09 18:02:42 +08:00
5 changed files with 36 additions and 4 deletions

View File

@ -22,10 +22,12 @@ import (
)
func InitSimulationRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
authed := api.Group("/v1/simulation").Use(authMiddleware.MiddlewareFunc(), middleware.PermissMiddleware)
authed.POST("/createByProject", createByProjectId)
authed.POST("/destroy/:id", destroy)
authed.GET("/list", findAllSimulations)
authed.POST("/check/data", checkSimMapData)
authed.POST("/train/add", addTrain)
authed.POST("/train/config", configTrain)

View File

@ -21,6 +21,7 @@ func InitUserRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware)
authed := api.Group("/v1/user").Use(authMiddleware.MiddlewareFunc(), middleware.PermissMiddleware)
authed.GET("/paging", pageQueryUser)
authed.GET("/current", findUserInfo)
}
// 用户注册

View File

@ -2,6 +2,7 @@ package middleware
import (
"log/slog"
"net/http"
"regexp"
"strings"
@ -36,12 +37,16 @@ func permissionMiddleware() gin.HandlerFunc {
return
}
path, method := c.Request.URL.Path, c.Request.Method
if method == http.MethodGet {
c.Next()
return
}
if validateUserPath(path, method, userAuth.AuthPaths) { // 用户有权限
c.Next()
return
}
slog.Error("无权限操作请求路径", "path", path, "method", method)
panic(sys_error.New("权限不足"))
panic(sys_error.NewCode("权限不足", 403))
}
}

View File

@ -86,7 +86,11 @@ func initServer() *gin.Engine {
}
}
c.Error(be)
c.JSON(http.StatusInternalServerError, &dto.ErrorDto{
statusCode := http.StatusInternalServerError
if be.ErrorCode > 0 {
statusCode = http.StatusForbidden
}
c.JSON(statusCode, &dto.ErrorDto{
Tip: be.UserMsg,
Message: be.Error(),
})

View File

@ -11,6 +11,7 @@ type BusinessError struct {
UserMsg string
// 错误信息传递(用于开发回溯定位,不给用户展示)
Errors []string
ErrorCode int
}
// 新建业务错误
@ -34,7 +35,26 @@ func New(userMsg string, errs ...error) *BusinessError {
// Errors: convert(errs),
}
}
func NewCode(userMsg string, errCode int, errs ...error) *BusinessError {
if len(errs) == 1 {
be, ok := errs[0].(*BusinessError)
if ok {
be.prependUserMsg(userMsg)
return be
} else {
return &BusinessError{
UserMsg: userMsg,
ErrorCode: errCode,
Errors: []string{errs[0].Error()},
}
}
}
return &BusinessError{
UserMsg: userMsg,
ErrorCode: errCode,
// Errors: convert(errs),
}
}
func IsBusinessError(err error) bool {
_, ok := err.(*BusinessError)
return ok