权限不足添加对应的错误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) {
|
func InitSimulationRouter(api *gin.RouterGroup, authMiddleware *jwt.GinJWTMiddleware) {
|
||||||
|
|
||||||
authed := api.Group("/v1/simulation").Use(authMiddleware.MiddlewareFunc(), middleware.PermissMiddleware)
|
authed := api.Group("/v1/simulation").Use(authMiddleware.MiddlewareFunc(), middleware.PermissMiddleware)
|
||||||
authed.POST("/createByProject", createByProjectId)
|
authed.POST("/createByProject", createByProjectId)
|
||||||
authed.POST("/destroy/:id", destroy)
|
authed.POST("/destroy/:id", destroy)
|
||||||
authed.GET("/list", findAllSimulations)
|
authed.GET("/list", findAllSimulations)
|
||||||
|
|
||||||
authed.POST("/check/data", checkSimMapData)
|
authed.POST("/check/data", checkSimMapData)
|
||||||
authed.POST("/train/add", addTrain)
|
authed.POST("/train/add", addTrain)
|
||||||
authed.POST("/train/config", configTrain)
|
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 := api.Group("/v1/user").Use(authMiddleware.MiddlewareFunc(), middleware.PermissMiddleware)
|
||||||
authed.GET("/paging", pageQueryUser)
|
authed.GET("/paging", pageQueryUser)
|
||||||
authed.GET("/current", findUserInfo)
|
authed.GET("/current", findUserInfo)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 用户注册
|
// 用户注册
|
||||||
|
|
|
@ -2,6 +2,7 @@ package middleware
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -36,12 +37,16 @@ func permissionMiddleware() gin.HandlerFunc {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
path, method := c.Request.URL.Path, c.Request.Method
|
path, method := c.Request.URL.Path, c.Request.Method
|
||||||
|
if method == http.MethodGet {
|
||||||
|
c.Next()
|
||||||
|
return
|
||||||
|
}
|
||||||
if validateUserPath(path, method, userAuth.AuthPaths) { // 用户有权限
|
if validateUserPath(path, method, userAuth.AuthPaths) { // 用户有权限
|
||||||
c.Next()
|
c.Next()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
slog.Error("无权限操作请求路径", "path", path, "method", method)
|
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.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,
|
Tip: be.UserMsg,
|
||||||
Message: be.Error(),
|
Message: be.Error(),
|
||||||
})
|
})
|
||||||
|
|
|
@ -10,7 +10,8 @@ type BusinessError struct {
|
||||||
// 用户提示信息
|
// 用户提示信息
|
||||||
UserMsg string
|
UserMsg string
|
||||||
// 错误信息传递(用于开发回溯定位,不给用户展示)
|
// 错误信息传递(用于开发回溯定位,不给用户展示)
|
||||||
Errors []string
|
Errors []string
|
||||||
|
ErrorCode int
|
||||||
}
|
}
|
||||||
|
|
||||||
// 新建业务错误
|
// 新建业务错误
|
||||||
|
@ -34,7 +35,26 @@ func New(userMsg string, errs ...error) *BusinessError {
|
||||||
// Errors: convert(errs),
|
// 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 {
|
func IsBusinessError(err error) bool {
|
||||||
_, ok := err.(*BusinessError)
|
_, ok := err.(*BusinessError)
|
||||||
return ok
|
return ok
|
||||||
|
|
Loading…
Reference in New Issue