|
@@ -11,7 +11,7 @@ import (
|
|
|
"strconv"
|
|
|
"strings"
|
|
|
|
|
|
- jwt "github.com/appleboy/gin-jwt/v2"
|
|
|
+ "github.com/casdoor/casdoor-go-sdk/casdoorsdk"
|
|
|
"github.com/gin-gonic/gin"
|
|
|
)
|
|
|
|
|
@@ -75,135 +75,14 @@ func ResultJWTWrapper(handle JWTHander, svc *Service) gin.HandlerFunc {
|
|
|
}
|
|
|
}()
|
|
|
|
|
|
- claims := jwt.ExtractClaims(c)
|
|
|
+ claims, _ := c.Get("casdoor_user")
|
|
|
|
|
|
- var usr *JWTUser
|
|
|
+ user := claims.(*casdoorsdk.User)
|
|
|
|
|
|
- if claims["id"] != nil {
|
|
|
- fmt.Printf("%#v\n", claims)
|
|
|
- id := claims["id"].(string)
|
|
|
- name := claims["name"].(string)
|
|
|
- phone := claims["phone"].(string)
|
|
|
- parent := claims["parent"].(string)
|
|
|
- state := int32(claims["state"].(float64))
|
|
|
-
|
|
|
- usr = &JWTUser{ID: id, Phone: phone, Name: name, Parent: parent, State: state}
|
|
|
- }
|
|
|
var apis = &ApiSession{
|
|
|
Svc: svc,
|
|
|
- User: usr,
|
|
|
- }
|
|
|
- data, err := handle(c, apis)
|
|
|
-
|
|
|
- if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
- httpErr, ok := err.(HTTPError)
|
|
|
- if ok {
|
|
|
- c.JSON(http.StatusOK, NewFailResultWithCode(httpErr.Error(), httpErr.Code))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- c.JSON(http.StatusOK, NewFailResult(err.Error()))
|
|
|
- return
|
|
|
+ User: user,
|
|
|
}
|
|
|
- if data != nil {
|
|
|
- c.JSON(http.StatusOK, NewOkResult(data))
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func ResultJWTTestWrapper(handle JWTHander, svc *Service) gin.HandlerFunc {
|
|
|
-
|
|
|
- return func(c *gin.Context) {
|
|
|
-
|
|
|
- defer func() {
|
|
|
- if r := recover(); r != nil {
|
|
|
-
|
|
|
- fmt.Println("recover success.")
|
|
|
- fmt.Println(r)
|
|
|
-
|
|
|
- buf := make([]byte, 1<<16)
|
|
|
- runtime.Stack(buf, true)
|
|
|
- fmt.Println("buf", string(buf))
|
|
|
-
|
|
|
- c.JSON(http.StatusOK, NewFailResultWithData("error", r))
|
|
|
- }
|
|
|
- }()
|
|
|
-
|
|
|
- var usr *JWTUser = &JWTUser{ID: svc.DebugUserId, Phone: svc.DebugUserPhone, Parent: svc.DebugUserId, Role: svc.DebugUserRole}
|
|
|
-
|
|
|
- data, err := handle(c, &ApiSession{Svc: svc, User: usr})
|
|
|
-
|
|
|
- if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
- httpErr, ok := err.(HTTPError)
|
|
|
- if ok {
|
|
|
- c.JSON(http.StatusOK, NewFailResultWithCode(httpErr.Error(), httpErr.Code))
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
- c.JSON(http.StatusOK, NewFailResult(err.Error()))
|
|
|
- return
|
|
|
- }
|
|
|
- c.JSON(http.StatusOK, NewOkResult(data))
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-
|
|
|
-func ResultJWTWrapperKey(handle JWTHander, svc *Service, keys []string) gin.HandlerFunc {
|
|
|
-
|
|
|
- return func(c *gin.Context) {
|
|
|
-
|
|
|
- defer func() {
|
|
|
- if r := recover(); r != nil {
|
|
|
- fmt.Println("recover success.")
|
|
|
- fmt.Println(r)
|
|
|
-
|
|
|
- buf := make([]byte, 1<<16)
|
|
|
- runtime.Stack(buf, true)
|
|
|
- fmt.Println("buf", string(buf))
|
|
|
-
|
|
|
- c.JSON(http.StatusOK, NewFailResultWithData("系统异常", r))
|
|
|
- }
|
|
|
- }()
|
|
|
-
|
|
|
- claims := jwt.ExtractClaims(c)
|
|
|
-
|
|
|
- var usr *JWTUser
|
|
|
-
|
|
|
- if claims["id"] != nil {
|
|
|
- id := claims["id"].(string)
|
|
|
- phone := claims["phone"].(string)
|
|
|
- name := claims["name"].(string)
|
|
|
- parent := claims["parent"].(string)
|
|
|
- state := int32(claims["state"].(float64))
|
|
|
- key := ""
|
|
|
- if claims["key"] != nil {
|
|
|
- key = claims["key"].(string)
|
|
|
- }
|
|
|
-
|
|
|
- usr = &JWTUser{ID: id, Name: name, Phone: phone, Parent: parent, State: state, Key: key}
|
|
|
- }
|
|
|
-
|
|
|
- var apis = &ApiSession{
|
|
|
- Svc: svc,
|
|
|
- User: usr,
|
|
|
- }
|
|
|
-
|
|
|
- flag := false
|
|
|
- for _, key := range keys {
|
|
|
- if usr.Key == key {
|
|
|
- flag = true
|
|
|
- break
|
|
|
- }
|
|
|
- }
|
|
|
- if !flag {
|
|
|
- c.JSON(http.StatusForbidden, NewFailResult("您没有权限"))
|
|
|
- c.Abort()
|
|
|
- return
|
|
|
- }
|
|
|
-
|
|
|
data, err := handle(c, apis)
|
|
|
|
|
|
if err != nil {
|