33 lines
699 B
Go
33 lines
699 B
Go
package middlewares
|
||
|
||
import (
|
||
"github.com/gin-gonic/gin"
|
||
"net/http"
|
||
)
|
||
|
||
func RBACMiddleware() gin.HandlerFunc {
|
||
return func(c *gin.Context) {
|
||
// 获取用户ID
|
||
userID, exists := c.Get("userID")
|
||
if !exists {
|
||
utils.Error(c, http.StatusForbidden, "User authentication required")
|
||
c.Abort()
|
||
return
|
||
}
|
||
|
||
// 获取请求方法和路径
|
||
method := c.Request.Method
|
||
path := c.FullPath()
|
||
|
||
// 这里需要实现RBAC检查逻辑(示例代码)
|
||
// 实际应该查询用户的角色权限是否匹配当前请求
|
||
// if !checkPermission(userID, method, path) {
|
||
// utils.Error(c, http.StatusForbidden, "Access denied")
|
||
// c.Abort()
|
||
// return
|
||
// }
|
||
|
||
c.Next()
|
||
}
|
||
}
|