权限不足添加对应的错误code,所有http get请求直接放过
This commit is contained in:
parent
0921835d27
commit
0dd0d5db67
@ -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)
|
||||
|
@ -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)
|
||||
|
||||
}
|
||||
|
||||
// 用户注册
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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(),
|
||||
})
|
||||
|
@ -10,7 +10,8 @@ type BusinessError struct {
|
||||
// 用户提示信息
|
||||
UserMsg string
|
||||
// 错误信息传递(用于开发回溯定位,不给用户展示)
|
||||
Errors []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
|
||||
|
Loading…
Reference in New Issue
Block a user