| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- package middleware
- import (
- "git.wanbits.cc/sin/flytalk/models"
- "github.com/gin-gonic/gin"
- "strings"
- )
- func RbacAuth(c *gin.Context) {
- roleId, _ := c.Get("role_id")
- role := models.FindRole(roleId)
- var methodFlag bool
- rPaths := strings.Split(c.Request.RequestURI, "?")
- if role.Method != "*" {
- methods := strings.Split(role.Method, ",")
- for _, m := range methods {
- if c.Request.Method == m {
- methodFlag = true
- break
- }
- }
- if !methodFlag {
- c.JSON(200, gin.H{
- "code": 403,
- "msg": "没有权限:" + c.Request.Method + "," + rPaths[0],
- })
- c.Abort()
- return
- }
- }
- var flag bool
- if role.Path != "*" {
- paths := strings.Split(role.Path, ",")
- for _, p := range paths {
- if rPaths[0] == p {
- flag = true
- break
- }
- }
- if !flag {
- c.JSON(200, gin.H{
- "code": 403,
- "msg": "没有权限:" + rPaths[0],
- })
- c.Abort()
- return
- }
- }
- }
|