gitee.com/woood2/luca@v1.0.4/cmd/backend/internal/middleware/perm.go (about)

     1  package middleware
     2  
     3  import (
     4  	"github.com/gin-gonic/gin"
     5  )
     6  
     7  func Perm(res string) gin.HandlerFunc {
     8  	return func(c *gin.Context) {
     9  		u, exists := c.Get(GinCtxKeyUid)
    10  		if !exists {
    11  			fail(c)
    12  			return
    13  		}
    14  		if u == nil {
    15  			fail(c)
    16  			return
    17  		}
    18  		uid, ok := u.(int)
    19  		if !ok {
    20  			fail(c)
    21  			return
    22  		}
    23  		if ok := checkPerm(uid, res); !ok {
    24  			fail(c)
    25  			return
    26  		}
    27  		c.Next()
    28  	}
    29  }
    30  
    31  func fail(c *gin.Context) {
    32  	c.Abort()
    33  	c.String(403, "forbidden")
    34  }
    35  
    36  func checkPerm(uid int, res string) bool {
    37  	if uid <= 0 || res == "" {
    38  		return false
    39  	}
    40  	//wanda-SDK:gRPC调用
    41  	return true
    42  }